Add C++ build tests
[urcu.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
EW
94/*
95 * 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
97 * types.
28757437
SM
98 *
99 * Avoid complaints from clang++ not knowing this attribute.
718eb63e 100 */
5135c0fd 101typedef union {
718eb63e
EW
102 struct __cds_wfs_stack *_s;
103 struct cds_wfs_stack *s;
28757437 104} caa_c_transparent_union cds_wfs_stack_ptr_t;
718eb63e 105
294d3396 106#ifdef _LGPL_SOURCE
cb3f3d6b 107
af7c2dbe 108#include <urcu/static/wfstack.h>
cb3f3d6b 109
16aa9ee8 110#define cds_wfs_node_init _cds_wfs_node_init
edac6b69 111#define cds_wfs_init _cds_wfs_init
200d100e 112#define cds_wfs_destroy _cds_wfs_destroy
711ff0f9 113#define __cds_wfs_init ___cds_wfs_init
edac6b69
MD
114#define cds_wfs_empty _cds_wfs_empty
115#define cds_wfs_push _cds_wfs_push
116
117/* Locking performed internally */
118#define cds_wfs_pop_blocking _cds_wfs_pop_blocking
c8975b94 119#define cds_wfs_pop_with_state_blocking _cds_wfs_pop_with_state_blocking
edac6b69
MD
120#define cds_wfs_pop_all_blocking _cds_wfs_pop_all_blocking
121
122/*
123 * For iteration on cds_wfs_head returned by __cds_wfs_pop_all or
124 * cds_wfs_pop_all_blocking.
125 */
c7ba06ba 126#define cds_wfs_first _cds_wfs_first
edac6b69 127#define cds_wfs_next_blocking _cds_wfs_next_blocking
150fc1bb 128#define cds_wfs_next_nonblocking _cds_wfs_next_nonblocking
edac6b69
MD
129
130/* Pop locking with internal mutex */
131#define cds_wfs_pop_lock _cds_wfs_pop_lock
132#define cds_wfs_pop_unlock _cds_wfs_pop_unlock
133
134/* Synchronization ensured by the caller. See synchronization table. */
135#define __cds_wfs_pop_blocking ___cds_wfs_pop_blocking
c8975b94
MD
136#define __cds_wfs_pop_with_state_blocking \
137 ___cds_wfs_pop_with_state_blocking
150fc1bb 138#define __cds_wfs_pop_nonblocking ___cds_wfs_pop_nonblocking
c8975b94
MD
139#define __cds_wfs_pop_with_state_nonblocking \
140 ___cds_wfs_pop_with_state_nonblocking
edac6b69 141#define __cds_wfs_pop_all ___cds_wfs_pop_all
cb3f3d6b 142
294d3396 143#else /* !_LGPL_SOURCE */
cb3f3d6b 144
edac6b69
MD
145/*
146 * cds_wfs_node_init: initialize wait-free stack node.
147 */
16aa9ee8 148extern void cds_wfs_node_init(struct cds_wfs_node *node);
edac6b69
MD
149
150/*
200d100e
MD
151 * cds_wfs_init: initialize wait-free stack (with lock). Pair with
152 * cds_wfs_destroy().
edac6b69 153 */
16aa9ee8 154extern void cds_wfs_init(struct cds_wfs_stack *s);
edac6b69 155
718eb63e 156/*
200d100e
MD
157 * cds_wfs_destroy: destroy wait-free stack (with lock). Pair with
158 * cds_wfs_init().
159 */
160extern void cds_wfs_destroy(struct cds_wfs_stack *s);
161
162/*
163 * __cds_wfs_init: initialize wait-free stack (no lock). Don't pair with
164 * any destroy function.
718eb63e
EW
165 */
166extern void __cds_wfs_init(struct __cds_wfs_stack *s);
167
edac6b69
MD
168/*
169 * cds_wfs_empty: return whether wait-free stack is empty.
170 *
171 * No memory barrier is issued. No mutual exclusion is required.
172 */
718eb63e 173extern bool cds_wfs_empty(cds_wfs_stack_ptr_t u_stack);
edac6b69
MD
174
175/*
176 * cds_wfs_push: push a node into the stack.
177 *
178 * Issues a full memory barrier before push. No mutual exclusion is
179 * required.
180 *
181 * Returns 0 if the stack was empty prior to adding the node.
182 * Returns non-zero otherwise.
183 */
718eb63e 184extern int cds_wfs_push(cds_wfs_stack_ptr_t u_stack, struct cds_wfs_node *node);
edac6b69
MD
185
186/*
187 * cds_wfs_pop_blocking: pop a node from the stack.
188 *
189 * Calls __cds_wfs_pop_blocking with an internal pop mutex held.
190 */
16aa9ee8 191extern struct cds_wfs_node *cds_wfs_pop_blocking(struct cds_wfs_stack *s);
cb3f3d6b 192
c8975b94
MD
193/*
194 * cds_wfs_pop_with_state_blocking: pop a node from the stack, with state.
195 *
196 * Same as cds_wfs_pop_blocking, but stores whether the stack was
197 * empty into state (CDS_WFS_STATE_LAST).
198 */
199extern struct cds_wfs_node *
200 cds_wfs_pop_with_state_blocking(struct cds_wfs_stack *s, int *state);
201
edac6b69
MD
202/*
203 * cds_wfs_pop_all_blocking: pop all nodes from a stack.
204 *
205 * Calls __cds_wfs_pop_all with an internal pop mutex held.
206 */
207extern struct cds_wfs_head *cds_wfs_pop_all_blocking(struct cds_wfs_stack *s);
208
209/*
c7ba06ba 210 * cds_wfs_first: get first node of a popped stack.
edac6b69
MD
211 *
212 * Content written into the node before enqueue is guaranteed to be
213 * consistent, but no other memory ordering is ensured.
214 *
215 * Used by for-like iteration macros in urcu/wfstack.h:
216 * cds_wfs_for_each_blocking()
217 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
218 *
219 * Returns NULL if popped stack is empty, top stack node otherwise.
edac6b69 220 */
c7ba06ba 221extern struct cds_wfs_node *cds_wfs_first(struct cds_wfs_head *head);
edac6b69
MD
222
223/*
224 * cds_wfs_next_blocking: get next node of a popped stack.
225 *
226 * Content written into the node before enqueue is guaranteed to be
227 * consistent, but no other memory ordering is ensured.
228 *
229 * Used by for-like iteration macros in urcu/wfstack.h:
230 * cds_wfs_for_each_blocking()
231 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
232 *
233 * Returns NULL if reached end of popped stack, non-NULL next stack
234 * node otherwise.
edac6b69
MD
235 */
236extern struct cds_wfs_node *cds_wfs_next_blocking(struct cds_wfs_node *node);
237
af67624d
MD
238/*
239 * cds_wfs_next_nonblocking: get next node of a popped stack.
240 *
241 * Same as cds_wfs_next_blocking, but returns CDS_WFS_WOULDBLOCK if it
242 * needs to block.
243 */
244extern struct cds_wfs_node *cds_wfs_next_nonblocking(struct cds_wfs_node *node);
245
edac6b69
MD
246/*
247 * cds_wfs_pop_lock: lock stack pop-protection mutex.
248 */
249extern void cds_wfs_pop_lock(struct cds_wfs_stack *s);
250
251/*
252 * cds_wfs_pop_unlock: unlock stack pop-protection mutex.
253 */
254extern void cds_wfs_pop_unlock(struct cds_wfs_stack *s);
255
256/*
257 * __cds_wfs_pop_blocking: pop a node from the stack.
258 *
259 * Returns NULL if stack is empty.
260 *
261 * __cds_wfs_pop_blocking needs to be synchronized using one of the
262 * following techniques:
263 *
264 * 1) Calling __cds_wfs_pop_blocking under rcu read lock critical
265 * section. The caller must wait for a grace period to pass before
266 * freeing the returned node or modifying the cds_wfs_node structure.
267 * 2) Using mutual exclusion (e.g. mutexes) to protect
268 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
269 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
270 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
271 */
711ff0f9 272extern struct cds_wfs_node *__cds_wfs_pop_blocking(cds_wfs_stack_ptr_t u_stack);
edac6b69 273
c8975b94
MD
274/*
275 * __cds_wfs_pop_with_state_blocking: pop a node from the stack, with state.
276 *
277 * Same as __cds_wfs_pop_blocking, but stores whether the stack was
278 * empty into state (CDS_WFS_STATE_LAST).
279 */
280extern struct cds_wfs_node *
711ff0f9
MD
281 __cds_wfs_pop_with_state_blocking(cds_wfs_stack_ptr_t u_stack,
282 int *state);
c8975b94 283
af67624d
MD
284/*
285 * __cds_wfs_pop_nonblocking: pop a node from the stack.
286 *
287 * Same as __cds_wfs_pop_blocking, but returns CDS_WFS_WOULDBLOCK if
288 * it needs to block.
289 */
711ff0f9 290extern struct cds_wfs_node *__cds_wfs_pop_nonblocking(cds_wfs_stack_ptr_t u_stack);
af67624d 291
c8975b94
MD
292/*
293 * __cds_wfs_pop_with_state_nonblocking: pop a node from the stack, with state.
294 *
295 * Same as __cds_wfs_pop_nonblocking, but stores whether the stack was
296 * empty into state (CDS_WFS_STATE_LAST).
297 */
298extern struct cds_wfs_node *
711ff0f9 299 __cds_wfs_pop_with_state_nonblocking(cds_wfs_stack_ptr_t u_stack,
c8975b94
MD
300 int *state);
301
edac6b69
MD
302/*
303 * __cds_wfs_pop_all: pop all nodes from a stack.
304 *
305 * __cds_wfs_pop_all does not require any synchronization with other
306 * push, nor with other __cds_wfs_pop_all, but requires synchronization
307 * matching the technique used to synchronize __cds_wfs_pop_blocking:
308 *
309 * 1) If __cds_wfs_pop_blocking is called under rcu read lock critical
310 * section, both __cds_wfs_pop_blocking and cds_wfs_pop_all callers
311 * must wait for a grace period to pass before freeing the returned
312 * node or modifying the cds_wfs_node structure. However, no RCU
313 * read-side critical section is needed around __cds_wfs_pop_all.
314 * 2) Using mutual exclusion (e.g. mutexes) to protect
315 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
316 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
317 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
318 */
711ff0f9 319extern struct cds_wfs_head *__cds_wfs_pop_all(cds_wfs_stack_ptr_t u_stack);
edac6b69 320
294d3396 321#endif /* !_LGPL_SOURCE */
cb3f3d6b
MD
322
323#ifdef __cplusplus
324}
325#endif
326
edac6b69
MD
327/*
328 * cds_wfs_for_each_blocking: Iterate over all nodes returned by
329 * __cds_wfs_pop_all().
330 * @head: head of the queue (struct cds_wfs_head pointer).
331 * @node: iterator (struct cds_wfs_node pointer).
332 *
333 * Content written into each node before enqueue is guaranteed to be
334 * consistent, but no other memory ordering is ensured.
335 */
336#define cds_wfs_for_each_blocking(head, node) \
c7ba06ba 337 for (node = cds_wfs_first(head); \
edac6b69
MD
338 node != NULL; \
339 node = cds_wfs_next_blocking(node))
340
341/*
342 * cds_wfs_for_each_blocking_safe: Iterate over all nodes returned by
343 * __cds_wfs_pop_all(). Safe against deletion.
344 * @head: head of the queue (struct cds_wfs_head pointer).
345 * @node: iterator (struct cds_wfs_node pointer).
346 * @n: struct cds_wfs_node pointer holding the next pointer (used
347 * internally).
348 *
349 * Content written into each node before enqueue is guaranteed to be
350 * consistent, but no other memory ordering is ensured.
351 */
352#define cds_wfs_for_each_blocking_safe(head, node, n) \
c7ba06ba 353 for (node = cds_wfs_first(head), \
edac6b69
MD
354 n = (node ? cds_wfs_next_blocking(node) : NULL); \
355 node != NULL; \
356 node = n, n = (node ? cds_wfs_next_blocking(node) : NULL))
357
cb3f3d6b 358#endif /* _URCU_WFSTACK_H */
This page took 0.057043 seconds and 4 git commands to generate.