wfstack: combine C++ API cds_wfs_stack_cast with overloading
[userspace-rcu.git] / include / urcu / wfstack.h
CommitLineData
cb3f3d6b
MD
1#ifndef _URCU_WFSTACK_H
2#define _URCU_WFSTACK_H
3
4/*
edac6b69 5 * urcu/wfstack.h
cb3f3d6b 6 *
edac6b69 7 * Userspace RCU library - Stack with wait-free push, blocking traversal.
cb3f3d6b 8 *
a03a0f42 9 * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
cb3f3d6b
MD
10 *
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.
15 *
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.
20 *
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
24 */
25
26#include <pthread.h>
edac6b69 27#include <stdbool.h>
cb3f3d6b
MD
28#include <urcu/compiler.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
edac6b69
MD
34/*
35 * Stack with wait-free push, blocking traversal.
36 *
37 * Stack implementing push, pop, pop_all operations, as well as iterator
38 * on the stack head returned by pop_all.
39 *
c97c6ce5
MD
40 * Wait-free operations: cds_wfs_push, __cds_wfs_pop_all, cds_wfs_empty,
41 * cds_wfs_first.
42 * Blocking operations: cds_wfs_pop, cds_wfs_pop_all, cds_wfs_next,
43 * iteration on stack head returned by pop_all.
edac6b69
MD
44 *
45 * Synchronization table:
46 *
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 "-".
50 *
51 * cds_wfs_push __cds_wfs_pop __cds_wfs_pop_all
52 * cds_wfs_push - - -
53 * __cds_wfs_pop - X X
54 * __cds_wfs_pop_all - X -
55 *
56 * cds_wfs_pop and cds_wfs_pop_all use an internal mutex to provide
57 * synchronization.
58 */
59
28757437 60#define CDS_WFS_WOULDBLOCK ((struct cds_wfs_node *) -1UL)
af67624d 61
c8975b94
MD
62enum cds_wfs_state {
63 CDS_WFS_STATE_LAST = (1U << 0),
64};
65
edac6b69
MD
66/*
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.
70 */
16aa9ee8
DG
71struct cds_wfs_node {
72 struct cds_wfs_node *next;
cb3f3d6b
MD
73};
74
edac6b69
MD
75/*
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
79 * types.
80 */
81struct cds_wfs_head {
82 struct cds_wfs_node node;
83};
84
718eb63e
EW
85struct __cds_wfs_stack {
86 struct cds_wfs_head *head;
87};
88
16aa9ee8 89struct cds_wfs_stack {
edac6b69 90 struct cds_wfs_head *head;
cb3f3d6b
MD
91 pthread_mutex_t lock;
92};
93
718eb63e 94/*
3ad920b4 95 * In C, the transparent union allows calling functions that work on both
718eb63e
EW
96 * struct cds_wfs_stack and struct __cds_wfs_stack on any of those two
97 * types.
28757437 98 *
3ad920b4
MD
99 * In C++, implement static inline wrappers using function overloading
100 * to obtain an API similar to C.
718eb63e 101 */
5135c0fd 102typedef union {
718eb63e
EW
103 struct __cds_wfs_stack *_s;
104 struct cds_wfs_stack *s;
28757437 105} caa_c_transparent_union cds_wfs_stack_ptr_t;
718eb63e 106
294d3396 107#ifdef _LGPL_SOURCE
cb3f3d6b 108
af7c2dbe 109#include <urcu/static/wfstack.h>
cb3f3d6b 110
16aa9ee8 111#define cds_wfs_node_init _cds_wfs_node_init
edac6b69 112#define cds_wfs_init _cds_wfs_init
200d100e 113#define cds_wfs_destroy _cds_wfs_destroy
711ff0f9 114#define __cds_wfs_init ___cds_wfs_init
edac6b69
MD
115#define cds_wfs_empty _cds_wfs_empty
116#define cds_wfs_push _cds_wfs_push
117
118/* Locking performed internally */
119#define cds_wfs_pop_blocking _cds_wfs_pop_blocking
c8975b94 120#define cds_wfs_pop_with_state_blocking _cds_wfs_pop_with_state_blocking
edac6b69
MD
121#define cds_wfs_pop_all_blocking _cds_wfs_pop_all_blocking
122
123/*
124 * For iteration on cds_wfs_head returned by __cds_wfs_pop_all or
125 * cds_wfs_pop_all_blocking.
126 */
c7ba06ba 127#define cds_wfs_first _cds_wfs_first
edac6b69 128#define cds_wfs_next_blocking _cds_wfs_next_blocking
150fc1bb 129#define cds_wfs_next_nonblocking _cds_wfs_next_nonblocking
edac6b69
MD
130
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
134
135/* Synchronization ensured by the caller. See synchronization table. */
136#define __cds_wfs_pop_blocking ___cds_wfs_pop_blocking
c8975b94
MD
137#define __cds_wfs_pop_with_state_blocking \
138 ___cds_wfs_pop_with_state_blocking
150fc1bb 139#define __cds_wfs_pop_nonblocking ___cds_wfs_pop_nonblocking
c8975b94
MD
140#define __cds_wfs_pop_with_state_nonblocking \
141 ___cds_wfs_pop_with_state_nonblocking
edac6b69 142#define __cds_wfs_pop_all ___cds_wfs_pop_all
cb3f3d6b 143
294d3396 144#else /* !_LGPL_SOURCE */
cb3f3d6b 145
edac6b69
MD
146/*
147 * cds_wfs_node_init: initialize wait-free stack node.
148 */
16aa9ee8 149extern void cds_wfs_node_init(struct cds_wfs_node *node);
edac6b69
MD
150
151/*
200d100e
MD
152 * cds_wfs_init: initialize wait-free stack (with lock). Pair with
153 * cds_wfs_destroy().
edac6b69 154 */
16aa9ee8 155extern void cds_wfs_init(struct cds_wfs_stack *s);
edac6b69 156
718eb63e 157/*
200d100e
MD
158 * cds_wfs_destroy: destroy wait-free stack (with lock). Pair with
159 * cds_wfs_init().
160 */
161extern void cds_wfs_destroy(struct cds_wfs_stack *s);
162
163/*
164 * __cds_wfs_init: initialize wait-free stack (no lock). Don't pair with
165 * any destroy function.
718eb63e
EW
166 */
167extern void __cds_wfs_init(struct __cds_wfs_stack *s);
168
edac6b69
MD
169/*
170 * cds_wfs_empty: return whether wait-free stack is empty.
171 *
172 * No memory barrier is issued. No mutual exclusion is required.
173 */
718eb63e 174extern bool cds_wfs_empty(cds_wfs_stack_ptr_t u_stack);
edac6b69
MD
175
176/*
177 * cds_wfs_push: push a node into the stack.
178 *
179 * Issues a full memory barrier before push. No mutual exclusion is
180 * required.
181 *
182 * Returns 0 if the stack was empty prior to adding the node.
183 * Returns non-zero otherwise.
184 */
718eb63e 185extern int cds_wfs_push(cds_wfs_stack_ptr_t u_stack, struct cds_wfs_node *node);
edac6b69
MD
186
187/*
188 * cds_wfs_pop_blocking: pop a node from the stack.
189 *
190 * Calls __cds_wfs_pop_blocking with an internal pop mutex held.
191 */
16aa9ee8 192extern struct cds_wfs_node *cds_wfs_pop_blocking(struct cds_wfs_stack *s);
cb3f3d6b 193
c8975b94
MD
194/*
195 * cds_wfs_pop_with_state_blocking: pop a node from the stack, with state.
196 *
197 * Same as cds_wfs_pop_blocking, but stores whether the stack was
198 * empty into state (CDS_WFS_STATE_LAST).
199 */
200extern struct cds_wfs_node *
201 cds_wfs_pop_with_state_blocking(struct cds_wfs_stack *s, int *state);
202
edac6b69
MD
203/*
204 * cds_wfs_pop_all_blocking: pop all nodes from a stack.
205 *
206 * Calls __cds_wfs_pop_all with an internal pop mutex held.
207 */
208extern struct cds_wfs_head *cds_wfs_pop_all_blocking(struct cds_wfs_stack *s);
209
210/*
c7ba06ba 211 * cds_wfs_first: get first node of a popped stack.
edac6b69
MD
212 *
213 * Content written into the node before enqueue is guaranteed to be
214 * consistent, but no other memory ordering is ensured.
215 *
216 * Used by for-like iteration macros in urcu/wfstack.h:
217 * cds_wfs_for_each_blocking()
218 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
219 *
220 * Returns NULL if popped stack is empty, top stack node otherwise.
edac6b69 221 */
c7ba06ba 222extern struct cds_wfs_node *cds_wfs_first(struct cds_wfs_head *head);
edac6b69
MD
223
224/*
225 * cds_wfs_next_blocking: get next node of a popped stack.
226 *
227 * Content written into the node before enqueue is guaranteed to be
228 * consistent, but no other memory ordering is ensured.
229 *
230 * Used by for-like iteration macros in urcu/wfstack.h:
231 * cds_wfs_for_each_blocking()
232 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
233 *
234 * Returns NULL if reached end of popped stack, non-NULL next stack
235 * node otherwise.
edac6b69
MD
236 */
237extern struct cds_wfs_node *cds_wfs_next_blocking(struct cds_wfs_node *node);
238
af67624d
MD
239/*
240 * cds_wfs_next_nonblocking: get next node of a popped stack.
241 *
242 * Same as cds_wfs_next_blocking, but returns CDS_WFS_WOULDBLOCK if it
243 * needs to block.
244 */
245extern struct cds_wfs_node *cds_wfs_next_nonblocking(struct cds_wfs_node *node);
246
edac6b69
MD
247/*
248 * cds_wfs_pop_lock: lock stack pop-protection mutex.
249 */
250extern void cds_wfs_pop_lock(struct cds_wfs_stack *s);
251
252/*
253 * cds_wfs_pop_unlock: unlock stack pop-protection mutex.
254 */
255extern void cds_wfs_pop_unlock(struct cds_wfs_stack *s);
256
257/*
258 * __cds_wfs_pop_blocking: pop a node from the stack.
259 *
260 * Returns NULL if stack is empty.
261 *
262 * __cds_wfs_pop_blocking needs to be synchronized using one of the
263 * following techniques:
264 *
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).
272 */
711ff0f9 273extern struct cds_wfs_node *__cds_wfs_pop_blocking(cds_wfs_stack_ptr_t u_stack);
edac6b69 274
c8975b94
MD
275/*
276 * __cds_wfs_pop_with_state_blocking: pop a node from the stack, with state.
277 *
278 * Same as __cds_wfs_pop_blocking, but stores whether the stack was
279 * empty into state (CDS_WFS_STATE_LAST).
280 */
281extern struct cds_wfs_node *
711ff0f9
MD
282 __cds_wfs_pop_with_state_blocking(cds_wfs_stack_ptr_t u_stack,
283 int *state);
c8975b94 284
af67624d
MD
285/*
286 * __cds_wfs_pop_nonblocking: pop a node from the stack.
287 *
288 * Same as __cds_wfs_pop_blocking, but returns CDS_WFS_WOULDBLOCK if
289 * it needs to block.
290 */
711ff0f9 291extern struct cds_wfs_node *__cds_wfs_pop_nonblocking(cds_wfs_stack_ptr_t u_stack);
af67624d 292
c8975b94
MD
293/*
294 * __cds_wfs_pop_with_state_nonblocking: pop a node from the stack, with state.
295 *
296 * Same as __cds_wfs_pop_nonblocking, but stores whether the stack was
297 * empty into state (CDS_WFS_STATE_LAST).
298 */
299extern struct cds_wfs_node *
711ff0f9 300 __cds_wfs_pop_with_state_nonblocking(cds_wfs_stack_ptr_t u_stack,
c8975b94
MD
301 int *state);
302
edac6b69
MD
303/*
304 * __cds_wfs_pop_all: pop all nodes from a stack.
305 *
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:
309 *
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).
319 */
711ff0f9 320extern struct cds_wfs_head *__cds_wfs_pop_all(cds_wfs_stack_ptr_t u_stack);
edac6b69 321
294d3396 322#endif /* !_LGPL_SOURCE */
cb3f3d6b 323
edac6b69
MD
324/*
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).
329 *
330 * Content written into each node before enqueue is guaranteed to be
331 * consistent, but no other memory ordering is ensured.
332 */
333#define cds_wfs_for_each_blocking(head, node) \
c7ba06ba 334 for (node = cds_wfs_first(head); \
edac6b69
MD
335 node != NULL; \
336 node = cds_wfs_next_blocking(node))
337
338/*
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
344 * internally).
345 *
346 * Content written into each node before enqueue is guaranteed to be
347 * consistent, but no other memory ordering is ensured.
348 */
349#define cds_wfs_for_each_blocking_safe(head, node, n) \
c7ba06ba 350 for (node = cds_wfs_first(head), \
edac6b69
MD
351 n = (node ? cds_wfs_next_blocking(node) : NULL); \
352 node != NULL; \
353 node = n, n = (node ? cds_wfs_next_blocking(node) : NULL))
354
3ad920b4
MD
355#ifdef __cplusplus
356}
357
358/*
359 * In C++, implement static inline wrappers using function overloading
360 * to obtain an API similar to C.
361 */
362
94d0ecca 363static inline cds_wfs_stack_ptr_t cds_wfs_stack_cast(struct __cds_wfs_stack *s)
3ad920b4
MD
364{
365 cds_wfs_stack_ptr_t ret = {
366 ._s = s,
367 };
368 return ret;
369}
370
371static inline cds_wfs_stack_ptr_t cds_wfs_stack_cast(struct cds_wfs_stack *s)
372{
373 cds_wfs_stack_ptr_t ret = {
374 .s = s,
375 };
376 return ret;
377}
378
379static inline bool cds_wfs_empty(struct __cds_wfs_stack *s)
380{
94d0ecca 381 return cds_wfs_empty(cds_wfs_stack_cast(s));
3ad920b4
MD
382}
383
384static inline bool cds_wfs_empty(struct cds_wfs_stack *s)
385{
386 return cds_wfs_empty(cds_wfs_stack_cast(s));
387}
388
389static inline int cds_wfs_push(struct __cds_wfs_stack *s, struct cds_wfs_node *node)
390{
94d0ecca 391 return cds_wfs_push(cds_wfs_stack_cast(s), node);
3ad920b4
MD
392}
393
394static inline int cds_wfs_push(struct cds_wfs_stack *s, struct cds_wfs_node *node)
395{
396 return cds_wfs_push(cds_wfs_stack_cast(s), node);
397}
398
399static inline struct cds_wfs_node *__cds_wfs_pop_blocking(struct __cds_wfs_stack *s)
400{
94d0ecca 401 return __cds_wfs_pop_blocking(cds_wfs_stack_cast(s));
3ad920b4
MD
402}
403
404static inline struct cds_wfs_node *__cds_wfs_pop_blocking(struct cds_wfs_stack *s)
405{
406 return __cds_wfs_pop_blocking(cds_wfs_stack_cast(s));
407}
408
409static inline struct cds_wfs_node *
410 __cds_wfs_pop_with_state_blocking(struct __cds_wfs_stack *s, int *state)
411{
94d0ecca 412 return __cds_wfs_pop_with_state_blocking(cds_wfs_stack_cast(s), state);
3ad920b4
MD
413}
414
415static inline struct cds_wfs_node *
416 __cds_wfs_pop_with_state_blocking(struct cds_wfs_stack *s, int *state)
417{
418 return __cds_wfs_pop_with_state_blocking(cds_wfs_stack_cast(s), state);
419}
420
421static inline struct cds_wfs_node *__cds_wfs_pop_nonblocking(struct __cds_wfs_stack *s)
422
423{
94d0ecca 424 return __cds_wfs_pop_nonblocking(cds_wfs_stack_cast(s));
3ad920b4
MD
425}
426
427static inline struct cds_wfs_node *__cds_wfs_pop_nonblocking(struct cds_wfs_stack *s)
428{
429 return __cds_wfs_pop_nonblocking(cds_wfs_stack_cast(s));
430}
431
432static inline struct cds_wfs_node *
433 __cds_wfs_pop_with_state_nonblocking(struct __cds_wfs_stack *s, int *state)
434{
94d0ecca 435 return __cds_wfs_pop_with_state_nonblocking(cds_wfs_stack_cast(s), state);
3ad920b4
MD
436}
437
438static inline struct cds_wfs_node *
439 __cds_wfs_pop_with_state_nonblocking(struct cds_wfs_stack *s, int *state)
440{
441 return __cds_wfs_pop_with_state_nonblocking(cds_wfs_stack_cast(s), state);
442}
443
444static inline struct cds_wfs_head *__cds_wfs_pop_all(struct __cds_wfs_stack *s)
445{
94d0ecca 446 return __cds_wfs_pop_all(cds_wfs_stack_cast(s));
3ad920b4
MD
447}
448
449static inline struct cds_wfs_head *__cds_wfs_pop_all(struct cds_wfs_stack *s)
450{
451 return __cds_wfs_pop_all(cds_wfs_stack_cast(s));
452}
453
454#endif
455
cb3f3d6b 456#endif /* _URCU_WFSTACK_H */
This page took 0.060629 seconds and 4 git commands to generate.