5ef8bab04825091232e939aff9d75e19b929f11c
1 #ifndef _URCU_WFSTACK_H
2 #define _URCU_WFSTACK_H
7 * Userspace RCU library - Stack with wait-free push, blocking traversal.
9 * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
29 #include <urcu/compiler.h>
36 * Stack with wait-free push, blocking traversal.
38 * Stack implementing push, pop, pop_all operations, as well as iterator
39 * on the stack head returned by pop_all.
41 * Wait-free operations: cds_wfs_push, __cds_wfs_pop_all, cds_wfs_empty,
43 * Blocking operations: cds_wfs_pop, cds_wfs_pop_all, cds_wfs_next,
44 * iteration on stack head returned by pop_all.
46 * Synchronization table:
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 "-".
52 * cds_wfs_push __cds_wfs_pop __cds_wfs_pop_all
55 * __cds_wfs_pop_all - X -
57 * cds_wfs_pop and cds_wfs_pop_all use an internal mutex to provide
61 #define CDS_WFS_WOULDBLOCK ((void *) -1UL)
64 CDS_WFS_STATE_LAST
= (1U << 0),
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.
73 struct cds_wfs_node
*next
;
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
83 struct cds_wfs_node node
;
86 struct cds_wfs_stack
{
87 struct cds_wfs_head
*head
;
93 #include <urcu/static/wfstack.h>
95 #define cds_wfs_node_init _cds_wfs_node_init
96 #define cds_wfs_init _cds_wfs_init
97 #define cds_wfs_destroy _cds_wfs_destroy
98 #define cds_wfs_empty _cds_wfs_empty
99 #define cds_wfs_push _cds_wfs_push
101 /* Locking performed internally */
102 #define cds_wfs_pop_blocking _cds_wfs_pop_blocking
103 #define cds_wfs_pop_with_state_blocking _cds_wfs_pop_with_state_blocking
104 #define cds_wfs_pop_all_blocking _cds_wfs_pop_all_blocking
107 * For iteration on cds_wfs_head returned by __cds_wfs_pop_all or
108 * cds_wfs_pop_all_blocking.
110 #define cds_wfs_first _cds_wfs_first
111 #define cds_wfs_next_blocking _cds_wfs_next_blocking
112 #define cds_wfs_next_nonblocking _cds_wfs_next_nonblocking
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
118 /* Synchronization ensured by the caller. See synchronization table. */
119 #define __cds_wfs_pop_blocking ___cds_wfs_pop_blocking
120 #define __cds_wfs_pop_with_state_blocking \
121 ___cds_wfs_pop_with_state_blocking
122 #define __cds_wfs_pop_nonblocking ___cds_wfs_pop_nonblocking
123 #define __cds_wfs_pop_with_state_nonblocking \
124 ___cds_wfs_pop_with_state_nonblocking
125 #define __cds_wfs_pop_all ___cds_wfs_pop_all
127 #else /* !_LGPL_SOURCE */
130 * cds_wfs_node_init: initialize wait-free stack node.
132 extern void cds_wfs_node_init(struct cds_wfs_node
*node
);
135 * cds_wfs_init: initialize wait-free stack (with lock). Pair with
138 extern void cds_wfs_init(struct cds_wfs_stack
*s
);
141 * cds_wfs_destroy: destroy wait-free stack (with lock). Pair with
144 extern void cds_wfs_destroy(struct cds_wfs_stack
*s
);
147 * cds_wfs_empty: return whether wait-free stack is empty.
149 * No memory barrier is issued. No mutual exclusion is required.
151 extern bool cds_wfs_empty(struct cds_wfs_stack
*s
);
154 * cds_wfs_push: push a node into the stack.
156 * Issues a full memory barrier before push. No mutual exclusion is
159 * Returns 0 if the stack was empty prior to adding the node.
160 * Returns non-zero otherwise.
162 extern int cds_wfs_push(struct cds_wfs_stack
*s
, struct cds_wfs_node
*node
);
165 * cds_wfs_pop_blocking: pop a node from the stack.
167 * Calls __cds_wfs_pop_blocking with an internal pop mutex held.
169 extern struct cds_wfs_node
*cds_wfs_pop_blocking(struct cds_wfs_stack
*s
);
172 * cds_wfs_pop_with_state_blocking: pop a node from the stack, with state.
174 * Same as cds_wfs_pop_blocking, but stores whether the stack was
175 * empty into state (CDS_WFS_STATE_LAST).
177 extern struct cds_wfs_node
*
178 cds_wfs_pop_with_state_blocking(struct cds_wfs_stack
*s
, int *state
);
181 * cds_wfs_pop_all_blocking: pop all nodes from a stack.
183 * Calls __cds_wfs_pop_all with an internal pop mutex held.
185 extern struct cds_wfs_head
*cds_wfs_pop_all_blocking(struct cds_wfs_stack
*s
);
188 * cds_wfs_first: get first node of a popped stack.
190 * Content written into the node before enqueue is guaranteed to be
191 * consistent, but no other memory ordering is ensured.
193 * Used by for-like iteration macros in urcu/wfstack.h:
194 * cds_wfs_for_each_blocking()
195 * cds_wfs_for_each_blocking_safe()
197 * Returns NULL if popped stack is empty, top stack node otherwise.
199 extern struct cds_wfs_node
*cds_wfs_first(struct cds_wfs_head
*head
);
202 * cds_wfs_next_blocking: get next node of a popped stack.
204 * Content written into the node before enqueue is guaranteed to be
205 * consistent, but no other memory ordering is ensured.
207 * Used by for-like iteration macros in urcu/wfstack.h:
208 * cds_wfs_for_each_blocking()
209 * cds_wfs_for_each_blocking_safe()
211 * Returns NULL if reached end of popped stack, non-NULL next stack
214 extern struct cds_wfs_node
*cds_wfs_next_blocking(struct cds_wfs_node
*node
);
217 * cds_wfs_next_nonblocking: get next node of a popped stack.
219 * Same as cds_wfs_next_blocking, but returns CDS_WFS_WOULDBLOCK if it
222 extern struct cds_wfs_node
*cds_wfs_next_nonblocking(struct cds_wfs_node
*node
);
225 * cds_wfs_pop_lock: lock stack pop-protection mutex.
227 extern void cds_wfs_pop_lock(struct cds_wfs_stack
*s
);
230 * cds_wfs_pop_unlock: unlock stack pop-protection mutex.
232 extern void cds_wfs_pop_unlock(struct cds_wfs_stack
*s
);
235 * __cds_wfs_pop_blocking: pop a node from the stack.
237 * Returns NULL if stack is empty.
239 * __cds_wfs_pop_blocking needs to be synchronized using one of the
240 * following techniques:
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).
250 extern struct cds_wfs_node
*__cds_wfs_pop_blocking(struct cds_wfs_stack
*s
);
253 * __cds_wfs_pop_with_state_blocking: pop a node from the stack, with state.
255 * Same as __cds_wfs_pop_blocking, but stores whether the stack was
256 * empty into state (CDS_WFS_STATE_LAST).
258 extern struct cds_wfs_node
*
259 __cds_wfs_pop_with_state_blocking(struct cds_wfs_stack
*s
, int *state
);
262 * __cds_wfs_pop_nonblocking: pop a node from the stack.
264 * Same as __cds_wfs_pop_blocking, but returns CDS_WFS_WOULDBLOCK if
267 extern struct cds_wfs_node
*__cds_wfs_pop_nonblocking(struct cds_wfs_stack
*s
);
270 * __cds_wfs_pop_with_state_nonblocking: pop a node from the stack, with state.
272 * Same as __cds_wfs_pop_nonblocking, but stores whether the stack was
273 * empty into state (CDS_WFS_STATE_LAST).
275 extern struct cds_wfs_node
*
276 __cds_wfs_pop_with_state_nonblocking(struct cds_wfs_stack
*s
,
280 * __cds_wfs_pop_all: pop all nodes from a stack.
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:
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).
296 extern struct cds_wfs_head
*__cds_wfs_pop_all(struct cds_wfs_stack
*s
);
298 #endif /* !_LGPL_SOURCE */
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).
310 * Content written into each node before enqueue is guaranteed to be
311 * consistent, but no other memory ordering is ensured.
313 #define cds_wfs_for_each_blocking(head, node) \
314 for (node = cds_wfs_first(head); \
316 node = cds_wfs_next_blocking(node))
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
326 * Content written into each node before enqueue is guaranteed to be
327 * consistent, but no other memory ordering is ensured.
329 #define cds_wfs_for_each_blocking_safe(head, node, n) \
330 for (node = cds_wfs_first(head), \
331 n = (node ? cds_wfs_next_blocking(node) : NULL); \
333 node = n, n = (node ? cds_wfs_next_blocking(node) : NULL))
335 #endif /* _URCU_WFSTACK_H */
This page took 0.035135 seconds and 3 git commands to generate.