1 #ifndef _URCU_WFSTACK_H
2 #define _URCU_WFSTACK_H
7 * Userspace RCU library - Stack with wait-free push, blocking traversal.
9 * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <urcu/compiler.h>
35 * Stack with wait-free push, blocking traversal.
37 * Stack implementing push, pop, pop_all operations, as well as iterator
38 * on the stack head returned by pop_all.
40 * Wait-free operations: cds_wfs_push, __cds_wfs_pop_all, cds_wfs_empty,
42 * Blocking operations: cds_wfs_pop, cds_wfs_pop_all, cds_wfs_next,
43 * iteration on stack head returned by pop_all.
45 * Synchronization table:
47 * External synchronization techniques described in the API below is
48 * required between pairs marked with "X". No external synchronization
49 * required between pairs marked with "-".
51 * cds_wfs_push __cds_wfs_pop __cds_wfs_pop_all
54 * __cds_wfs_pop_all - X -
56 * cds_wfs_pop and cds_wfs_pop_all use an internal mutex to provide
60 #define CDS_WFS_WOULDBLOCK ((struct cds_wfs_node *) -1UL)
63 CDS_WFS_STATE_LAST
= (1U << 0),
67 * struct cds_wfs_node is returned by __cds_wfs_pop, and also used as
68 * iterator on stack. It is not safe to dereference the node next
69 * pointer when returned by __cds_wfs_pop_blocking.
72 struct cds_wfs_node
*next
;
76 * struct cds_wfs_head is returned by __cds_wfs_pop_all, and can be used
77 * to begin iteration on the stack. "node" needs to be the first field of
78 * cds_wfs_head, so the end-of-stack pointer value can be used for both
82 struct cds_wfs_node node
;
85 struct __cds_wfs_stack
{
86 struct cds_wfs_head
*head
;
89 struct cds_wfs_stack
{
90 struct cds_wfs_head
*head
;
95 * In C, the transparent union allows calling functions that work on both
96 * struct cds_wfs_stack and struct __cds_wfs_stack on any of those two
99 * In C++, implement static inline wrappers using function overloading
100 * to obtain an API similar to C.
103 struct __cds_wfs_stack
*_s
;
104 struct cds_wfs_stack
*s
;
105 } caa_c_transparent_union cds_wfs_stack_ptr_t
;
109 #include <urcu/static/wfstack.h>
111 #define cds_wfs_node_init _cds_wfs_node_init
112 #define cds_wfs_init _cds_wfs_init
113 #define cds_wfs_destroy _cds_wfs_destroy
114 #define __cds_wfs_init ___cds_wfs_init
115 #define cds_wfs_empty _cds_wfs_empty
116 #define cds_wfs_push _cds_wfs_push
118 /* Locking performed internally */
119 #define cds_wfs_pop_blocking _cds_wfs_pop_blocking
120 #define cds_wfs_pop_with_state_blocking _cds_wfs_pop_with_state_blocking
121 #define cds_wfs_pop_all_blocking _cds_wfs_pop_all_blocking
124 * For iteration on cds_wfs_head returned by __cds_wfs_pop_all or
125 * cds_wfs_pop_all_blocking.
127 #define cds_wfs_first _cds_wfs_first
128 #define cds_wfs_next_blocking _cds_wfs_next_blocking
129 #define cds_wfs_next_nonblocking _cds_wfs_next_nonblocking
131 /* Pop locking with internal mutex */
132 #define cds_wfs_pop_lock _cds_wfs_pop_lock
133 #define cds_wfs_pop_unlock _cds_wfs_pop_unlock
135 /* Synchronization ensured by the caller. See synchronization table. */
136 #define __cds_wfs_pop_blocking ___cds_wfs_pop_blocking
137 #define __cds_wfs_pop_with_state_blocking \
138 ___cds_wfs_pop_with_state_blocking
139 #define __cds_wfs_pop_nonblocking ___cds_wfs_pop_nonblocking
140 #define __cds_wfs_pop_with_state_nonblocking \
141 ___cds_wfs_pop_with_state_nonblocking
142 #define __cds_wfs_pop_all ___cds_wfs_pop_all
144 #else /* !_LGPL_SOURCE */
147 * cds_wfs_node_init: initialize wait-free stack node.
149 extern void cds_wfs_node_init(struct cds_wfs_node
*node
);
152 * cds_wfs_init: initialize wait-free stack (with lock). Pair with
155 extern void cds_wfs_init(struct cds_wfs_stack
*s
);
158 * cds_wfs_destroy: destroy wait-free stack (with lock). Pair with
161 extern void cds_wfs_destroy(struct cds_wfs_stack
*s
);
164 * __cds_wfs_init: initialize wait-free stack (no lock). Don't pair with
165 * any destroy function.
167 extern void __cds_wfs_init(struct __cds_wfs_stack
*s
);
170 * cds_wfs_empty: return whether wait-free stack is empty.
172 * No memory barrier is issued. No mutual exclusion is required.
174 extern bool cds_wfs_empty(cds_wfs_stack_ptr_t u_stack
);
177 * cds_wfs_push: push a node into the stack.
179 * Issues a full memory barrier before push. No mutual exclusion is
182 * Returns 0 if the stack was empty prior to adding the node.
183 * Returns non-zero otherwise.
185 extern int cds_wfs_push(cds_wfs_stack_ptr_t u_stack
, struct cds_wfs_node
*node
);
188 * cds_wfs_pop_blocking: pop a node from the stack.
190 * Calls __cds_wfs_pop_blocking with an internal pop mutex held.
192 extern struct cds_wfs_node
*cds_wfs_pop_blocking(struct cds_wfs_stack
*s
);
195 * cds_wfs_pop_with_state_blocking: pop a node from the stack, with state.
197 * Same as cds_wfs_pop_blocking, but stores whether the stack was
198 * empty into state (CDS_WFS_STATE_LAST).
200 extern struct cds_wfs_node
*
201 cds_wfs_pop_with_state_blocking(struct cds_wfs_stack
*s
, int *state
);
204 * cds_wfs_pop_all_blocking: pop all nodes from a stack.
206 * Calls __cds_wfs_pop_all with an internal pop mutex held.
208 extern struct cds_wfs_head
*cds_wfs_pop_all_blocking(struct cds_wfs_stack
*s
);
211 * cds_wfs_first: get first node of a popped stack.
213 * Content written into the node before enqueue is guaranteed to be
214 * consistent, but no other memory ordering is ensured.
216 * Used by for-like iteration macros in urcu/wfstack.h:
217 * cds_wfs_for_each_blocking()
218 * cds_wfs_for_each_blocking_safe()
220 * Returns NULL if popped stack is empty, top stack node otherwise.
222 extern struct cds_wfs_node
*cds_wfs_first(struct cds_wfs_head
*head
);
225 * cds_wfs_next_blocking: get next node of a popped stack.
227 * Content written into the node before enqueue is guaranteed to be
228 * consistent, but no other memory ordering is ensured.
230 * Used by for-like iteration macros in urcu/wfstack.h:
231 * cds_wfs_for_each_blocking()
232 * cds_wfs_for_each_blocking_safe()
234 * Returns NULL if reached end of popped stack, non-NULL next stack
237 extern struct cds_wfs_node
*cds_wfs_next_blocking(struct cds_wfs_node
*node
);
240 * cds_wfs_next_nonblocking: get next node of a popped stack.
242 * Same as cds_wfs_next_blocking, but returns CDS_WFS_WOULDBLOCK if it
245 extern struct cds_wfs_node
*cds_wfs_next_nonblocking(struct cds_wfs_node
*node
);
248 * cds_wfs_pop_lock: lock stack pop-protection mutex.
250 extern void cds_wfs_pop_lock(struct cds_wfs_stack
*s
);
253 * cds_wfs_pop_unlock: unlock stack pop-protection mutex.
255 extern void cds_wfs_pop_unlock(struct cds_wfs_stack
*s
);
258 * __cds_wfs_pop_blocking: pop a node from the stack.
260 * Returns NULL if stack is empty.
262 * __cds_wfs_pop_blocking needs to be synchronized using one of the
263 * following techniques:
265 * 1) Calling __cds_wfs_pop_blocking under rcu read lock critical
266 * section. The caller must wait for a grace period to pass before
267 * freeing the returned node or modifying the cds_wfs_node structure.
268 * 2) Using mutual exclusion (e.g. mutexes) to protect
269 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
270 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
271 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
273 extern struct cds_wfs_node
*__cds_wfs_pop_blocking(cds_wfs_stack_ptr_t u_stack
);
276 * __cds_wfs_pop_with_state_blocking: pop a node from the stack, with state.
278 * Same as __cds_wfs_pop_blocking, but stores whether the stack was
279 * empty into state (CDS_WFS_STATE_LAST).
281 extern struct cds_wfs_node
*
282 __cds_wfs_pop_with_state_blocking(cds_wfs_stack_ptr_t u_stack
,
286 * __cds_wfs_pop_nonblocking: pop a node from the stack.
288 * Same as __cds_wfs_pop_blocking, but returns CDS_WFS_WOULDBLOCK if
291 extern struct cds_wfs_node
*__cds_wfs_pop_nonblocking(cds_wfs_stack_ptr_t u_stack
);
294 * __cds_wfs_pop_with_state_nonblocking: pop a node from the stack, with state.
296 * Same as __cds_wfs_pop_nonblocking, but stores whether the stack was
297 * empty into state (CDS_WFS_STATE_LAST).
299 extern struct cds_wfs_node
*
300 __cds_wfs_pop_with_state_nonblocking(cds_wfs_stack_ptr_t u_stack
,
304 * __cds_wfs_pop_all: pop all nodes from a stack.
306 * __cds_wfs_pop_all does not require any synchronization with other
307 * push, nor with other __cds_wfs_pop_all, but requires synchronization
308 * matching the technique used to synchronize __cds_wfs_pop_blocking:
310 * 1) If __cds_wfs_pop_blocking is called under rcu read lock critical
311 * section, both __cds_wfs_pop_blocking and cds_wfs_pop_all callers
312 * must wait for a grace period to pass before freeing the returned
313 * node or modifying the cds_wfs_node structure. However, no RCU
314 * read-side critical section is needed around __cds_wfs_pop_all.
315 * 2) Using mutual exclusion (e.g. mutexes) to protect
316 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
317 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
318 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
320 extern struct cds_wfs_head
*__cds_wfs_pop_all(cds_wfs_stack_ptr_t u_stack
);
322 #endif /* !_LGPL_SOURCE */
325 * cds_wfs_for_each_blocking: Iterate over all nodes returned by
326 * __cds_wfs_pop_all().
327 * @head: head of the queue (struct cds_wfs_head pointer).
328 * @node: iterator (struct cds_wfs_node pointer).
330 * Content written into each node before enqueue is guaranteed to be
331 * consistent, but no other memory ordering is ensured.
333 #define cds_wfs_for_each_blocking(head, node) \
334 for (node = cds_wfs_first(head); \
336 node = cds_wfs_next_blocking(node))
339 * cds_wfs_for_each_blocking_safe: Iterate over all nodes returned by
340 * __cds_wfs_pop_all(). Safe against deletion.
341 * @head: head of the queue (struct cds_wfs_head pointer).
342 * @node: iterator (struct cds_wfs_node pointer).
343 * @n: struct cds_wfs_node pointer holding the next pointer (used
346 * Content written into each node before enqueue is guaranteed to be
347 * consistent, but no other memory ordering is ensured.
349 #define cds_wfs_for_each_blocking_safe(head, node, n) \
350 for (node = cds_wfs_first(head), \
351 n = (node ? cds_wfs_next_blocking(node) : NULL); \
353 node = n, n = (node ? cds_wfs_next_blocking(node) : NULL))
359 * In C++, implement static inline wrappers using function overloading
360 * to obtain an API similar to C.
363 static inline cds_wfs_stack_ptr_t
cds_wfs_stack_cast(struct __cds_wfs_stack
*s
)
365 cds_wfs_stack_ptr_t ret
= {
371 static inline cds_wfs_stack_ptr_t
cds_wfs_stack_cast(struct cds_wfs_stack
*s
)
373 cds_wfs_stack_ptr_t ret
= {
379 static inline bool cds_wfs_empty(struct __cds_wfs_stack
*s
)
381 return cds_wfs_empty(cds_wfs_stack_cast(s
));
384 static inline bool cds_wfs_empty(struct cds_wfs_stack
*s
)
386 return cds_wfs_empty(cds_wfs_stack_cast(s
));
389 static inline int cds_wfs_push(struct __cds_wfs_stack
*s
, struct cds_wfs_node
*node
)
391 return cds_wfs_push(cds_wfs_stack_cast(s
), node
);
394 static inline int cds_wfs_push(struct cds_wfs_stack
*s
, struct cds_wfs_node
*node
)
396 return cds_wfs_push(cds_wfs_stack_cast(s
), node
);
399 static inline struct cds_wfs_node
*__cds_wfs_pop_blocking(struct __cds_wfs_stack
*s
)
401 return __cds_wfs_pop_blocking(cds_wfs_stack_cast(s
));
404 static inline struct cds_wfs_node
*__cds_wfs_pop_blocking(struct cds_wfs_stack
*s
)
406 return __cds_wfs_pop_blocking(cds_wfs_stack_cast(s
));
409 static inline struct cds_wfs_node
*
410 __cds_wfs_pop_with_state_blocking(struct __cds_wfs_stack
*s
, int *state
)
412 return __cds_wfs_pop_with_state_blocking(cds_wfs_stack_cast(s
), state
);
415 static inline struct cds_wfs_node
*
416 __cds_wfs_pop_with_state_blocking(struct cds_wfs_stack
*s
, int *state
)
418 return __cds_wfs_pop_with_state_blocking(cds_wfs_stack_cast(s
), state
);
421 static inline struct cds_wfs_node
*__cds_wfs_pop_nonblocking(struct __cds_wfs_stack
*s
)
424 return __cds_wfs_pop_nonblocking(cds_wfs_stack_cast(s
));
427 static inline struct cds_wfs_node
*__cds_wfs_pop_nonblocking(struct cds_wfs_stack
*s
)
429 return __cds_wfs_pop_nonblocking(cds_wfs_stack_cast(s
));
432 static inline struct cds_wfs_node
*
433 __cds_wfs_pop_with_state_nonblocking(struct __cds_wfs_stack
*s
, int *state
)
435 return __cds_wfs_pop_with_state_nonblocking(cds_wfs_stack_cast(s
), state
);
438 static inline struct cds_wfs_node
*
439 __cds_wfs_pop_with_state_nonblocking(struct cds_wfs_stack
*s
, int *state
)
441 return __cds_wfs_pop_with_state_nonblocking(cds_wfs_stack_cast(s
), state
);
444 static inline struct cds_wfs_head
*__cds_wfs_pop_all(struct __cds_wfs_stack
*s
)
446 return __cds_wfs_pop_all(cds_wfs_stack_cast(s
));
449 static inline struct cds_wfs_head
*__cds_wfs_pop_all(struct cds_wfs_stack
*s
)
451 return __cds_wfs_pop_all(cds_wfs_stack_cast(s
));
456 #endif /* _URCU_WFSTACK_H */