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