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