1 #ifndef _URCU_WFSTACK_H
2 #define _URCU_WFSTACK_H
7 * Userspace RCU library - Stack with wait-free push, blocking traversal.
9 * Copyright 2010 - 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.
42 * Blocking operations: cds_wfs_pop, cds_wfs_pop_all, iteration on stack
43 * head returned by pop_all.
45 * Synchronization table:
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 "-".
51 * cds_wfs_push __cds_wfs_pop __cds_wfs_pop_all
54 * __cds_wfs_pop_all - X -
56 * cds_wfs_pop and cds_wfs_pop_all use an internal mutex to provide
61 * struct cds_wfs_node is returned by __cds_wfs_pop, and also used as
62 * iterator on stack. It is not safe to dereference the node next
63 * pointer when returned by __cds_wfs_pop_blocking.
66 struct cds_wfs_node
*next
;
70 * struct cds_wfs_head is returned by __cds_wfs_pop_all, and can be used
71 * to begin iteration on the stack. "node" needs to be the first field of
72 * cds_wfs_head, so the end-of-stack pointer value can be used for both
76 struct cds_wfs_node node
;
79 struct cds_wfs_stack
{
80 struct cds_wfs_head
*head
;
86 #include <urcu/static/wfstack.h>
88 #define cds_wfs_node_init _cds_wfs_node_init
89 #define cds_wfs_init _cds_wfs_init
90 #define cds_wfs_empty _cds_wfs_empty
91 #define cds_wfs_push _cds_wfs_push
93 /* Locking performed internally */
94 #define cds_wfs_pop_blocking _cds_wfs_pop_blocking
95 #define cds_wfs_pop_all_blocking _cds_wfs_pop_all_blocking
98 * For iteration on cds_wfs_head returned by __cds_wfs_pop_all or
99 * cds_wfs_pop_all_blocking.
101 #define cds_wfs_first_blocking _cds_wfs_first_blocking
102 #define cds_wfs_next_blocking _cds_wfs_next_blocking
104 /* Pop locking with internal mutex */
105 #define cds_wfs_pop_lock _cds_wfs_pop_lock
106 #define cds_wfs_pop_unlock _cds_wfs_pop_unlock
108 /* Synchronization ensured by the caller. See synchronization table. */
109 #define __cds_wfs_pop_blocking ___cds_wfs_pop_blocking
110 #define __cds_wfs_pop_all ___cds_wfs_pop_all
112 #else /* !_LGPL_SOURCE */
115 * cds_wfs_node_init: initialize wait-free stack node.
117 extern void cds_wfs_node_init(struct cds_wfs_node
*node
);
120 * cds_wfs_init: initialize wait-free stack.
122 extern void cds_wfs_init(struct cds_wfs_stack
*s
);
125 * cds_wfs_empty: return whether wait-free stack is empty.
127 * No memory barrier is issued. No mutual exclusion is required.
129 extern bool cds_wfs_empty(struct cds_wfs_stack
*s
);
132 * cds_wfs_push: push a node into the stack.
134 * Issues a full memory barrier before push. No mutual exclusion is
137 * Returns 0 if the stack was empty prior to adding the node.
138 * Returns non-zero otherwise.
140 extern int cds_wfs_push(struct cds_wfs_stack
*s
, struct cds_wfs_node
*node
);
143 * cds_wfs_pop_blocking: pop a node from the stack.
145 * Calls __cds_wfs_pop_blocking with an internal pop mutex held.
147 extern struct cds_wfs_node
*cds_wfs_pop_blocking(struct cds_wfs_stack
*s
);
150 * cds_wfs_pop_all_blocking: pop all nodes from a stack.
152 * Calls __cds_wfs_pop_all with an internal pop mutex held.
154 extern struct cds_wfs_head
*cds_wfs_pop_all_blocking(struct cds_wfs_stack
*s
);
157 * cds_wfs_first_blocking: get first node of a popped stack.
159 * Content written into the node before enqueue is guaranteed to be
160 * consistent, but no other memory ordering is ensured.
162 * Used by for-like iteration macros in urcu/wfstack.h:
163 * cds_wfs_for_each_blocking()
164 * cds_wfs_for_each_blocking_safe()
166 extern struct cds_wfs_node
*cds_wfs_first_blocking(struct cds_wfs_head
*head
);
169 * cds_wfs_next_blocking: get next node of a popped stack.
171 * Content written into the node before enqueue is guaranteed to be
172 * consistent, but no other memory ordering is ensured.
174 * Used by for-like iteration macros in urcu/wfstack.h:
175 * cds_wfs_for_each_blocking()
176 * cds_wfs_for_each_blocking_safe()
178 extern struct cds_wfs_node
*cds_wfs_next_blocking(struct cds_wfs_node
*node
);
181 * cds_wfs_pop_lock: lock stack pop-protection mutex.
183 extern void cds_wfs_pop_lock(struct cds_wfs_stack
*s
);
186 * cds_wfs_pop_unlock: unlock stack pop-protection mutex.
188 extern void cds_wfs_pop_unlock(struct cds_wfs_stack
*s
);
191 * __cds_wfs_pop_blocking: pop a node from the stack.
193 * Returns NULL if stack is empty.
195 * __cds_wfs_pop_blocking needs to be synchronized using one of the
196 * following techniques:
198 * 1) Calling __cds_wfs_pop_blocking under rcu read lock critical
199 * section. The caller must wait for a grace period to pass before
200 * freeing the returned node or modifying the cds_wfs_node structure.
201 * 2) Using mutual exclusion (e.g. mutexes) to protect
202 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
203 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
204 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
206 extern struct cds_wfs_node
*__cds_wfs_pop_blocking(struct cds_wfs_stack
*s
);
209 * __cds_wfs_pop_all: pop all nodes from a stack.
211 * __cds_wfs_pop_all does not require any synchronization with other
212 * push, nor with other __cds_wfs_pop_all, but requires synchronization
213 * matching the technique used to synchronize __cds_wfs_pop_blocking:
215 * 1) If __cds_wfs_pop_blocking is called under rcu read lock critical
216 * section, both __cds_wfs_pop_blocking and cds_wfs_pop_all callers
217 * must wait for a grace period to pass before freeing the returned
218 * node or modifying the cds_wfs_node structure. However, no RCU
219 * read-side critical section is needed around __cds_wfs_pop_all.
220 * 2) Using mutual exclusion (e.g. mutexes) to protect
221 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
222 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
223 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
225 extern struct cds_wfs_head
*__cds_wfs_pop_all(struct cds_wfs_stack
*s
);
227 #endif /* !_LGPL_SOURCE */
234 * cds_wfs_for_each_blocking: Iterate over all nodes returned by
235 * __cds_wfs_pop_all().
236 * @head: head of the queue (struct cds_wfs_head pointer).
237 * @node: iterator (struct cds_wfs_node pointer).
239 * Content written into each node before enqueue is guaranteed to be
240 * consistent, but no other memory ordering is ensured.
242 #define cds_wfs_for_each_blocking(head, node) \
243 for (node = cds_wfs_first_blocking(head); \
245 node = cds_wfs_next_blocking(node))
248 * cds_wfs_for_each_blocking_safe: Iterate over all nodes returned by
249 * __cds_wfs_pop_all(). Safe against deletion.
250 * @head: head of the queue (struct cds_wfs_head pointer).
251 * @node: iterator (struct cds_wfs_node pointer).
252 * @n: struct cds_wfs_node pointer holding the next pointer (used
255 * Content written into each node before enqueue is guaranteed to be
256 * consistent, but no other memory ordering is ensured.
258 #define cds_wfs_for_each_blocking_safe(head, node, n) \
259 for (node = cds_wfs_first_blocking(head), \
260 n = (node ? cds_wfs_next_blocking(node) : NULL); \
262 node = n, n = (node ? cds_wfs_next_blocking(node) : NULL))
264 #endif /* _URCU_WFSTACK_H */
This page took 0.037914 seconds and 4 git commands to generate.