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