wfstack API: rename cds_wfs_first_blocking to cds_wfs_first
[urcu.git] / urcu / wfstack.h
CommitLineData
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
32extern "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 *
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.
44 *
45 * Synchronization table:
46 *
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 "-".
50 *
51 * cds_wfs_push __cds_wfs_pop __cds_wfs_pop_all
52 * cds_wfs_push - - -
53 * __cds_wfs_pop - X X
54 * __cds_wfs_pop_all - X -
55 *
56 * cds_wfs_pop and cds_wfs_pop_all use an internal mutex to provide
57 * synchronization.
58 */
59
60/*
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.
64 */
16aa9ee8
DG
65struct cds_wfs_node {
66 struct cds_wfs_node *next;
cb3f3d6b
MD
67};
68
edac6b69
MD
69/*
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
73 * types.
74 */
75struct cds_wfs_head {
76 struct cds_wfs_node node;
77};
78
16aa9ee8 79struct cds_wfs_stack {
edac6b69 80 struct cds_wfs_head *head;
cb3f3d6b
MD
81 pthread_mutex_t lock;
82};
83
294d3396 84#ifdef _LGPL_SOURCE
cb3f3d6b 85
af7c2dbe 86#include <urcu/static/wfstack.h>
cb3f3d6b 87
16aa9ee8 88#define cds_wfs_node_init _cds_wfs_node_init
edac6b69
MD
89#define cds_wfs_init _cds_wfs_init
90#define cds_wfs_empty _cds_wfs_empty
91#define cds_wfs_push _cds_wfs_push
92
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
96
97/*
98 * For iteration on cds_wfs_head returned by __cds_wfs_pop_all or
99 * cds_wfs_pop_all_blocking.
100 */
c7ba06ba 101#define cds_wfs_first _cds_wfs_first
edac6b69
MD
102#define cds_wfs_next_blocking _cds_wfs_next_blocking
103
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
107
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
cb3f3d6b 111
294d3396 112#else /* !_LGPL_SOURCE */
cb3f3d6b 113
edac6b69
MD
114/*
115 * cds_wfs_node_init: initialize wait-free stack node.
116 */
16aa9ee8 117extern void cds_wfs_node_init(struct cds_wfs_node *node);
edac6b69
MD
118
119/*
120 * cds_wfs_init: initialize wait-free stack.
121 */
16aa9ee8 122extern void cds_wfs_init(struct cds_wfs_stack *s);
edac6b69
MD
123
124/*
125 * cds_wfs_empty: return whether wait-free stack is empty.
126 *
127 * No memory barrier is issued. No mutual exclusion is required.
128 */
129extern bool cds_wfs_empty(struct cds_wfs_stack *s);
130
131/*
132 * cds_wfs_push: push a node into the stack.
133 *
134 * Issues a full memory barrier before push. No mutual exclusion is
135 * required.
136 *
137 * Returns 0 if the stack was empty prior to adding the node.
138 * Returns non-zero otherwise.
139 */
191098fc 140extern int cds_wfs_push(struct cds_wfs_stack *s, struct cds_wfs_node *node);
edac6b69
MD
141
142/*
143 * cds_wfs_pop_blocking: pop a node from the stack.
144 *
145 * Calls __cds_wfs_pop_blocking with an internal pop mutex held.
146 */
16aa9ee8 147extern struct cds_wfs_node *cds_wfs_pop_blocking(struct cds_wfs_stack *s);
cb3f3d6b 148
edac6b69
MD
149/*
150 * cds_wfs_pop_all_blocking: pop all nodes from a stack.
151 *
152 * Calls __cds_wfs_pop_all with an internal pop mutex held.
153 */
154extern struct cds_wfs_head *cds_wfs_pop_all_blocking(struct cds_wfs_stack *s);
155
156/*
c7ba06ba 157 * cds_wfs_first: get first node of a popped stack.
edac6b69
MD
158 *
159 * Content written into the node before enqueue is guaranteed to be
160 * consistent, but no other memory ordering is ensured.
161 *
162 * Used by for-like iteration macros in urcu/wfstack.h:
163 * cds_wfs_for_each_blocking()
164 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
165 *
166 * Returns NULL if popped stack is empty, top stack node otherwise.
edac6b69 167 */
c7ba06ba 168extern struct cds_wfs_node *cds_wfs_first(struct cds_wfs_head *head);
edac6b69
MD
169
170/*
171 * cds_wfs_next_blocking: get next node of a popped stack.
172 *
173 * Content written into the node before enqueue is guaranteed to be
174 * consistent, but no other memory ordering is ensured.
175 *
176 * Used by for-like iteration macros in urcu/wfstack.h:
177 * cds_wfs_for_each_blocking()
178 * cds_wfs_for_each_blocking_safe()
8af2956c
MD
179 *
180 * Returns NULL if reached end of popped stack, non-NULL next stack
181 * node otherwise.
edac6b69
MD
182 */
183extern struct cds_wfs_node *cds_wfs_next_blocking(struct cds_wfs_node *node);
184
185/*
186 * cds_wfs_pop_lock: lock stack pop-protection mutex.
187 */
188extern void cds_wfs_pop_lock(struct cds_wfs_stack *s);
189
190/*
191 * cds_wfs_pop_unlock: unlock stack pop-protection mutex.
192 */
193extern void cds_wfs_pop_unlock(struct cds_wfs_stack *s);
194
195/*
196 * __cds_wfs_pop_blocking: pop a node from the stack.
197 *
198 * Returns NULL if stack is empty.
199 *
200 * __cds_wfs_pop_blocking needs to be synchronized using one of the
201 * following techniques:
202 *
203 * 1) Calling __cds_wfs_pop_blocking under rcu read lock critical
204 * section. The caller must wait for a grace period to pass before
205 * freeing the returned node or modifying the cds_wfs_node structure.
206 * 2) Using mutual exclusion (e.g. mutexes) to protect
207 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
208 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
209 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
210 */
211extern struct cds_wfs_node *__cds_wfs_pop_blocking(struct cds_wfs_stack *s);
212
213/*
214 * __cds_wfs_pop_all: pop all nodes from a stack.
215 *
216 * __cds_wfs_pop_all does not require any synchronization with other
217 * push, nor with other __cds_wfs_pop_all, but requires synchronization
218 * matching the technique used to synchronize __cds_wfs_pop_blocking:
219 *
220 * 1) If __cds_wfs_pop_blocking is called under rcu read lock critical
221 * section, both __cds_wfs_pop_blocking and cds_wfs_pop_all callers
222 * must wait for a grace period to pass before freeing the returned
223 * node or modifying the cds_wfs_node structure. However, no RCU
224 * read-side critical section is needed around __cds_wfs_pop_all.
225 * 2) Using mutual exclusion (e.g. mutexes) to protect
226 * __cds_wfs_pop_blocking and __cds_wfs_pop_all callers.
227 * 3) Ensuring that only ONE thread can call __cds_wfs_pop_blocking()
228 * and __cds_wfs_pop_all(). (multi-provider/single-consumer scheme).
229 */
230extern struct cds_wfs_head *__cds_wfs_pop_all(struct cds_wfs_stack *s);
231
294d3396 232#endif /* !_LGPL_SOURCE */
cb3f3d6b
MD
233
234#ifdef __cplusplus
235}
236#endif
237
edac6b69
MD
238/*
239 * cds_wfs_for_each_blocking: Iterate over all nodes returned by
240 * __cds_wfs_pop_all().
241 * @head: head of the queue (struct cds_wfs_head pointer).
242 * @node: iterator (struct cds_wfs_node pointer).
243 *
244 * Content written into each node before enqueue is guaranteed to be
245 * consistent, but no other memory ordering is ensured.
246 */
247#define cds_wfs_for_each_blocking(head, node) \
c7ba06ba 248 for (node = cds_wfs_first(head); \
edac6b69
MD
249 node != NULL; \
250 node = cds_wfs_next_blocking(node))
251
252/*
253 * cds_wfs_for_each_blocking_safe: Iterate over all nodes returned by
254 * __cds_wfs_pop_all(). Safe against deletion.
255 * @head: head of the queue (struct cds_wfs_head pointer).
256 * @node: iterator (struct cds_wfs_node pointer).
257 * @n: struct cds_wfs_node pointer holding the next pointer (used
258 * internally).
259 *
260 * Content written into each node before enqueue is guaranteed to be
261 * consistent, but no other memory ordering is ensured.
262 */
263#define cds_wfs_for_each_blocking_safe(head, node, n) \
c7ba06ba 264 for (node = cds_wfs_first(head), \
edac6b69
MD
265 n = (node ? cds_wfs_next_blocking(node) : NULL); \
266 node != NULL; \
267 node = n, n = (node ? cds_wfs_next_blocking(node) : NULL))
268
cb3f3d6b 269#endif /* _URCU_WFSTACK_H */
This page took 0.036086 seconds and 4 git commands to generate.