Fix: preserve example files' timestamps when copying
[urcu.git] / 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>
27#include <assert.h>
edac6b69 28#include <stdbool.h>
cb3f3d6b
MD
29#include <urcu/compiler.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
edac6b69
MD
35/*
36 * Stack with wait-free push, blocking traversal.
37 *
38 * Stack implementing push, pop, pop_all operations, as well as iterator
39 * on the stack head returned by pop_all.
40 *
c97c6ce5
MD
41 * Wait-free operations: cds_wfs_push, __cds_wfs_pop_all, cds_wfs_empty,
42 * cds_wfs_first.
43 * Blocking operations: cds_wfs_pop, cds_wfs_pop_all, cds_wfs_next,
44 * iteration on stack head returned by pop_all.
edac6b69
MD
45 *
46 * Synchronization table:
47 *
48 * External synchronization techniques described in the API below is
49 * required between pairs marked with "X". No external synchronization
50 * required between pairs marked with "-".
51 *
52 * cds_wfs_push __cds_wfs_pop __cds_wfs_pop_all
53 * cds_wfs_push - - -
54 * __cds_wfs_pop - X X
55 * __cds_wfs_pop_all - X -
56 *
57 * cds_wfs_pop and cds_wfs_pop_all use an internal mutex to provide
58 * synchronization.
59 */
60
af67624d
MD
61#define CDS_WFS_WOULDBLOCK ((void *) -1UL)
62
c8975b94
MD
63enum cds_wfs_state {
64 CDS_WFS_STATE_LAST = (1U << 0),
65};
66
edac6b69
MD
67/*
68 * struct cds_wfs_node is returned by __cds_wfs_pop, and also used as
69 * iterator on stack. It is not safe to dereference the node next
70 * pointer when returned by __cds_wfs_pop_blocking.
71 */
16aa9ee8
DG
72struct cds_wfs_node {
73 struct cds_wfs_node *next;
cb3f3d6b
MD
74};
75
edac6b69
MD
76/*
77 * struct cds_wfs_head is returned by __cds_wfs_pop_all, and can be used
78 * to begin iteration on the stack. "node" needs to be the first field of
79 * cds_wfs_head, so the end-of-stack pointer value can be used for both
80 * types.
81 */
82struct cds_wfs_head {
83 struct cds_wfs_node node;
84};
85
718eb63e
EW
86struct __cds_wfs_stack {
87 struct cds_wfs_head *head;
88};
89
16aa9ee8 90struct cds_wfs_stack {
edac6b69 91 struct cds_wfs_head *head;
cb3f3d6b
MD
92 pthread_mutex_t lock;
93};
94
718eb63e
EW
95/*
96 * The transparent union allows calling functions that work on both
97 * struct cds_wfs_stack and struct __cds_wfs_stack on any of those two
98 * types.
99 */
100typedef union __attribute__((__transparent_union__)) {
101 struct __cds_wfs_stack *_s;
102 struct cds_wfs_stack *s;
103} cds_wfs_stack_ptr_t;
104
294d3396 105#ifdef _LGPL_SOURCE
cb3f3d6b 106
af7c2dbe 107#include <urcu/static/wfstack.h>
cb3f3d6b 108
16aa9ee8 109#define cds_wfs_node_init _cds_wfs_node_init
edac6b69
MD
110#define cds_wfs_init _cds_wfs_init
111#define cds_wfs_empty _cds_wfs_empty
112#define cds_wfs_push _cds_wfs_push
113
114/* Locking performed internally */
115#define cds_wfs_pop_blocking _cds_wfs_pop_blocking
c8975b94 116#define cds_wfs_pop_with_state_blocking _cds_wfs_pop_with_state_blocking
edac6b69
MD
117#define cds_wfs_pop_all_blocking _cds_wfs_pop_all_blocking
118
119/*
120 * For iteration on cds_wfs_head returned by __cds_wfs_pop_all or
121 * cds_wfs_pop_all_blocking.
122 */
c7ba06ba 123#define cds_wfs_first _cds_wfs_first
edac6b69 124#define cds_wfs_next_blocking _cds_wfs_next_blocking
150fc1bb 125#define cds_wfs_next_nonblocking _cds_wfs_next_nonblocking
edac6b69
MD
126
127/* Pop locking with internal mutex */
128#define cds_wfs_pop_lock _cds_wfs_pop_lock
129#define cds_wfs_pop_unlock _cds_wfs_pop_unlock
130
131/* Synchronization ensured by the caller. See synchronization table. */
132#define __cds_wfs_pop_blocking ___cds_wfs_pop_blocking
c8975b94
MD
133#define __cds_wfs_pop_with_state_blocking \
134 ___cds_wfs_pop_with_state_blocking
150fc1bb 135#define __cds_wfs_pop_nonblocking ___cds_wfs_pop_nonblocking
c8975b94
MD
136#define __cds_wfs_pop_with_state_nonblocking \
137 ___cds_wfs_pop_with_state_nonblocking
edac6b69 138#define __cds_wfs_pop_all ___cds_wfs_pop_all
cb3f3d6b 139
294d3396 140#else /* !_LGPL_SOURCE */
cb3f3d6b 141
edac6b69
MD
142/*
143 * cds_wfs_node_init: initialize wait-free stack node.
144 */
16aa9ee8 145extern void cds_wfs_node_init(struct cds_wfs_node *node);
edac6b69
MD
146
147/*
148 * cds_wfs_init: initialize wait-free stack.
149 */
16aa9ee8 150extern void cds_wfs_init(struct cds_wfs_stack *s);
edac6b69 151
718eb63e
EW
152/*
153 * __cds_wfs_init: initialize wait-free stack.
154 */
155extern void __cds_wfs_init(struct __cds_wfs_stack *s);
156
edac6b69
MD
157/*
158 * cds_wfs_empty: return whether wait-free stack is empty.
159 *
160 * No memory barrier is issued. No mutual exclusion is required.
161 */
718eb63e 162extern bool cds_wfs_empty(cds_wfs_stack_ptr_t u_stack);
edac6b69
MD
163
164/*
165 * cds_wfs_push: push a node into the stack.
166 *
167 * Issues a full memory barrier before push. No mutual exclusion is
168 * required.
169 *
170 * Returns 0 if the stack was empty prior to adding the node.
171 * Returns non-zero otherwise.
172 */
718eb63e 173extern int cds_wfs_push(cds_wfs_stack_ptr_t u_stack, struct cds_wfs_node *node);
edac6b69
MD
174
175/*
176 * cds_wfs_pop_blocking: pop a node from the stack.
177 *
178 * Calls __cds_wfs_pop_blocking with an internal pop mutex held.
179 */
16aa9ee8 180extern struct cds_wfs_node *cds_wfs_pop_blocking(struct cds_wfs_stack *s);
cb3f3d6b 181
c8975b94
MD
182/*
183 * cds_wfs_pop_with_state_blocking: pop a node from the stack, with state.
184 *
185 * Same as cds_wfs_pop_blocking, but stores whether the stack was
186 * empty into state (CDS_WFS_STATE_LAST).
187 */
188extern struct cds_wfs_node *
189 cds_wfs_pop_with_state_blocking(struct cds_wfs_stack *s, int *state);
190
edac6b69
MD
191/*
192 * cds_wfs_pop_all_blocking: pop all nodes from a stack.
193 *
194 * Calls __cds_wfs_pop_all with an internal pop mutex held.
195 */
196extern struct cds_wfs_head *cds_wfs_pop_all_blocking(struct cds_wfs_stack *s);
197
198/*
c7ba06ba 199 * cds_wfs_first: get first node of a popped stack.
edac6b69
MD
200 *
201 * Content written into the node before enqueue is guaranteed to be
202 * consistent, but no other memory ordering is ensured.
203 *
204 * Used by for-like iteration macros in urcu/wfstack.h:
205 * cds_wfs_for_each_blocking()
206 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
207 *
208 * Returns NULL if popped stack is empty, top stack node otherwise.
edac6b69 209 */
c7ba06ba 210extern struct cds_wfs_node *cds_wfs_first(struct cds_wfs_head *head);
edac6b69
MD
211
212/*
213 * cds_wfs_next_blocking: get next node of a popped stack.
214 *
215 * Content written into the node before enqueue is guaranteed to be
216 * consistent, but no other memory ordering is ensured.
217 *
218 * Used by for-like iteration macros in urcu/wfstack.h:
219 * cds_wfs_for_each_blocking()
220 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
221 *
222 * Returns NULL if reached end of popped stack, non-NULL next stack
223 * node otherwise.
edac6b69
MD
224 */
225extern struct cds_wfs_node *cds_wfs_next_blocking(struct cds_wfs_node *node);
226
af67624d
MD
227/*
228 * cds_wfs_next_nonblocking: get next node of a popped stack.
229 *
230 * Same as cds_wfs_next_blocking, but returns CDS_WFS_WOULDBLOCK if it
231 * needs to block.
232 */
233extern struct cds_wfs_node *cds_wfs_next_nonblocking(struct cds_wfs_node *node);
234
edac6b69
MD
235/*
236 * cds_wfs_pop_lock: lock stack pop-protection mutex.
237 */
238extern void cds_wfs_pop_lock(struct cds_wfs_stack *s);
239
240/*
241 * cds_wfs_pop_unlock: unlock stack pop-protection mutex.
242 */
243extern void cds_wfs_pop_unlock(struct cds_wfs_stack *s);
244
245/*
246 * __cds_wfs_pop_blocking: pop a node from the stack.
247 *
248 * Returns NULL if stack is empty.
249 *
250 * __cds_wfs_pop_blocking needs to be synchronized using one of the
251 * following techniques:
252 *
253 * 1) Calling __cds_wfs_pop_blocking under rcu read lock critical
254 * section. The caller must wait for a grace period to pass before
255 * freeing the returned node or modifying the cds_wfs_node structure.
256 * 2) Using mutual exclusion (e.g. mutexes) to protect
257 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
258 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
259 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
260 */
261extern struct cds_wfs_node *__cds_wfs_pop_blocking(struct cds_wfs_stack *s);
262
c8975b94
MD
263/*
264 * __cds_wfs_pop_with_state_blocking: pop a node from the stack, with state.
265 *
266 * Same as __cds_wfs_pop_blocking, but stores whether the stack was
267 * empty into state (CDS_WFS_STATE_LAST).
268 */
269extern struct cds_wfs_node *
270 __cds_wfs_pop_with_state_blocking(struct cds_wfs_stack *s, int *state);
271
af67624d
MD
272/*
273 * __cds_wfs_pop_nonblocking: pop a node from the stack.
274 *
275 * Same as __cds_wfs_pop_blocking, but returns CDS_WFS_WOULDBLOCK if
276 * it needs to block.
277 */
278extern struct cds_wfs_node *__cds_wfs_pop_nonblocking(struct cds_wfs_stack *s);
279
c8975b94
MD
280/*
281 * __cds_wfs_pop_with_state_nonblocking: pop a node from the stack, with state.
282 *
283 * Same as __cds_wfs_pop_nonblocking, but stores whether the stack was
284 * empty into state (CDS_WFS_STATE_LAST).
285 */
286extern struct cds_wfs_node *
287 __cds_wfs_pop_with_state_nonblocking(struct cds_wfs_stack *s,
288 int *state);
289
edac6b69
MD
290/*
291 * __cds_wfs_pop_all: pop all nodes from a stack.
292 *
293 * __cds_wfs_pop_all does not require any synchronization with other
294 * push, nor with other __cds_wfs_pop_all, but requires synchronization
295 * matching the technique used to synchronize __cds_wfs_pop_blocking:
296 *
297 * 1) If __cds_wfs_pop_blocking is called under rcu read lock critical
298 * section, both __cds_wfs_pop_blocking and cds_wfs_pop_all callers
299 * must wait for a grace period to pass before freeing the returned
300 * node or modifying the cds_wfs_node structure. However, no RCU
301 * read-side critical section is needed around __cds_wfs_pop_all.
302 * 2) Using mutual exclusion (e.g. mutexes) to protect
303 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
304 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
305 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
306 */
307extern struct cds_wfs_head *__cds_wfs_pop_all(struct cds_wfs_stack *s);
308
294d3396 309#endif /* !_LGPL_SOURCE */
cb3f3d6b
MD
310
311#ifdef __cplusplus
312}
313#endif
314
edac6b69
MD
315/*
316 * cds_wfs_for_each_blocking: Iterate over all nodes returned by
317 * __cds_wfs_pop_all().
318 * @head: head of the queue (struct cds_wfs_head pointer).
319 * @node: iterator (struct cds_wfs_node pointer).
320 *
321 * Content written into each node before enqueue is guaranteed to be
322 * consistent, but no other memory ordering is ensured.
323 */
324#define cds_wfs_for_each_blocking(head, node) \
c7ba06ba 325 for (node = cds_wfs_first(head); \
edac6b69
MD
326 node != NULL; \
327 node = cds_wfs_next_blocking(node))
328
329/*
330 * cds_wfs_for_each_blocking_safe: Iterate over all nodes returned by
331 * __cds_wfs_pop_all(). Safe against deletion.
332 * @head: head of the queue (struct cds_wfs_head pointer).
333 * @node: iterator (struct cds_wfs_node pointer).
334 * @n: struct cds_wfs_node pointer holding the next pointer (used
335 * internally).
336 *
337 * Content written into each node before enqueue is guaranteed to be
338 * consistent, but no other memory ordering is ensured.
339 */
340#define cds_wfs_for_each_blocking_safe(head, node, n) \
c7ba06ba 341 for (node = cds_wfs_first(head), \
edac6b69
MD
342 n = (node ? cds_wfs_next_blocking(node) : NULL); \
343 node != NULL; \
344 node = n, n = (node ? cds_wfs_next_blocking(node) : NULL))
345
cb3f3d6b 346#endif /* _URCU_WFSTACK_H */
This page took 0.041382 seconds and 4 git commands to generate.