1 #ifndef _URCU_STATIC_WFSTACK_H
2 #define _URCU_STATIC_WFSTACK_H
5 * urcu/static/wfstack.h
7 * Userspace RCU library - Stack with with wait-free push, blocking traversal.
9 * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See urcu/wfstack.h for
10 * linking dynamically with the userspace rcu library.
12 * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <urcu/compiler.h>
34 #include <urcu/uatomic.h>
40 #define CDS_WFS_END ((void *) 0x1UL)
41 #define CDS_WFS_ADAPT_ATTEMPTS 10 /* Retry if being set */
42 #define CDS_WFS_WAIT 10 /* Wait 10 ms if being set */
45 * Stack with wait-free push, blocking traversal.
47 * Stack implementing push, pop, pop_all operations, as well as iterator
48 * on the stack head returned by pop_all.
50 * Wait-free operations: cds_wfs_push, __cds_wfs_pop_all, cds_wfs_empty,
52 * Blocking operations: cds_wfs_pop, cds_wfs_pop_all, cds_wfs_next,
53 * iteration on stack head returned by pop_all.
55 * Synchronization table:
57 * External synchronization techniques described in the API below is
58 * required between pairs marked with "X". No external synchronization
59 * required between pairs marked with "-".
61 * cds_wfs_push __cds_wfs_pop __cds_wfs_pop_all
64 * __cds_wfs_pop_all - X -
66 * cds_wfs_pop and cds_wfs_pop_all use an internal mutex to provide
71 * cds_wfs_node_init: initialize wait-free stack node.
74 void _cds_wfs_node_init(struct cds_wfs_node
*node
)
80 * cds_wfs_init: initialize wait-free stack.
83 void _cds_wfs_init(struct cds_wfs_stack
*s
)
87 s
->head
= CDS_WFS_END
;
88 ret
= pthread_mutex_init(&s
->lock
, NULL
);
92 static inline bool ___cds_wfs_end(void *node
)
94 return node
== CDS_WFS_END
;
98 * cds_wfs_empty: return whether wait-free stack is empty.
100 * No memory barrier is issued. No mutual exclusion is required.
102 static inline bool _cds_wfs_empty(struct cds_wfs_stack
*s
)
104 return ___cds_wfs_end(CMM_LOAD_SHARED(s
->head
));
108 * cds_wfs_push: push a node into the stack.
110 * Issues a full memory barrier before push. No mutual exclusion is
113 * Returns 0 if the stack was empty prior to adding the node.
114 * Returns non-zero otherwise.
117 int _cds_wfs_push(struct cds_wfs_stack
*s
, struct cds_wfs_node
*node
)
119 struct cds_wfs_head
*old_head
, *new_head
;
121 assert(node
->next
== NULL
);
122 new_head
= caa_container_of(node
, struct cds_wfs_head
, node
);
124 * uatomic_xchg() implicit memory barrier orders earlier stores
125 * to node (setting it to NULL) before publication.
127 old_head
= uatomic_xchg(&s
->head
, new_head
);
129 * At this point, dequeuers see a NULL node->next, they should
130 * busy-wait until node->next is set to old_head.
132 CMM_STORE_SHARED(node
->next
, &old_head
->node
);
133 return !___cds_wfs_end(old_head
);
137 * Waiting for push to complete enqueue and return the next node.
139 static inline struct cds_wfs_node
*
140 ___cds_wfs_node_sync_next(struct cds_wfs_node
*node
, int blocking
)
142 struct cds_wfs_node
*next
;
146 * Adaptative busy-looping waiting for push to complete.
148 while ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
150 return CDS_WFS_WOULDBLOCK
;
151 if (++attempt
>= CDS_WFS_ADAPT_ATTEMPTS
) {
152 poll(NULL
, 0, CDS_WFS_WAIT
); /* Wait for 10ms */
163 struct cds_wfs_node
*
164 ___cds_wfs_pop(struct cds_wfs_stack
*s
, int *state
, int blocking
)
166 struct cds_wfs_head
*head
, *new_head
;
167 struct cds_wfs_node
*next
;
172 head
= CMM_LOAD_SHARED(s
->head
);
173 if (___cds_wfs_end(head
)) {
176 next
= ___cds_wfs_node_sync_next(&head
->node
, blocking
);
177 if (!blocking
&& next
== CDS_WFS_WOULDBLOCK
) {
178 return CDS_WFS_WOULDBLOCK
;
180 new_head
= caa_container_of(next
, struct cds_wfs_head
, node
);
181 if (uatomic_cmpxchg(&s
->head
, head
, new_head
) == head
) {
182 if (state
&& ___cds_wfs_end(new_head
))
183 *state
|= CDS_WFS_STATE_LAST
;
187 return CDS_WFS_WOULDBLOCK
;
189 /* busy-loop if head changed under us */
194 * __cds_wfs_pop_with_state_blocking: pop a node from the stack, with state.
196 * Returns NULL if stack is empty.
198 * __cds_wfs_pop_blocking needs to be synchronized using one of the
199 * following techniques:
201 * 1) Calling __cds_wfs_pop_blocking under rcu read lock critical
202 * section. The caller must wait for a grace period to pass before
203 * freeing the returned node or modifying the cds_wfs_node structure.
204 * 2) Using mutual exclusion (e.g. mutexes) to protect
205 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
206 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
207 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
209 * "state" saves state flags atomically sampled with pop operation.
212 struct cds_wfs_node
*
213 ___cds_wfs_pop_with_state_blocking(struct cds_wfs_stack
*s
, int *state
)
215 return ___cds_wfs_pop(s
, state
, 1);
219 struct cds_wfs_node
*
220 ___cds_wfs_pop_blocking(struct cds_wfs_stack
*s
)
222 return ___cds_wfs_pop_with_state_blocking(s
, NULL
);
226 * __cds_wfs_pop_with_state_nonblocking: pop a node from the stack.
228 * Same as __cds_wfs_pop_with_state_blocking, but returns
229 * CDS_WFS_WOULDBLOCK if it needs to block.
231 * "state" saves state flags atomically sampled with pop operation.
234 struct cds_wfs_node
*
235 ___cds_wfs_pop_with_state_nonblocking(struct cds_wfs_stack
*s
, int *state
)
237 return ___cds_wfs_pop(s
, state
, 0);
241 * __cds_wfs_pop_nonblocking: pop a node from the stack.
243 * Same as __cds_wfs_pop_blocking, but returns CDS_WFS_WOULDBLOCK if
247 struct cds_wfs_node
*
248 ___cds_wfs_pop_nonblocking(struct cds_wfs_stack
*s
)
250 return ___cds_wfs_pop_with_state_nonblocking(s
, NULL
);
254 * __cds_wfs_pop_all: pop all nodes from a stack.
256 * __cds_wfs_pop_all does not require any synchronization with other
257 * push, nor with other __cds_wfs_pop_all, but requires synchronization
258 * matching the technique used to synchronize __cds_wfs_pop_blocking:
260 * 1) If __cds_wfs_pop_blocking is called under rcu read lock critical
261 * section, both __cds_wfs_pop_blocking and cds_wfs_pop_all callers
262 * must wait for a grace period to pass before freeing the returned
263 * node or modifying the cds_wfs_node structure. However, no RCU
264 * read-side critical section is needed around __cds_wfs_pop_all.
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).
271 struct cds_wfs_head
*
272 ___cds_wfs_pop_all(struct cds_wfs_stack
*s
)
274 struct cds_wfs_head
*head
;
277 * Implicit memory barrier after uatomic_xchg() matches implicit
278 * memory barrier before uatomic_xchg() in cds_wfs_push. It
279 * ensures that all nodes of the returned list are consistent.
280 * There is no need to issue memory barriers when iterating on
281 * the returned list, because the full memory barrier issued
282 * prior to each uatomic_cmpxchg, which each write to head, are
283 * taking care to order writes to each node prior to the full
284 * memory barrier after this uatomic_xchg().
286 head
= uatomic_xchg(&s
->head
, CDS_WFS_END
);
287 if (___cds_wfs_end(head
))
293 * cds_wfs_pop_lock: lock stack pop-protection mutex.
295 static inline void _cds_wfs_pop_lock(struct cds_wfs_stack
*s
)
299 ret
= pthread_mutex_lock(&s
->lock
);
304 * cds_wfs_pop_unlock: unlock stack pop-protection mutex.
306 static inline void _cds_wfs_pop_unlock(struct cds_wfs_stack
*s
)
310 ret
= pthread_mutex_unlock(&s
->lock
);
315 * Call __cds_wfs_pop_with_state_blocking with an internal pop mutex held.
318 struct cds_wfs_node
*
319 _cds_wfs_pop_with_state_blocking(struct cds_wfs_stack
*s
, int *state
)
321 struct cds_wfs_node
*retnode
;
323 _cds_wfs_pop_lock(s
);
324 retnode
= ___cds_wfs_pop_with_state_blocking(s
, state
);
325 _cds_wfs_pop_unlock(s
);
330 * Call _cds_wfs_pop_with_state_blocking without saving any state.
333 struct cds_wfs_node
*
334 _cds_wfs_pop_blocking(struct cds_wfs_stack
*s
)
336 return _cds_wfs_pop_with_state_blocking(s
, NULL
);
340 * Call __cds_wfs_pop_all with an internal pop mutex held.
343 struct cds_wfs_head
*
344 _cds_wfs_pop_all_blocking(struct cds_wfs_stack
*s
)
346 struct cds_wfs_head
*rethead
;
348 _cds_wfs_pop_lock(s
);
349 rethead
= ___cds_wfs_pop_all(s
);
350 _cds_wfs_pop_unlock(s
);
355 * cds_wfs_first: get first node of a popped stack.
357 * Content written into the node before enqueue is guaranteed to be
358 * consistent, but no other memory ordering is ensured.
360 * Used by for-like iteration macros in urcu/wfstack.h:
361 * cds_wfs_for_each_blocking()
362 * cds_wfs_for_each_blocking_safe()
364 * Returns NULL if popped stack is empty, top stack node otherwise.
366 static inline struct cds_wfs_node
*
367 _cds_wfs_first(struct cds_wfs_head
*head
)
369 if (___cds_wfs_end(head
))
374 static inline struct cds_wfs_node
*
375 ___cds_wfs_next(struct cds_wfs_node
*node
, int blocking
)
377 struct cds_wfs_node
*next
;
379 next
= ___cds_wfs_node_sync_next(node
, blocking
);
381 * CDS_WFS_WOULDBLOCK != CSD_WFS_END, so we can check for end
382 * even if ___cds_wfs_node_sync_next returns CDS_WFS_WOULDBLOCK,
383 * and still return CDS_WFS_WOULDBLOCK.
385 if (___cds_wfs_end(next
))
391 * cds_wfs_next_blocking: get next node of a popped stack.
393 * Content written into the node before enqueue is guaranteed to be
394 * consistent, but no other memory ordering is ensured.
396 * Used by for-like iteration macros in urcu/wfstack.h:
397 * cds_wfs_for_each_blocking()
398 * cds_wfs_for_each_blocking_safe()
400 * Returns NULL if reached end of popped stack, non-NULL next stack
403 static inline struct cds_wfs_node
*
404 _cds_wfs_next_blocking(struct cds_wfs_node
*node
)
406 return ___cds_wfs_next(node
, 1);
411 * cds_wfs_next_nonblocking: get next node of a popped stack.
413 * Same as cds_wfs_next_blocking, but returns CDS_WFS_WOULDBLOCK if it
416 static inline struct cds_wfs_node
*
417 _cds_wfs_next_nonblocking(struct cds_wfs_node
*node
)
419 return ___cds_wfs_next(node
, 0);
426 #endif /* _URCU_STATIC_WFSTACK_H */
This page took 0.03924 seconds and 4 git commands to generate.