wfcqueue tests: use dequeue empty state
[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
edac6b69
MD
63/*
64 * struct cds_wfs_node is returned by __cds_wfs_pop, and also used as
65 * iterator on stack. It is not safe to dereference the node next
66 * pointer when returned by __cds_wfs_pop_blocking.
67 */
16aa9ee8
DG
68struct cds_wfs_node {
69 struct cds_wfs_node *next;
cb3f3d6b
MD
70};
71
edac6b69
MD
72/*
73 * struct cds_wfs_head is returned by __cds_wfs_pop_all, and can be used
74 * to begin iteration on the stack. "node" needs to be the first field of
75 * cds_wfs_head, so the end-of-stack pointer value can be used for both
76 * types.
77 */
78struct cds_wfs_head {
79 struct cds_wfs_node node;
80};
81
16aa9ee8 82struct cds_wfs_stack {
edac6b69 83 struct cds_wfs_head *head;
cb3f3d6b
MD
84 pthread_mutex_t lock;
85};
86
294d3396 87#ifdef _LGPL_SOURCE
cb3f3d6b 88
af7c2dbe 89#include <urcu/static/wfstack.h>
cb3f3d6b 90
16aa9ee8 91#define cds_wfs_node_init _cds_wfs_node_init
edac6b69
MD
92#define cds_wfs_init _cds_wfs_init
93#define cds_wfs_empty _cds_wfs_empty
94#define cds_wfs_push _cds_wfs_push
95
96/* Locking performed internally */
97#define cds_wfs_pop_blocking _cds_wfs_pop_blocking
98#define cds_wfs_pop_all_blocking _cds_wfs_pop_all_blocking
99
100/*
101 * For iteration on cds_wfs_head returned by __cds_wfs_pop_all or
102 * cds_wfs_pop_all_blocking.
103 */
c7ba06ba 104#define cds_wfs_first _cds_wfs_first
edac6b69 105#define cds_wfs_next_blocking _cds_wfs_next_blocking
150fc1bb 106#define cds_wfs_next_nonblocking _cds_wfs_next_nonblocking
edac6b69
MD
107
108/* Pop locking with internal mutex */
109#define cds_wfs_pop_lock _cds_wfs_pop_lock
110#define cds_wfs_pop_unlock _cds_wfs_pop_unlock
111
112/* Synchronization ensured by the caller. See synchronization table. */
113#define __cds_wfs_pop_blocking ___cds_wfs_pop_blocking
150fc1bb 114#define __cds_wfs_pop_nonblocking ___cds_wfs_pop_nonblocking
edac6b69 115#define __cds_wfs_pop_all ___cds_wfs_pop_all
cb3f3d6b 116
294d3396 117#else /* !_LGPL_SOURCE */
cb3f3d6b 118
edac6b69
MD
119/*
120 * cds_wfs_node_init: initialize wait-free stack node.
121 */
16aa9ee8 122extern void cds_wfs_node_init(struct cds_wfs_node *node);
edac6b69
MD
123
124/*
125 * cds_wfs_init: initialize wait-free stack.
126 */
16aa9ee8 127extern void cds_wfs_init(struct cds_wfs_stack *s);
edac6b69
MD
128
129/*
130 * cds_wfs_empty: return whether wait-free stack is empty.
131 *
132 * No memory barrier is issued. No mutual exclusion is required.
133 */
134extern bool cds_wfs_empty(struct cds_wfs_stack *s);
135
136/*
137 * cds_wfs_push: push a node into the stack.
138 *
139 * Issues a full memory barrier before push. No mutual exclusion is
140 * required.
141 *
142 * Returns 0 if the stack was empty prior to adding the node.
143 * Returns non-zero otherwise.
144 */
191098fc 145extern int cds_wfs_push(struct cds_wfs_stack *s, struct cds_wfs_node *node);
edac6b69
MD
146
147/*
148 * cds_wfs_pop_blocking: pop a node from the stack.
149 *
150 * Calls __cds_wfs_pop_blocking with an internal pop mutex held.
151 */
16aa9ee8 152extern struct cds_wfs_node *cds_wfs_pop_blocking(struct cds_wfs_stack *s);
cb3f3d6b 153
edac6b69
MD
154/*
155 * cds_wfs_pop_all_blocking: pop all nodes from a stack.
156 *
157 * Calls __cds_wfs_pop_all with an internal pop mutex held.
158 */
159extern struct cds_wfs_head *cds_wfs_pop_all_blocking(struct cds_wfs_stack *s);
160
161/*
c7ba06ba 162 * cds_wfs_first: get first node of a popped stack.
edac6b69
MD
163 *
164 * Content written into the node before enqueue is guaranteed to be
165 * consistent, but no other memory ordering is ensured.
166 *
167 * Used by for-like iteration macros in urcu/wfstack.h:
168 * cds_wfs_for_each_blocking()
169 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
170 *
171 * Returns NULL if popped stack is empty, top stack node otherwise.
edac6b69 172 */
c7ba06ba 173extern struct cds_wfs_node *cds_wfs_first(struct cds_wfs_head *head);
edac6b69
MD
174
175/*
176 * cds_wfs_next_blocking: get next node of a popped stack.
177 *
178 * Content written into the node before enqueue is guaranteed to be
179 * consistent, but no other memory ordering is ensured.
180 *
181 * Used by for-like iteration macros in urcu/wfstack.h:
182 * cds_wfs_for_each_blocking()
183 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
184 *
185 * Returns NULL if reached end of popped stack, non-NULL next stack
186 * node otherwise.
edac6b69
MD
187 */
188extern struct cds_wfs_node *cds_wfs_next_blocking(struct cds_wfs_node *node);
189
af67624d
MD
190/*
191 * cds_wfs_next_nonblocking: get next node of a popped stack.
192 *
193 * Same as cds_wfs_next_blocking, but returns CDS_WFS_WOULDBLOCK if it
194 * needs to block.
195 */
196extern struct cds_wfs_node *cds_wfs_next_nonblocking(struct cds_wfs_node *node);
197
edac6b69
MD
198/*
199 * cds_wfs_pop_lock: lock stack pop-protection mutex.
200 */
201extern void cds_wfs_pop_lock(struct cds_wfs_stack *s);
202
203/*
204 * cds_wfs_pop_unlock: unlock stack pop-protection mutex.
205 */
206extern void cds_wfs_pop_unlock(struct cds_wfs_stack *s);
207
208/*
209 * __cds_wfs_pop_blocking: pop a node from the stack.
210 *
211 * Returns NULL if stack is empty.
212 *
213 * __cds_wfs_pop_blocking needs to be synchronized using one of the
214 * following techniques:
215 *
216 * 1) Calling __cds_wfs_pop_blocking under rcu read lock critical
217 * section. The caller must wait for a grace period to pass before
218 * freeing the returned node or modifying the cds_wfs_node structure.
219 * 2) Using mutual exclusion (e.g. mutexes) to protect
220 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
221 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
222 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
223 */
224extern struct cds_wfs_node *__cds_wfs_pop_blocking(struct cds_wfs_stack *s);
225
af67624d
MD
226/*
227 * __cds_wfs_pop_nonblocking: pop a node from the stack.
228 *
229 * Same as __cds_wfs_pop_blocking, but returns CDS_WFS_WOULDBLOCK if
230 * it needs to block.
231 */
232extern struct cds_wfs_node *__cds_wfs_pop_nonblocking(struct cds_wfs_stack *s);
233
edac6b69
MD
234/*
235 * __cds_wfs_pop_all: pop all nodes from a stack.
236 *
237 * __cds_wfs_pop_all does not require any synchronization with other
238 * push, nor with other __cds_wfs_pop_all, but requires synchronization
239 * matching the technique used to synchronize __cds_wfs_pop_blocking:
240 *
241 * 1) If __cds_wfs_pop_blocking is called under rcu read lock critical
242 * section, both __cds_wfs_pop_blocking and cds_wfs_pop_all callers
243 * must wait for a grace period to pass before freeing the returned
244 * node or modifying the cds_wfs_node structure. However, no RCU
245 * read-side critical section is needed around __cds_wfs_pop_all.
246 * 2) Using mutual exclusion (e.g. mutexes) to protect
247 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
248 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
249 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
250 */
251extern struct cds_wfs_head *__cds_wfs_pop_all(struct cds_wfs_stack *s);
252
294d3396 253#endif /* !_LGPL_SOURCE */
cb3f3d6b
MD
254
255#ifdef __cplusplus
256}
257#endif
258
edac6b69
MD
259/*
260 * cds_wfs_for_each_blocking: Iterate over all nodes returned by
261 * __cds_wfs_pop_all().
262 * @head: head of the queue (struct cds_wfs_head pointer).
263 * @node: iterator (struct cds_wfs_node pointer).
264 *
265 * Content written into each node before enqueue is guaranteed to be
266 * consistent, but no other memory ordering is ensured.
267 */
268#define cds_wfs_for_each_blocking(head, node) \
c7ba06ba 269 for (node = cds_wfs_first(head); \
edac6b69
MD
270 node != NULL; \
271 node = cds_wfs_next_blocking(node))
272
273/*
274 * cds_wfs_for_each_blocking_safe: Iterate over all nodes returned by
275 * __cds_wfs_pop_all(). Safe against deletion.
276 * @head: head of the queue (struct cds_wfs_head pointer).
277 * @node: iterator (struct cds_wfs_node pointer).
278 * @n: struct cds_wfs_node pointer holding the next pointer (used
279 * internally).
280 *
281 * Content written into each node before enqueue is guaranteed to be
282 * consistent, but no other memory ordering is ensured.
283 */
284#define cds_wfs_for_each_blocking_safe(head, node, n) \
c7ba06ba 285 for (node = cds_wfs_first(head), \
edac6b69
MD
286 n = (node ? cds_wfs_next_blocking(node) : NULL); \
287 node != NULL; \
288 node = n, n = (node ? cds_wfs_next_blocking(node) : NULL))
289
cb3f3d6b 290#endif /* _URCU_WFSTACK_H */
This page took 0.036332 seconds and 4 git commands to generate.