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