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