lfstack: implement C++ API based on function overloading
[urcu.git] / include / urcu / lfstack.h
CommitLineData
d3bfcb24
MD
1#ifndef _URCU_LFSTACK_H
2#define _URCU_LFSTACK_H
3
4/*
5 * lfstack.h
6 *
7 * Userspace RCU library - Lock-Free Stack
8 *
9 * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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#ifdef __cplusplus
27extern "C" {
28#endif
29
7294103b
MD
30#include <stdbool.h>
31#include <pthread.h>
28757437 32#include <urcu/compiler.h>
7294103b
MD
33
34/*
35 * Lock-free stack.
36 *
37 * Stack implementing push, pop, pop_all operations, as well as iterator
38 * on the stack head returned by pop_all.
39 *
40 * Synchronization table:
41 *
42 * External synchronization techniques described in the API below is
43 * required between pairs marked with "X". No external synchronization
44 * required between pairs marked with "-".
45 *
46 * cds_lfs_push __cds_lfs_pop __cds_lfs_pop_all
47 * cds_lfs_push - - -
48 * __cds_lfs_pop - X X
49 * __cds_lfs_pop_all - X -
50 *
51 * cds_lfs_pop_blocking and cds_lfs_pop_all_blocking use an internal
52 * mutex to provide synchronization.
53 */
54
55/*
56 * struct cds_lfs_node is returned by cds_lfs_pop, and also used as
57 * iterator on stack. It is not safe to dereference the node next
58 * pointer when returned by cds_lfs_pop.
59 */
d3bfcb24
MD
60struct cds_lfs_node {
61 struct cds_lfs_node *next;
62};
63
7294103b
MD
64/*
65 * struct cds_lfs_head is returned by __cds_lfs_pop_all, and can be used
66 * to begin iteration on the stack. "node" needs to be the first field
67 * of cds_lfs_head, so the end-of-stack pointer value can be used for
68 * both types.
69 */
70struct cds_lfs_head {
71 struct cds_lfs_node node;
72};
73
48a8832b
MD
74struct __cds_lfs_stack {
75 struct cds_lfs_head *head;
76};
77
d3bfcb24 78struct cds_lfs_stack {
7294103b
MD
79 struct cds_lfs_head *head;
80 pthread_mutex_t lock;
d3bfcb24
MD
81};
82
48a8832b 83/*
e9349020
MD
84 * In C, the transparent union allows calling functions that work on
85 * both struct cds_lfs_stack and struct __cds_lfs_stack on any of those
86 * two types.
28757437 87 *
e9349020
MD
88 * In C++, implement static inline wrappers using function overloading
89 * to obtain an API similar to C.
48a8832b 90 */
5135c0fd 91typedef union {
48a8832b
MD
92 struct __cds_lfs_stack *_s;
93 struct cds_lfs_stack *s;
28757437 94} caa_c_transparent_union cds_lfs_stack_ptr_t;
48a8832b 95
d3bfcb24
MD
96#ifdef _LGPL_SOURCE
97
98#include <urcu/static/lfstack.h>
99
100#define cds_lfs_node_init _cds_lfs_node_init
101#define cds_lfs_init _cds_lfs_init
200d100e 102#define cds_lfs_destroy _cds_lfs_destroy
817a0178 103#define __cds_lfs_init ___cds_lfs_init
7294103b 104#define cds_lfs_empty _cds_lfs_empty
d3bfcb24 105#define cds_lfs_push _cds_lfs_push
7294103b
MD
106
107/* Locking performed internally */
108#define cds_lfs_pop_blocking _cds_lfs_pop_blocking
109#define cds_lfs_pop_all_blocking _cds_lfs_pop_all_blocking
110
111/* Synchronize pop with internal mutex */
112#define cds_lfs_pop_lock _cds_lfs_pop_lock
113#define cds_lfs_pop_unlock _cds_lfs_pop_unlock
114
115/* Synchronization ensured by the caller. See synchronization table. */
116#define __cds_lfs_pop ___cds_lfs_pop
117#define __cds_lfs_pop_all ___cds_lfs_pop_all
d3bfcb24
MD
118
119#else /* !_LGPL_SOURCE */
120
7294103b
MD
121/*
122 * cds_lfs_node_init: initialize lock-free stack node.
123 */
d3bfcb24 124extern void cds_lfs_node_init(struct cds_lfs_node *node);
7294103b
MD
125
126/*
200d100e
MD
127 * cds_lfs_init: initialize lock-free stack (with locking). Pair with
128 * cds_lfs_destroy().
7294103b 129 */
d3bfcb24
MD
130extern void cds_lfs_init(struct cds_lfs_stack *s);
131
48a8832b 132/*
200d100e
MD
133 * cds_lfs_destroy: destroy lock-free stack (with lock). Pair with
134 * cds_lfs_init().
135 */
136extern void cds_lfs_destroy(struct cds_lfs_stack *s);
137
138/*
139 * __cds_lfs_init: initialize lock-free stack (without lock).
140 * Don't pair with any destroy function.
48a8832b
MD
141 */
142extern void __cds_lfs_init(struct __cds_lfs_stack *s);
143
7294103b
MD
144/*
145 * cds_lfs_empty: return whether lock-free stack is empty.
146 *
147 * No memory barrier is issued. No mutual exclusion is required.
148 */
48a8832b 149extern bool cds_lfs_empty(cds_lfs_stack_ptr_t s);
7294103b 150
d3bfcb24
MD
151/*
152 * cds_lfs_push: push a node into the stack.
153 *
154 * Does not require any synchronization with other push nor pop.
155 *
156 * Returns 0 if the stack was empty prior to adding the node.
157 * Returns non-zero otherwise.
158 */
48a8832b 159extern bool cds_lfs_push(cds_lfs_stack_ptr_t s,
d3bfcb24
MD
160 struct cds_lfs_node *node);
161
162/*
7294103b
MD
163 * cds_lfs_pop_blocking: pop a node from the stack.
164 *
165 * Calls __cds_lfs_pop with an internal pop mutex held.
166 */
167extern struct cds_lfs_node *cds_lfs_pop_blocking(struct cds_lfs_stack *s);
168
169/*
170 * cds_lfs_pop_all_blocking: pop all nodes from a stack.
171 *
172 * Calls __cds_lfs_pop_all with an internal pop mutex held.
173 */
174extern struct cds_lfs_head *cds_lfs_pop_all_blocking(struct cds_lfs_stack *s);
175
176/*
177 * cds_lfs_pop_lock: lock stack pop-protection mutex.
178 */
179extern void cds_lfs_pop_lock(struct cds_lfs_stack *s);
180
181/*
182 * cds_lfs_pop_unlock: unlock stack pop-protection mutex.
183 */
184extern void cds_lfs_pop_unlock(struct cds_lfs_stack *s);
185
186/*
187 * __cds_lfs_pop: pop a node from the stack.
d3bfcb24
MD
188 *
189 * Returns NULL if stack is empty.
190 *
7294103b 191 * __cds_lfs_pop needs to be synchronized using one of the following
d3bfcb24
MD
192 * techniques:
193 *
f9b5c2b6
MD
194 * 1) Calling __cds_lfs_pop under rcu read lock critical section.
195 * Both __cds_lfs_pop and __cds_lfs_pop_all callers must wait for a
196 * grace period to pass before freeing the returned node or pushing
197 * the node back into the stack. It is valid to overwrite the content
198 * of cds_lfs_node immediately after __cds_lfs_pop and
199 * __cds_lfs_pop_all.
7294103b
MD
200 * 2) Using mutual exclusion (e.g. mutexes) to protect __cds_lfs_pop
201 * and __cds_lfs_pop_all callers.
202 * 3) Ensuring that only ONE thread can call __cds_lfs_pop() and
203 * __cds_lfs_pop_all(). (multi-provider/single-consumer scheme).
d3bfcb24 204 */
48a8832b 205extern struct cds_lfs_node *__cds_lfs_pop(cds_lfs_stack_ptr_t s);
7294103b
MD
206
207/*
208 * __cds_lfs_pop_all: pop all nodes from a stack.
209 *
210 * __cds_lfs_pop_all does not require any synchronization with other
211 * push, nor with other __cds_lfs_pop_all, but requires synchronization
212 * matching the technique used to synchronize __cds_lfs_pop:
213 *
214 * 1) If __cds_lfs_pop is called under rcu read lock critical section,
f9b5c2b6
MD
215 * both __cds_lfs_pop and __cds_lfs_pop_all callers must wait for a
216 * grace period to pass before freeing the returned node or pushing
217 * the node back into the stack. It is valid to overwrite the content
218 * of cds_lfs_node immediately after __cds_lfs_pop and
219 * __cds_lfs_pop_all. No RCU read-side critical section is needed
220 * around __cds_lfs_pop_all.
7294103b
MD
221 * 2) Using mutual exclusion (e.g. mutexes) to protect __cds_lfs_pop and
222 * __cds_lfs_pop_all callers.
223 * 3) Ensuring that only ONE thread can call __cds_lfs_pop() and
224 * __cds_lfs_pop_all(). (multi-provider/single-consumer scheme).
225 */
48a8832b 226extern struct cds_lfs_head *__cds_lfs_pop_all(cds_lfs_stack_ptr_t s);
d3bfcb24
MD
227
228#endif /* !_LGPL_SOURCE */
229
7294103b
MD
230/*
231 * cds_lfs_for_each: Iterate over all nodes returned by
232 * __cds_lfs_pop_all.
233 * @__head: node returned by __cds_lfs_pop_all (struct cds_lfs_head pointer).
234 * @__node: node to use as iterator (struct cds_lfs_node pointer).
235 *
236 * Content written into each node before push is guaranteed to be
237 * consistent, but no other memory ordering is ensured.
238 */
239#define cds_lfs_for_each(__head, __node) \
240 for (__node = &__head->node; \
241 __node != NULL; \
242 __node = __node->next)
243
244/*
245 * cds_lfs_for_each_safe: Iterate over all nodes returned by
246 * __cds_lfs_pop_all, safe against node deletion.
247 * @__head: node returned by __cds_lfs_pop_all (struct cds_lfs_head pointer).
248 * @__node: node to use as iterator (struct cds_lfs_node pointer).
249 * @__n: struct cds_lfs_node pointer holding the next pointer (used
250 * internally).
251 *
252 * Content written into each node before push is guaranteed to be
253 * consistent, but no other memory ordering is ensured.
254 */
255#define cds_lfs_for_each_safe(__head, __node, __n) \
256 for (__node = &__head->node, __n = (__node ? __node->next : NULL); \
257 __node != NULL; \
258 __node = __n, __n = (__node ? __node->next : NULL))
259
d3bfcb24
MD
260#ifdef __cplusplus
261}
e9349020
MD
262
263/*
264 * In C++, implement static inline wrappers using function overloading
265 * to obtain an API similar to C.
266 */
267
268static inline cds_lfs_stack_ptr_t __cds_lfs_stack_cast(struct __cds_lfs_stack *s)
269{
270 cds_lfs_stack_ptr_t ret = {
271 ._s = s,
272 };
273 return ret;
274}
275
276static inline cds_lfs_stack_ptr_t cds_lfs_stack_cast(struct cds_lfs_stack *s)
277{
278 cds_lfs_stack_ptr_t ret = {
279 .s = s,
280 };
281 return ret;
282}
283
284static inline bool cds_lfs_empty(struct __cds_lfs_stack *_s)
285{
286 return cds_lfs_empty(__cds_lfs_stack_cast(_s));
287}
288
289static inline bool cds_lfs_empty(struct cds_lfs_stack *s)
290{
291 return cds_lfs_empty(cds_lfs_stack_cast(s));
292}
293
294static inline bool cds_lfs_push(struct __cds_lfs_stack *s,
295 struct cds_lfs_node *node)
296{
297 return cds_lfs_push(__cds_lfs_stack_cast(s), node);
298}
299
300static inline bool cds_lfs_push(struct cds_lfs_stack *s,
301 struct cds_lfs_node *node)
302{
303 return cds_lfs_push(cds_lfs_stack_cast(s), node);
304}
305
306static inline struct cds_lfs_node *__cds_lfs_pop(struct __cds_lfs_stack *s)
307{
308 return __cds_lfs_pop(__cds_lfs_stack_cast(s));
309}
310
311static inline struct cds_lfs_node *__cds_lfs_pop(struct cds_lfs_stack *s)
312{
313 return __cds_lfs_pop(cds_lfs_stack_cast(s));
314}
315
316static inline struct cds_lfs_head *__cds_lfs_pop_all(struct __cds_lfs_stack *s)
317{
318 return __cds_lfs_pop_all(__cds_lfs_stack_cast(s));
319}
320
321static inline struct cds_lfs_head *__cds_lfs_pop_all(struct cds_lfs_stack *s)
322{
323 return __cds_lfs_pop_all(cds_lfs_stack_cast(s));
324}
325
326#endif /* __cplusplus */
d3bfcb24
MD
327
328#endif /* _URCU_LFSTACK_H */
This page took 0.078607 seconds and 4 git commands to generate.