rculfhash: remove duplicated code
[urcu.git] / urcu / static / lfstack.h
1 #ifndef _URCU_STATIC_LFSTACK_H
2 #define _URCU_STATIC_LFSTACK_H
3
4 /*
5 * urcu/static/lfstack.h
6 *
7 * Userspace RCU library - Lock-Free Stack
8 *
9 * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See urcu/lfstack.h for
12 * linking dynamically with the userspace rcu library.
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29 #include <stdbool.h>
30 #include <pthread.h>
31 #include <assert.h>
32 #include <urcu/uatomic.h>
33 #include <urcu-pointer.h>
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 /*
40 * Lock-free stack.
41 *
42 * Stack implementing push, pop, pop_all operations, as well as iterator
43 * on the stack 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_lfs_push __cds_lfs_pop __cds_lfs_pop_all
52 * cds_lfs_push - - -
53 * __cds_lfs_pop - X X
54 * __cds_lfs_pop_all - X -
55 *
56 * cds_lfs_pop_blocking and cds_lfs_pop_all_blocking use an internal
57 * mutex to provide synchronization.
58 */
59
60 /*
61 * cds_lfs_node_init: initialize lock-free stack node.
62 */
63 static inline
64 void _cds_lfs_node_init(struct cds_lfs_node *node)
65 {
66 }
67
68 /*
69 * cds_lfs_init: initialize lock-free stack.
70 */
71 static inline
72 void _cds_lfs_init(struct cds_lfs_stack *s)
73 {
74 int ret;
75
76 s->head = NULL;
77 ret = pthread_mutex_init(&s->lock, NULL);
78 assert(!ret);
79 }
80
81 static inline
82 bool ___cds_lfs_empty_head(struct cds_lfs_head *head)
83 {
84 return head == NULL;
85 }
86
87 /*
88 * cds_lfs_empty: return whether lock-free stack is empty.
89 *
90 * No memory barrier is issued. No mutual exclusion is required.
91 */
92 static inline
93 bool _cds_lfs_empty(struct cds_lfs_stack *s)
94 {
95 return ___cds_lfs_empty_head(CMM_LOAD_SHARED(s->head));
96 }
97
98 /*
99 * cds_lfs_push: push a node into the stack.
100 *
101 * Does not require any synchronization with other push nor pop.
102 *
103 * Lock-free stack push is not subject to ABA problem, so no need to
104 * take the RCU read-side lock. Even if "head" changes between two
105 * uatomic_cmpxchg() invocations here (being popped, and then pushed
106 * again by one or more concurrent threads), the second
107 * uatomic_cmpxchg() invocation only cares about pushing a new entry at
108 * the head of the stack, ensuring consistency by making sure the new
109 * node->next is the same pointer value as the value replaced as head.
110 * It does not care about the content of the actual next node, so it can
111 * very well be reallocated between the two uatomic_cmpxchg().
112 *
113 * We take the approach of expecting the stack to be usually empty, so
114 * we first try an initial uatomic_cmpxchg() on a NULL old_head, and
115 * retry if the old head was non-NULL (the value read by the first
116 * uatomic_cmpxchg() is used as old head for the following loop). The
117 * upside of this scheme is to minimize the amount of cacheline traffic,
118 * always performing an exclusive cacheline access, rather than doing
119 * non-exclusive followed by exclusive cacheline access (which would be
120 * required if we first read the old head value). This design decision
121 * might be revisited after more thorough benchmarking on various
122 * platforms.
123 *
124 * Returns 0 if the stack was empty prior to adding the node.
125 * Returns non-zero otherwise.
126 */
127 static inline
128 bool _cds_lfs_push(struct cds_lfs_stack *s,
129 struct cds_lfs_node *node)
130 {
131 struct cds_lfs_head *head = NULL;
132 struct cds_lfs_head *new_head =
133 caa_container_of(node, struct cds_lfs_head, node);
134
135 for (;;) {
136 struct cds_lfs_head *old_head = head;
137
138 /*
139 * node->next is still private at this point, no need to
140 * perform a _CMM_STORE_SHARED().
141 */
142 node->next = &head->node;
143 /*
144 * uatomic_cmpxchg() implicit memory barrier orders earlier
145 * stores to node before publication.
146 */
147 head = uatomic_cmpxchg(&s->head, old_head, new_head);
148 if (old_head == head)
149 break;
150 }
151 return ___cds_lfs_empty_head(head);
152 }
153
154 /*
155 * __cds_lfs_pop: pop a node from the stack.
156 *
157 * Returns NULL if stack is empty.
158 *
159 * __cds_lfs_pop needs to be synchronized using one of the following
160 * techniques:
161 *
162 * 1) Calling __cds_lfs_pop under rcu read lock critical section. The
163 * caller must wait for a grace period to pass before freeing the
164 * returned node or modifying the cds_lfs_node structure.
165 * 2) Using mutual exclusion (e.g. mutexes) to protect __cds_lfs_pop
166 * and __cds_lfs_pop_all callers.
167 * 3) Ensuring that only ONE thread can call __cds_lfs_pop() and
168 * __cds_lfs_pop_all(). (multi-provider/single-consumer scheme).
169 */
170 static inline
171 struct cds_lfs_node *___cds_lfs_pop(struct cds_lfs_stack *s)
172 {
173 for (;;) {
174 struct cds_lfs_head *head, *next_head;
175 struct cds_lfs_node *next;
176
177 head = _CMM_LOAD_SHARED(s->head);
178 if (___cds_lfs_empty_head(head))
179 return NULL; /* Empty stack */
180
181 /*
182 * Read head before head->next. Matches the implicit
183 * memory barrier before uatomic_cmpxchg() in
184 * cds_lfs_push.
185 */
186 cmm_smp_read_barrier_depends();
187 next = _CMM_LOAD_SHARED(head->node.next);
188 next_head = caa_container_of(next,
189 struct cds_lfs_head, node);
190 if (uatomic_cmpxchg(&s->head, head, next_head) == head)
191 return &head->node;
192 /* busy-loop if head changed under us */
193 }
194 }
195
196 /*
197 * __cds_lfs_pop_all: pop all nodes from a stack.
198 *
199 * __cds_lfs_pop_all does not require any synchronization with other
200 * push, nor with other __cds_lfs_pop_all, but requires synchronization
201 * matching the technique used to synchronize __cds_lfs_pop:
202 *
203 * 1) If __cds_lfs_pop is called under rcu read lock critical section,
204 * both __cds_lfs_pop and cds_lfs_pop_all callers must wait for a
205 * grace period to pass before freeing the returned node or modifying
206 * the cds_lfs_node structure. However, no RCU read-side critical
207 * section is needed around __cds_lfs_pop_all.
208 * 2) Using mutual exclusion (e.g. mutexes) to protect __cds_lfs_pop and
209 * __cds_lfs_pop_all callers.
210 * 3) Ensuring that only ONE thread can call __cds_lfs_pop() and
211 * __cds_lfs_pop_all(). (multi-provider/single-consumer scheme).
212 */
213 static inline
214 struct cds_lfs_head *___cds_lfs_pop_all(struct cds_lfs_stack *s)
215 {
216 /*
217 * Implicit memory barrier after uatomic_xchg() matches implicit
218 * memory barrier before uatomic_cmpxchg() in cds_lfs_push. It
219 * ensures that all nodes of the returned list are consistent.
220 * There is no need to issue memory barriers when iterating on
221 * the returned list, because the full memory barrier issued
222 * prior to each uatomic_cmpxchg, which each write to head, are
223 * taking care to order writes to each node prior to the full
224 * memory barrier after this uatomic_xchg().
225 */
226 return uatomic_xchg(&s->head, NULL);
227 }
228
229 /*
230 * cds_lfs_pop_lock: lock stack pop-protection mutex.
231 */
232 static inline void _cds_lfs_pop_lock(struct cds_lfs_stack *s)
233 {
234 int ret;
235
236 ret = pthread_mutex_lock(&s->lock);
237 assert(!ret);
238 }
239
240 /*
241 * cds_lfs_pop_unlock: unlock stack pop-protection mutex.
242 */
243 static inline void _cds_lfs_pop_unlock(struct cds_lfs_stack *s)
244 {
245 int ret;
246
247 ret = pthread_mutex_unlock(&s->lock);
248 assert(!ret);
249 }
250
251 /*
252 * Call __cds_lfs_pop with an internal pop mutex held.
253 */
254 static inline
255 struct cds_lfs_node *
256 _cds_lfs_pop_blocking(struct cds_lfs_stack *s)
257 {
258 struct cds_lfs_node *retnode;
259
260 _cds_lfs_pop_lock(s);
261 retnode = ___cds_lfs_pop(s);
262 _cds_lfs_pop_unlock(s);
263 return retnode;
264 }
265
266 /*
267 * Call __cds_lfs_pop_all with an internal pop mutex held.
268 */
269 static inline
270 struct cds_lfs_head *
271 _cds_lfs_pop_all_blocking(struct cds_lfs_stack *s)
272 {
273 struct cds_lfs_head *rethead;
274
275 _cds_lfs_pop_lock(s);
276 rethead = ___cds_lfs_pop_all(s);
277 _cds_lfs_pop_unlock(s);
278 return rethead;
279 }
280
281 #ifdef __cplusplus
282 }
283 #endif
284
285 #endif /* _URCU_STATIC_LFSTACK_H */
This page took 0.051994 seconds and 4 git commands to generate.