lfstack: Implement mutex-free stack head with transparent union (v2)
[urcu.git] / 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
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 */
59 struct cds_lfs_node {
60 struct cds_lfs_node *next;
61 };
62
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 */
69 struct cds_lfs_head {
70 struct cds_lfs_node node;
71 };
72
73 struct __cds_lfs_stack {
74 struct cds_lfs_head *head;
75 };
76
77 struct cds_lfs_stack {
78 struct cds_lfs_head *head;
79 pthread_mutex_t lock;
80 };
81
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 */
87 typedef union __attribute__((__transparent_union__)) {
88 struct __cds_lfs_stack *_s;
89 struct cds_lfs_stack *s;
90 } cds_lfs_stack_ptr_t;
91
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
98 #define cds_lfs_empty _cds_lfs_empty
99 #define cds_lfs_push _cds_lfs_push
100
101 /* Locking performed internally */
102 #define cds_lfs_pop_blocking _cds_lfs_pop_blocking
103 #define cds_lfs_pop_all_blocking _cds_lfs_pop_all_blocking
104
105 /* Synchronize pop with internal mutex */
106 #define cds_lfs_pop_lock _cds_lfs_pop_lock
107 #define cds_lfs_pop_unlock _cds_lfs_pop_unlock
108
109 /* Synchronization ensured by the caller. See synchronization table. */
110 #define __cds_lfs_pop ___cds_lfs_pop
111 #define __cds_lfs_pop_all ___cds_lfs_pop_all
112
113 #else /* !_LGPL_SOURCE */
114
115 /*
116 * cds_lfs_node_init: initialize lock-free stack node.
117 */
118 extern void cds_lfs_node_init(struct cds_lfs_node *node);
119
120 /*
121 * cds_lfs_init: initialize lock-free stack.
122 */
123 extern void cds_lfs_init(struct cds_lfs_stack *s);
124
125 /*
126 * __cds_lfs_init: initialize lock-free stack.
127 */
128 extern void __cds_lfs_init(struct __cds_lfs_stack *s);
129
130 /*
131 * cds_lfs_empty: return whether lock-free stack is empty.
132 *
133 * No memory barrier is issued. No mutual exclusion is required.
134 */
135 extern bool cds_lfs_empty(cds_lfs_stack_ptr_t s);
136
137 /*
138 * cds_lfs_push: push a node into the stack.
139 *
140 * Does not require any synchronization with other push nor pop.
141 *
142 * Returns 0 if the stack was empty prior to adding the node.
143 * Returns non-zero otherwise.
144 */
145 extern bool cds_lfs_push(cds_lfs_stack_ptr_t s,
146 struct cds_lfs_node *node);
147
148 /*
149 * cds_lfs_pop_blocking: pop a node from the stack.
150 *
151 * Calls __cds_lfs_pop with an internal pop mutex held.
152 */
153 extern struct cds_lfs_node *cds_lfs_pop_blocking(struct cds_lfs_stack *s);
154
155 /*
156 * cds_lfs_pop_all_blocking: pop all nodes from a stack.
157 *
158 * Calls __cds_lfs_pop_all with an internal pop mutex held.
159 */
160 extern struct cds_lfs_head *cds_lfs_pop_all_blocking(struct cds_lfs_stack *s);
161
162 /*
163 * cds_lfs_pop_lock: lock stack pop-protection mutex.
164 */
165 extern void cds_lfs_pop_lock(struct cds_lfs_stack *s);
166
167 /*
168 * cds_lfs_pop_unlock: unlock stack pop-protection mutex.
169 */
170 extern void cds_lfs_pop_unlock(struct cds_lfs_stack *s);
171
172 /*
173 * __cds_lfs_pop: pop a node from the stack.
174 *
175 * Returns NULL if stack is empty.
176 *
177 * __cds_lfs_pop needs to be synchronized using one of the following
178 * techniques:
179 *
180 * 1) Calling __cds_lfs_pop under rcu read lock critical section. The
181 * caller must wait for a grace period to pass before freeing the
182 * returned node or modifying the cds_lfs_node structure.
183 * 2) Using mutual exclusion (e.g. mutexes) to protect __cds_lfs_pop
184 * and __cds_lfs_pop_all callers.
185 * 3) Ensuring that only ONE thread can call __cds_lfs_pop() and
186 * __cds_lfs_pop_all(). (multi-provider/single-consumer scheme).
187 */
188 extern struct cds_lfs_node *__cds_lfs_pop(cds_lfs_stack_ptr_t s);
189
190 /*
191 * __cds_lfs_pop_all: pop all nodes from a stack.
192 *
193 * __cds_lfs_pop_all does not require any synchronization with other
194 * push, nor with other __cds_lfs_pop_all, but requires synchronization
195 * matching the technique used to synchronize __cds_lfs_pop:
196 *
197 * 1) If __cds_lfs_pop is called under rcu read lock critical section,
198 * both __cds_lfs_pop and cds_lfs_pop_all callers must wait for a
199 * grace period to pass before freeing the returned node or modifying
200 * the cds_lfs_node structure. However, no RCU read-side critical
201 * section is needed around __cds_lfs_pop_all.
202 * 2) Using mutual exclusion (e.g. mutexes) to protect __cds_lfs_pop and
203 * __cds_lfs_pop_all callers.
204 * 3) Ensuring that only ONE thread can call __cds_lfs_pop() and
205 * __cds_lfs_pop_all(). (multi-provider/single-consumer scheme).
206 */
207 extern struct cds_lfs_head *__cds_lfs_pop_all(cds_lfs_stack_ptr_t s);
208
209 #endif /* !_LGPL_SOURCE */
210
211 /*
212 * cds_lfs_for_each: Iterate over all nodes returned by
213 * __cds_lfs_pop_all.
214 * @__head: node returned by __cds_lfs_pop_all (struct cds_lfs_head pointer).
215 * @__node: node to use as iterator (struct cds_lfs_node pointer).
216 *
217 * Content written into each node before push is guaranteed to be
218 * consistent, but no other memory ordering is ensured.
219 */
220 #define cds_lfs_for_each(__head, __node) \
221 for (__node = &__head->node; \
222 __node != NULL; \
223 __node = __node->next)
224
225 /*
226 * cds_lfs_for_each_safe: Iterate over all nodes returned by
227 * __cds_lfs_pop_all, safe against node deletion.
228 * @__head: node returned by __cds_lfs_pop_all (struct cds_lfs_head pointer).
229 * @__node: node to use as iterator (struct cds_lfs_node pointer).
230 * @__n: struct cds_lfs_node pointer holding the next pointer (used
231 * internally).
232 *
233 * Content written into each node before push is guaranteed to be
234 * consistent, but no other memory ordering is ensured.
235 */
236 #define cds_lfs_for_each_safe(__head, __node, __n) \
237 for (__node = &__head->node, __n = (__node ? __node->next : NULL); \
238 __node != NULL; \
239 __node = __n, __n = (__node ? __node->next : NULL))
240
241 #ifdef __cplusplus
242 }
243 #endif
244
245 #endif /* _URCU_LFSTACK_H */
This page took 0.049843 seconds and 4 git commands to generate.