wfcqueue: document first/next return values
[urcu.git] / urcu / static / wfstack.h
CommitLineData
edac6b69
MD
1#ifndef _URCU_STATIC_WFSTACK_H
2#define _URCU_STATIC_WFSTACK_H
294d3396
MD
3
4/*
edac6b69 5 * urcu/static/wfstack.h
294d3396 6 *
edac6b69 7 * Userspace RCU library - Stack with with wait-free push, blocking traversal.
294d3396 8 *
07c2a4fd
MD
9 * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See urcu/wfstack.h for
10 * linking dynamically with the userspace rcu library.
294d3396 11 *
a03a0f42 12 * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
294d3396
MD
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#include <pthread.h>
30#include <assert.h>
b57aee66 31#include <poll.h>
edac6b69 32#include <stdbool.h>
294d3396 33#include <urcu/compiler.h>
a2e7bf9c 34#include <urcu/uatomic.h>
294d3396
MD
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
edac6b69 40#define CDS_WFS_END ((void *) 0x1UL)
16aa9ee8
DG
41#define CDS_WFS_ADAPT_ATTEMPTS 10 /* Retry if being set */
42#define CDS_WFS_WAIT 10 /* Wait 10 ms if being set */
294d3396 43
edac6b69
MD
44/*
45 * Stack with wait-free push, blocking traversal.
46 *
47 * Stack implementing push, pop, pop_all operations, as well as iterator
48 * on the stack head returned by pop_all.
49 *
c97c6ce5
MD
50 * Wait-free operations: cds_wfs_push, __cds_wfs_pop_all, cds_wfs_empty,
51 * cds_wfs_first.
52 * Blocking operations: cds_wfs_pop, cds_wfs_pop_all, cds_wfs_next,
53 * iteration on stack head returned by pop_all.
edac6b69
MD
54 *
55 * Synchronization table:
56 *
57 * External synchronization techniques described in the API below is
58 * required between pairs marked with "X". No external synchronization
59 * required between pairs marked with "-".
60 *
61 * cds_wfs_push __cds_wfs_pop __cds_wfs_pop_all
62 * cds_wfs_push - - -
63 * __cds_wfs_pop - X X
64 * __cds_wfs_pop_all - X -
65 *
66 * cds_wfs_pop and cds_wfs_pop_all use an internal mutex to provide
67 * synchronization.
68 */
69
70/*
71 * cds_wfs_node_init: initialize wait-free stack node.
72 */
756a0322 73static inline
16aa9ee8 74void _cds_wfs_node_init(struct cds_wfs_node *node)
294d3396
MD
75{
76 node->next = NULL;
77}
78
edac6b69
MD
79/*
80 * cds_wfs_init: initialize wait-free stack.
81 */
756a0322 82static inline
16aa9ee8 83void _cds_wfs_init(struct cds_wfs_stack *s)
294d3396
MD
84{
85 int ret;
86
edac6b69 87 s->head = CDS_WFS_END;
294d3396
MD
88 ret = pthread_mutex_init(&s->lock, NULL);
89 assert(!ret);
90}
91
edac6b69
MD
92static inline bool ___cds_wfs_end(void *node)
93{
94 return node == CDS_WFS_END;
95}
96
191098fc 97/*
edac6b69
MD
98 * cds_wfs_empty: return whether wait-free stack is empty.
99 *
100 * No memory barrier is issued. No mutual exclusion is required.
101 */
102static inline bool _cds_wfs_empty(struct cds_wfs_stack *s)
103{
104 return ___cds_wfs_end(CMM_LOAD_SHARED(s->head));
105}
106
107/*
108 * cds_wfs_push: push a node into the stack.
109 *
110 * Issues a full memory barrier before push. No mutual exclusion is
111 * required.
112 *
113 * Returns 0 if the stack was empty prior to adding the node.
114 * Returns non-zero otherwise.
191098fc 115 */
756a0322 116static inline
191098fc 117int _cds_wfs_push(struct cds_wfs_stack *s, struct cds_wfs_node *node)
294d3396 118{
edac6b69 119 struct cds_wfs_head *old_head, *new_head;
294d3396
MD
120
121 assert(node->next == NULL);
edac6b69 122 new_head = caa_container_of(node, struct cds_wfs_head, node);
294d3396 123 /*
edac6b69
MD
124 * uatomic_xchg() implicit memory barrier orders earlier stores
125 * to node (setting it to NULL) before publication.
294d3396 126 */
edac6b69 127 old_head = uatomic_xchg(&s->head, new_head);
294d3396 128 /*
edac6b69
MD
129 * At this point, dequeuers see a NULL node->next, they should
130 * busy-wait until node->next is set to old_head.
294d3396 131 */
edac6b69
MD
132 CMM_STORE_SHARED(node->next, &old_head->node);
133 return !___cds_wfs_end(old_head);
294d3396
MD
134}
135
136/*
edac6b69 137 * Waiting for push to complete enqueue and return the next node.
294d3396 138 */
edac6b69
MD
139static inline struct cds_wfs_node *
140___cds_wfs_node_sync_next(struct cds_wfs_node *node)
294d3396 141{
edac6b69 142 struct cds_wfs_node *next;
294d3396
MD
143 int attempt = 0;
144
294d3396
MD
145 /*
146 * Adaptative busy-looping waiting for push to complete.
147 */
edac6b69 148 while ((next = CMM_LOAD_SHARED(node->next)) == NULL) {
16aa9ee8
DG
149 if (++attempt >= CDS_WFS_ADAPT_ATTEMPTS) {
150 poll(NULL, 0, CDS_WFS_WAIT); /* Wait for 10ms */
294d3396 151 attempt = 0;
edac6b69 152 } else {
06f22bdb 153 caa_cpu_relax();
edac6b69 154 }
294d3396 155 }
edac6b69
MD
156
157 return next;
294d3396
MD
158}
159
edac6b69
MD
160/*
161 * __cds_wfs_pop_blocking: pop a node from the stack.
162 *
163 * Returns NULL if stack is empty.
164 *
165 * __cds_wfs_pop_blocking needs to be synchronized using one of the
166 * following techniques:
167 *
168 * 1) Calling __cds_wfs_pop_blocking under rcu read lock critical
169 * section. The caller must wait for a grace period to pass before
170 * freeing the returned node or modifying the cds_wfs_node structure.
171 * 2) Using mutual exclusion (e.g. mutexes) to protect
172 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
173 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
174 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
175 */
756a0322 176static inline
16aa9ee8 177struct cds_wfs_node *
edac6b69
MD
178___cds_wfs_pop_blocking(struct cds_wfs_stack *s)
179{
180 struct cds_wfs_head *head, *new_head;
181 struct cds_wfs_node *next;
182
183 for (;;) {
184 head = CMM_LOAD_SHARED(s->head);
185 if (___cds_wfs_end(head))
186 return NULL;
187 next = ___cds_wfs_node_sync_next(&head->node);
188 new_head = caa_container_of(next, struct cds_wfs_head, node);
189 if (uatomic_cmpxchg(&s->head, head, new_head) == head)
190 return &head->node;
191 /* busy-loop if head changed under us */
192 }
193}
194
195/*
196 * __cds_wfs_pop_all: pop all nodes from a stack.
197 *
198 * __cds_wfs_pop_all does not require any synchronization with other
199 * push, nor with other __cds_wfs_pop_all, but requires synchronization
200 * matching the technique used to synchronize __cds_wfs_pop_blocking:
201 *
202 * 1) If __cds_wfs_pop_blocking is called under rcu read lock critical
203 * section, both __cds_wfs_pop_blocking and cds_wfs_pop_all callers
204 * must wait for a grace period to pass before freeing the returned
205 * node or modifying the cds_wfs_node structure. However, no RCU
206 * read-side critical section is needed around __cds_wfs_pop_all.
207 * 2) Using mutual exclusion (e.g. mutexes) to protect
208 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
209 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
210 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
211 */
212static inline
213struct cds_wfs_head *
214___cds_wfs_pop_all(struct cds_wfs_stack *s)
215{
216 struct cds_wfs_head *head;
217
218 /*
219 * Implicit memory barrier after uatomic_xchg() matches implicit
220 * memory barrier before uatomic_xchg() in cds_wfs_push. It
221 * ensures that all nodes of the returned list are consistent.
222 * There is no need to issue memory barriers when iterating on
223 * the returned list, because the full memory barrier issued
224 * prior to each uatomic_cmpxchg, which each write to head, are
225 * taking care to order writes to each node prior to the full
226 * memory barrier after this uatomic_xchg().
227 */
228 head = uatomic_xchg(&s->head, CDS_WFS_END);
229 if (___cds_wfs_end(head))
230 return NULL;
231 return head;
232}
233
234/*
235 * cds_wfs_pop_lock: lock stack pop-protection mutex.
236 */
237static inline void _cds_wfs_pop_lock(struct cds_wfs_stack *s)
294d3396 238{
294d3396
MD
239 int ret;
240
241 ret = pthread_mutex_lock(&s->lock);
242 assert(!ret);
edac6b69
MD
243}
244
245/*
246 * cds_wfs_pop_unlock: unlock stack pop-protection mutex.
247 */
248static inline void _cds_wfs_pop_unlock(struct cds_wfs_stack *s)
249{
250 int ret;
251
294d3396
MD
252 ret = pthread_mutex_unlock(&s->lock);
253 assert(!ret);
edac6b69
MD
254}
255
256/*
257 * Call __cds_wfs_pop_blocking with an internal pop mutex held.
258 */
259static inline
260struct cds_wfs_node *
261_cds_wfs_pop_blocking(struct cds_wfs_stack *s)
262{
263 struct cds_wfs_node *retnode;
264
265 _cds_wfs_pop_lock(s);
266 retnode = ___cds_wfs_pop_blocking(s);
267 _cds_wfs_pop_unlock(s);
294d3396
MD
268 return retnode;
269}
270
edac6b69
MD
271/*
272 * Call __cds_wfs_pop_all with an internal pop mutex held.
273 */
274static inline
275struct cds_wfs_head *
276_cds_wfs_pop_all_blocking(struct cds_wfs_stack *s)
277{
278 struct cds_wfs_head *rethead;
279
280 _cds_wfs_pop_lock(s);
281 rethead = ___cds_wfs_pop_all(s);
282 _cds_wfs_pop_unlock(s);
283 return rethead;
284}
285
286/*
c7ba06ba 287 * cds_wfs_first: get first node of a popped stack.
edac6b69
MD
288 *
289 * Content written into the node before enqueue is guaranteed to be
290 * consistent, but no other memory ordering is ensured.
291 *
292 * Used by for-like iteration macros in urcu/wfstack.h:
293 * cds_wfs_for_each_blocking()
294 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
295 *
296 * Returns NULL if popped stack is empty, top stack node otherwise.
edac6b69
MD
297 */
298static inline struct cds_wfs_node *
c7ba06ba 299_cds_wfs_first(struct cds_wfs_head *head)
edac6b69
MD
300{
301 if (___cds_wfs_end(head))
302 return NULL;
303 return &head->node;
304}
305
306/*
307 * cds_wfs_next_blocking: get next node of a popped stack.
308 *
309 * Content written into the node before enqueue is guaranteed to be
310 * consistent, but no other memory ordering is ensured.
311 *
312 * Used by for-like iteration macros in urcu/wfstack.h:
313 * cds_wfs_for_each_blocking()
314 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
315 *
316 * Returns NULL if reached end of popped stack, non-NULL next stack
317 * node otherwise.
edac6b69
MD
318 */
319static inline struct cds_wfs_node *
320_cds_wfs_next_blocking(struct cds_wfs_node *node)
321{
322 struct cds_wfs_node *next;
323
324 next = ___cds_wfs_node_sync_next(node);
325 if (___cds_wfs_end(next))
326 return NULL;
327 return next;
328}
329
294d3396
MD
330#ifdef __cplusplus
331}
332#endif
333
edac6b69 334#endif /* _URCU_STATIC_WFSTACK_H */
This page took 0.039321 seconds and 4 git commands to generate.