Public headers: use SPDX identifiers
[urcu.git] / include / urcu / static / wfstack.h
CommitLineData
d3d3857f
MJ
1// SPDX-FileCopyrightText: 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
edac6b69
MD
5#ifndef _URCU_STATIC_WFSTACK_H
6#define _URCU_STATIC_WFSTACK_H
294d3396
MD
7
8/*
edac6b69 9 * Userspace RCU library - Stack with with wait-free push, blocking traversal.
294d3396 10 *
07c2a4fd
MD
11 * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See urcu/wfstack.h for
12 * linking dynamically with the userspace rcu library.
294d3396
MD
13 */
14
15#include <pthread.h>
b57aee66 16#include <poll.h>
edac6b69 17#include <stdbool.h>
01477510 18#include <urcu/assert.h>
294d3396 19#include <urcu/compiler.h>
a2e7bf9c 20#include <urcu/uatomic.h>
294d3396
MD
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
28757437 26#define CDS_WFS_END ((struct cds_wfs_head *) 0x1UL)
16aa9ee8
DG
27#define CDS_WFS_ADAPT_ATTEMPTS 10 /* Retry if being set */
28#define CDS_WFS_WAIT 10 /* Wait 10 ms if being set */
294d3396 29
edac6b69
MD
30/*
31 * Stack with wait-free push, blocking traversal.
32 *
33 * Stack implementing push, pop, pop_all operations, as well as iterator
34 * on the stack head returned by pop_all.
35 *
c97c6ce5
MD
36 * Wait-free operations: cds_wfs_push, __cds_wfs_pop_all, cds_wfs_empty,
37 * cds_wfs_first.
38 * Blocking operations: cds_wfs_pop, cds_wfs_pop_all, cds_wfs_next,
39 * iteration on stack head returned by pop_all.
edac6b69
MD
40 *
41 * Synchronization table:
42 *
43 * External synchronization techniques described in the API below is
44 * required between pairs marked with "X". No external synchronization
45 * required between pairs marked with "-".
46 *
47 * cds_wfs_push __cds_wfs_pop __cds_wfs_pop_all
48 * cds_wfs_push - - -
49 * __cds_wfs_pop - X X
50 * __cds_wfs_pop_all - X -
51 *
52 * cds_wfs_pop and cds_wfs_pop_all use an internal mutex to provide
53 * synchronization.
54 */
55
56/*
57 * cds_wfs_node_init: initialize wait-free stack node.
58 */
756a0322 59static inline
16aa9ee8 60void _cds_wfs_node_init(struct cds_wfs_node *node)
294d3396
MD
61{
62 node->next = NULL;
63}
64
718eb63e 65/*
200d100e
MD
66 * __cds_wfs_init: initialize wait-free stack. Don't pair with
67 * any destroy function.
718eb63e
EW
68 */
69static inline void ___cds_wfs_init(struct __cds_wfs_stack *s)
70{
71 s->head = CDS_WFS_END;
72}
73
edac6b69 74/*
200d100e
MD
75 * cds_wfs_init: initialize wait-free stack. Pair with
76 * cds_wfs_destroy().
edac6b69 77 */
756a0322 78static inline
16aa9ee8 79void _cds_wfs_init(struct cds_wfs_stack *s)
294d3396
MD
80{
81 int ret;
82
edac6b69 83 s->head = CDS_WFS_END;
294d3396 84 ret = pthread_mutex_init(&s->lock, NULL);
01477510 85 urcu_posix_assert(!ret);
294d3396
MD
86}
87
200d100e
MD
88/*
89 * cds_wfs_destroy: destroy wait-free stack. Pair with
90 * cds_wfs_init().
91 */
92static inline
93void _cds_wfs_destroy(struct cds_wfs_stack *s)
94{
95 int ret = pthread_mutex_destroy(&s->lock);
01477510 96 urcu_posix_assert(!ret);
200d100e
MD
97}
98
edac6b69
MD
99static inline bool ___cds_wfs_end(void *node)
100{
101 return node == CDS_WFS_END;
102}
103
191098fc 104/*
edac6b69
MD
105 * cds_wfs_empty: return whether wait-free stack is empty.
106 *
107 * No memory barrier is issued. No mutual exclusion is required.
108 */
718eb63e 109static inline bool _cds_wfs_empty(cds_wfs_stack_ptr_t u_stack)
edac6b69 110{
718eb63e
EW
111 struct __cds_wfs_stack *s = u_stack._s;
112
edac6b69
MD
113 return ___cds_wfs_end(CMM_LOAD_SHARED(s->head));
114}
115
116/*
117 * cds_wfs_push: push a node into the stack.
118 *
119 * Issues a full memory barrier before push. No mutual exclusion is
120 * required.
121 *
122 * Returns 0 if the stack was empty prior to adding the node.
123 * Returns non-zero otherwise.
191098fc 124 */
756a0322 125static inline
718eb63e 126int _cds_wfs_push(cds_wfs_stack_ptr_t u_stack, struct cds_wfs_node *node)
294d3396 127{
718eb63e 128 struct __cds_wfs_stack *s = u_stack._s;
edac6b69 129 struct cds_wfs_head *old_head, *new_head;
294d3396 130
01477510 131 urcu_posix_assert(node->next == NULL);
edac6b69 132 new_head = caa_container_of(node, struct cds_wfs_head, node);
294d3396 133 /*
edac6b69
MD
134 * uatomic_xchg() implicit memory barrier orders earlier stores
135 * to node (setting it to NULL) before publication.
294d3396 136 */
edac6b69 137 old_head = uatomic_xchg(&s->head, new_head);
294d3396 138 /*
edac6b69
MD
139 * At this point, dequeuers see a NULL node->next, they should
140 * busy-wait until node->next is set to old_head.
294d3396 141 */
edac6b69
MD
142 CMM_STORE_SHARED(node->next, &old_head->node);
143 return !___cds_wfs_end(old_head);
294d3396
MD
144}
145
146/*
edac6b69 147 * Waiting for push to complete enqueue and return the next node.
294d3396 148 */
edac6b69 149static inline struct cds_wfs_node *
af67624d 150___cds_wfs_node_sync_next(struct cds_wfs_node *node, int blocking)
294d3396 151{
edac6b69 152 struct cds_wfs_node *next;
294d3396
MD
153 int attempt = 0;
154
294d3396
MD
155 /*
156 * Adaptative busy-looping waiting for push to complete.
157 */
edac6b69 158 while ((next = CMM_LOAD_SHARED(node->next)) == NULL) {
af67624d
MD
159 if (!blocking)
160 return CDS_WFS_WOULDBLOCK;
16aa9ee8 161 if (++attempt >= CDS_WFS_ADAPT_ATTEMPTS) {
d8a93add 162 (void) poll(NULL, 0, CDS_WFS_WAIT); /* Wait for 10ms */
294d3396 163 attempt = 0;
edac6b69 164 } else {
06f22bdb 165 caa_cpu_relax();
edac6b69 166 }
294d3396 167 }
edac6b69
MD
168
169 return next;
294d3396
MD
170}
171
af67624d
MD
172static inline
173struct cds_wfs_node *
711ff0f9 174___cds_wfs_pop(cds_wfs_stack_ptr_t u_stack, int *state, int blocking)
af67624d
MD
175{
176 struct cds_wfs_head *head, *new_head;
177 struct cds_wfs_node *next;
711ff0f9 178 struct __cds_wfs_stack *s = u_stack._s;
af67624d 179
c8975b94
MD
180 if (state)
181 *state = 0;
af67624d
MD
182 for (;;) {
183 head = CMM_LOAD_SHARED(s->head);
c8975b94 184 if (___cds_wfs_end(head)) {
af67624d 185 return NULL;
c8975b94 186 }
af67624d 187 next = ___cds_wfs_node_sync_next(&head->node, blocking);
c8975b94 188 if (!blocking && next == CDS_WFS_WOULDBLOCK) {
af67624d 189 return CDS_WFS_WOULDBLOCK;
c8975b94 190 }
af67624d 191 new_head = caa_container_of(next, struct cds_wfs_head, node);
c8975b94
MD
192 if (uatomic_cmpxchg(&s->head, head, new_head) == head) {
193 if (state && ___cds_wfs_end(new_head))
194 *state |= CDS_WFS_STATE_LAST;
af67624d 195 return &head->node;
c8975b94
MD
196 }
197 if (!blocking) {
af67624d 198 return CDS_WFS_WOULDBLOCK;
c8975b94 199 }
af67624d
MD
200 /* busy-loop if head changed under us */
201 }
202}
203
edac6b69 204/*
c8975b94 205 * __cds_wfs_pop_with_state_blocking: pop a node from the stack, with state.
edac6b69
MD
206 *
207 * Returns NULL if stack is empty.
208 *
209 * __cds_wfs_pop_blocking needs to be synchronized using one of the
210 * following techniques:
211 *
212 * 1) Calling __cds_wfs_pop_blocking under rcu read lock critical
213 * section. The caller must wait for a grace period to pass before
214 * freeing the returned node or modifying the cds_wfs_node structure.
215 * 2) Using mutual exclusion (e.g. mutexes) to protect
216 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
217 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
218 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
c8975b94
MD
219 *
220 * "state" saves state flags atomically sampled with pop operation.
edac6b69 221 */
c8975b94
MD
222static inline
223struct cds_wfs_node *
711ff0f9 224___cds_wfs_pop_with_state_blocking(cds_wfs_stack_ptr_t u_stack, int *state)
c8975b94 225{
711ff0f9 226 return ___cds_wfs_pop(u_stack, state, 1);
c8975b94
MD
227}
228
756a0322 229static inline
16aa9ee8 230struct cds_wfs_node *
711ff0f9 231___cds_wfs_pop_blocking(cds_wfs_stack_ptr_t u_stack)
edac6b69 232{
711ff0f9 233 return ___cds_wfs_pop_with_state_blocking(u_stack, NULL);
c8975b94
MD
234}
235
236/*
237 * __cds_wfs_pop_with_state_nonblocking: pop a node from the stack.
238 *
239 * Same as __cds_wfs_pop_with_state_blocking, but returns
240 * CDS_WFS_WOULDBLOCK if it needs to block.
241 *
242 * "state" saves state flags atomically sampled with pop operation.
243 */
244static inline
245struct cds_wfs_node *
711ff0f9 246___cds_wfs_pop_with_state_nonblocking(cds_wfs_stack_ptr_t u_stack, int *state)
c8975b94 247{
711ff0f9 248 return ___cds_wfs_pop(u_stack, state, 0);
af67624d 249}
edac6b69 250
af67624d
MD
251/*
252 * __cds_wfs_pop_nonblocking: pop a node from the stack.
253 *
254 * Same as __cds_wfs_pop_blocking, but returns CDS_WFS_WOULDBLOCK if
255 * it needs to block.
256 */
257static inline
258struct cds_wfs_node *
711ff0f9 259___cds_wfs_pop_nonblocking(cds_wfs_stack_ptr_t u_stack)
af67624d 260{
711ff0f9 261 return ___cds_wfs_pop_with_state_nonblocking(u_stack, NULL);
edac6b69
MD
262}
263
264/*
265 * __cds_wfs_pop_all: pop all nodes from a stack.
266 *
267 * __cds_wfs_pop_all does not require any synchronization with other
268 * push, nor with other __cds_wfs_pop_all, but requires synchronization
269 * matching the technique used to synchronize __cds_wfs_pop_blocking:
270 *
271 * 1) If __cds_wfs_pop_blocking is called under rcu read lock critical
272 * section, both __cds_wfs_pop_blocking and cds_wfs_pop_all callers
273 * must wait for a grace period to pass before freeing the returned
274 * node or modifying the cds_wfs_node structure. However, no RCU
275 * read-side critical section is needed around __cds_wfs_pop_all.
276 * 2) Using mutual exclusion (e.g. mutexes) to protect
277 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
278 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
279 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
280 */
281static inline
282struct cds_wfs_head *
718eb63e 283___cds_wfs_pop_all(cds_wfs_stack_ptr_t u_stack)
edac6b69 284{
718eb63e 285 struct __cds_wfs_stack *s = u_stack._s;
edac6b69
MD
286 struct cds_wfs_head *head;
287
288 /*
289 * Implicit memory barrier after uatomic_xchg() matches implicit
290 * memory barrier before uatomic_xchg() in cds_wfs_push. It
291 * ensures that all nodes of the returned list are consistent.
292 * There is no need to issue memory barriers when iterating on
293 * the returned list, because the full memory barrier issued
294 * prior to each uatomic_cmpxchg, which each write to head, are
295 * taking care to order writes to each node prior to the full
296 * memory barrier after this uatomic_xchg().
297 */
298 head = uatomic_xchg(&s->head, CDS_WFS_END);
299 if (___cds_wfs_end(head))
300 return NULL;
301 return head;
302}
303
304/*
305 * cds_wfs_pop_lock: lock stack pop-protection mutex.
306 */
307static inline void _cds_wfs_pop_lock(struct cds_wfs_stack *s)
294d3396 308{
294d3396
MD
309 int ret;
310
311 ret = pthread_mutex_lock(&s->lock);
01477510 312 urcu_posix_assert(!ret);
edac6b69
MD
313}
314
315/*
316 * cds_wfs_pop_unlock: unlock stack pop-protection mutex.
317 */
318static inline void _cds_wfs_pop_unlock(struct cds_wfs_stack *s)
319{
320 int ret;
321
294d3396 322 ret = pthread_mutex_unlock(&s->lock);
01477510 323 urcu_posix_assert(!ret);
edac6b69
MD
324}
325
326/*
c8975b94 327 * Call __cds_wfs_pop_with_state_blocking with an internal pop mutex held.
edac6b69
MD
328 */
329static inline
330struct cds_wfs_node *
c8975b94 331_cds_wfs_pop_with_state_blocking(struct cds_wfs_stack *s, int *state)
edac6b69
MD
332{
333 struct cds_wfs_node *retnode;
28757437 334 cds_wfs_stack_ptr_t stack;
edac6b69
MD
335
336 _cds_wfs_pop_lock(s);
28757437
SM
337 stack.s = s;
338 retnode = ___cds_wfs_pop_with_state_blocking(stack, state);
edac6b69 339 _cds_wfs_pop_unlock(s);
294d3396
MD
340 return retnode;
341}
342
c8975b94
MD
343/*
344 * Call _cds_wfs_pop_with_state_blocking without saving any state.
345 */
346static inline
347struct cds_wfs_node *
348_cds_wfs_pop_blocking(struct cds_wfs_stack *s)
349{
350 return _cds_wfs_pop_with_state_blocking(s, NULL);
351}
352
edac6b69
MD
353/*
354 * Call __cds_wfs_pop_all with an internal pop mutex held.
355 */
356static inline
357struct cds_wfs_head *
358_cds_wfs_pop_all_blocking(struct cds_wfs_stack *s)
359{
360 struct cds_wfs_head *rethead;
28757437 361 cds_wfs_stack_ptr_t stack;
edac6b69
MD
362
363 _cds_wfs_pop_lock(s);
28757437
SM
364 stack.s = s;
365 rethead = ___cds_wfs_pop_all(stack);
edac6b69
MD
366 _cds_wfs_pop_unlock(s);
367 return rethead;
368}
369
370/*
c7ba06ba 371 * cds_wfs_first: get first node of a popped stack.
edac6b69
MD
372 *
373 * Content written into the node before enqueue is guaranteed to be
374 * consistent, but no other memory ordering is ensured.
375 *
376 * Used by for-like iteration macros in urcu/wfstack.h:
377 * cds_wfs_for_each_blocking()
378 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
379 *
380 * Returns NULL if popped stack is empty, top stack node otherwise.
edac6b69
MD
381 */
382static inline struct cds_wfs_node *
c7ba06ba 383_cds_wfs_first(struct cds_wfs_head *head)
edac6b69
MD
384{
385 if (___cds_wfs_end(head))
386 return NULL;
387 return &head->node;
388}
389
af67624d
MD
390static inline struct cds_wfs_node *
391___cds_wfs_next(struct cds_wfs_node *node, int blocking)
392{
393 struct cds_wfs_node *next;
394
395 next = ___cds_wfs_node_sync_next(node, blocking);
396 /*
397 * CDS_WFS_WOULDBLOCK != CSD_WFS_END, so we can check for end
398 * even if ___cds_wfs_node_sync_next returns CDS_WFS_WOULDBLOCK,
399 * and still return CDS_WFS_WOULDBLOCK.
400 */
401 if (___cds_wfs_end(next))
402 return NULL;
403 return next;
404}
405
edac6b69
MD
406/*
407 * cds_wfs_next_blocking: get next node of a popped stack.
408 *
409 * Content written into the node before enqueue is guaranteed to be
410 * consistent, but no other memory ordering is ensured.
411 *
412 * Used by for-like iteration macros in urcu/wfstack.h:
413 * cds_wfs_for_each_blocking()
414 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
415 *
416 * Returns NULL if reached end of popped stack, non-NULL next stack
417 * node otherwise.
edac6b69
MD
418 */
419static inline struct cds_wfs_node *
420_cds_wfs_next_blocking(struct cds_wfs_node *node)
421{
af67624d
MD
422 return ___cds_wfs_next(node, 1);
423}
edac6b69 424
af67624d
MD
425
426/*
427 * cds_wfs_next_nonblocking: get next node of a popped stack.
428 *
429 * Same as cds_wfs_next_blocking, but returns CDS_WFS_WOULDBLOCK if it
430 * needs to block.
431 */
432static inline struct cds_wfs_node *
433_cds_wfs_next_nonblocking(struct cds_wfs_node *node)
434{
435 return ___cds_wfs_next(node, 0);
edac6b69
MD
436}
437
294d3396
MD
438#ifdef __cplusplus
439}
440#endif
441
edac6b69 442#endif /* _URCU_STATIC_WFSTACK_H */
This page took 0.065564 seconds and 4 git commands to generate.