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