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