Add test for rcu_assign_pointer()
[urcu.git] / urcu-static.h
CommitLineData
adcfce54
MD
1#ifndef _URCU_STATIC_H
2#define _URCU_STATIC_H
3
4/*
5 * urcu-static.h
6 *
d2d23035 7 * Userspace RCU header.
adcfce54 8 *
d2d23035
MD
9 * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See urcu.h for linking
10 * dynamically with the userspace rcu library.
adcfce54 11 *
d2d23035
MD
12 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
13 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
adcfce54 14 *
d2d23035
MD
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
adcfce54
MD
28 *
29 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
30 */
31
32#include <stdlib.h>
33#include <pthread.h>
bc6c15bb
MD
34#include <syscall.h>
35#include <unistd.h>
adcfce54 36
ec4e58a3
MD
37#include <urcu/compiler.h>
38#include <urcu/arch.h>
1ccf9cbe 39#include <urcu/arch_uatomic.h>
e3b0cef0 40#include <urcu/list.h>
adcfce54
MD
41
42/*
43 * Identify a shared load. A smp_rmc() or smp_mc() should come before the load.
44 */
45#define _LOAD_SHARED(p) ACCESS_ONCE(p)
46
47/*
48 * Load a data from shared memory, doing a cache flush if required.
49 */
50#define LOAD_SHARED(p) \
51 ({ \
52 smp_rmc(); \
53 _LOAD_SHARED(p); \
54 })
55
56/*
57 * Identify a shared store. A smp_wmc() or smp_mc() should follow the store.
58 */
59#define _STORE_SHARED(x, v) ({ ACCESS_ONCE(x) = (v); })
60
61/*
62 * Store v into x, where x is located in shared memory. Performs the required
63 * cache flush after writing. Returns v.
64 */
65#define STORE_SHARED(x, v) \
66 ({ \
67 _STORE_SHARED(x, v); \
68 smp_wmc(); \
69 (v); \
70 })
71
72/**
73 * _rcu_dereference - reads (copy) a RCU-protected pointer to a local variable
74 * into a RCU read-side critical section. The pointer can later be safely
75 * dereferenced within the critical section.
76 *
77 * This ensures that the pointer copy is invariant thorough the whole critical
78 * section.
79 *
80 * Inserts memory barriers on architectures that require them (currently only
81 * Alpha) and documents which pointers are protected by RCU.
82 *
809f4fde
MD
83 * The compiler memory barrier in LOAD_SHARED() ensures that value-speculative
84 * optimizations (e.g. VSS: Value Speculation Scheduling) does not perform the
85 * data read before the pointer read by speculating the value of the pointer.
86 * Correct ordering is ensured because the pointer is read as a volatile access.
87 * This acts as a global side-effect operation, which forbids reordering of
015c702f
MD
88 * dependent memory operations. Note that such concern about dependency-breaking
89 * optimizations will eventually be taken care of by the "memory_order_consume"
90 * addition to forthcoming C++ standard.
809f4fde 91 *
adcfce54
MD
92 * Should match rcu_assign_pointer() or rcu_xchg_pointer().
93 */
94
95#define _rcu_dereference(p) ({ \
96 typeof(p) _________p1 = LOAD_SHARED(p); \
97 smp_read_barrier_depends(); \
98 (_________p1); \
99 })
100
bc6c15bb
MD
101#define futex(...) syscall(__NR_futex, __VA_ARGS__)
102#define FUTEX_WAIT 0
103#define FUTEX_WAKE 1
104
adcfce54
MD
105/*
106 * This code section can only be included in LGPL 2.1 compatible source code.
107 * See below for the function call wrappers which can be used in code meant to
108 * be only linked with the Userspace RCU library. This comes with a small
109 * performance degradation on the read-side due to the added function calls.
110 * This is required to permit relinking with newer versions of the library.
111 */
112
113/*
114 * The signal number used by the RCU library can be overridden with
115 * -DSIGURCU= when compiling the library.
116 */
117#ifndef SIGURCU
118#define SIGURCU SIGUSR1
119#endif
120
121/*
122 * If a reader is really non-cooperative and refuses to commit its
123 * urcu_active_readers count to memory (there is no barrier in the reader
124 * per-se), kick it after a few loops waiting for it.
125 */
126#define KICK_READER_LOOPS 10000
127
bc6c15bb
MD
128/*
129 * Active attempts to check for reader Q.S. before calling futex().
130 */
131#define RCU_QS_ACTIVE_ATTEMPTS 100
132
7ac06cef
MD
133#ifdef DEBUG_RCU
134#define rcu_assert(args...) assert(args)
135#else
136#define rcu_assert(args...)
137#endif
138
adcfce54
MD
139#ifdef DEBUG_YIELD
140#include <sched.h>
141#include <time.h>
142#include <pthread.h>
143#include <unistd.h>
144
145#define YIELD_READ (1 << 0)
146#define YIELD_WRITE (1 << 1)
147
b4ce1526 148/*
0a1d290b 149 * Updates without URCU_MB are much slower. Account this in
b4ce1526
MD
150 * the delay.
151 */
0a1d290b 152#ifdef URCU_MB
adcfce54
MD
153/* maximum sleep delay, in us */
154#define MAX_SLEEP 50
155#else
156#define MAX_SLEEP 30000
157#endif
158
159extern unsigned int yield_active;
160extern unsigned int __thread rand_yield;
161
162static inline void debug_yield_read(void)
163{
164 if (yield_active & YIELD_READ)
165 if (rand_r(&rand_yield) & 0x1)
166 usleep(rand_r(&rand_yield) % MAX_SLEEP);
167}
168
169static inline void debug_yield_write(void)
170{
171 if (yield_active & YIELD_WRITE)
172 if (rand_r(&rand_yield) & 0x1)
173 usleep(rand_r(&rand_yield) % MAX_SLEEP);
174}
175
176static inline void debug_yield_init(void)
177{
178 rand_yield = time(NULL) ^ pthread_self();
179}
180#else
181static inline void debug_yield_read(void)
182{
183}
184
185static inline void debug_yield_write(void)
186{
187}
188
189static inline void debug_yield_init(void)
190{
191
192}
193#endif
194
0a1d290b 195#ifdef URCU_MB
adcfce54
MD
196static inline void reader_barrier()
197{
198 smp_mb();
199}
200#else
201static inline void reader_barrier()
202{
203 barrier();
204}
205#endif
206
207/*
208 * The trick here is that RCU_GP_CTR_BIT must be a multiple of 8 so we can use a
209 * full 8-bits, 16-bits or 32-bits bitmask for the lower order bits.
210 */
211#define RCU_GP_COUNT (1UL << 0)
212/* Use the amount of bits equal to half of the architecture long size */
213#define RCU_GP_CTR_BIT (1UL << (sizeof(long) << 2))
214#define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1)
215
216/*
217 * Global quiescent period counter with low-order bits unused.
218 * Using a int rather than a char to eliminate false register dependencies
219 * causing stalls on some architectures.
220 */
221extern long urcu_gp_ctr;
222
e3b0cef0 223struct urcu_reader {
bd1a5e15 224 /* Data used by both reader and synchronize_rcu() */
e3b0cef0 225 long ctr;
e3b0cef0 226 char need_mb;
bd1a5e15
MD
227 /* Data used for registry */
228 struct list_head head __attribute__((aligned(CACHE_LINE_SIZE)));
229 pthread_t tid;
e3b0cef0
MD
230};
231
232extern struct urcu_reader __thread urcu_reader;
adcfce54 233
bc6c15bb
MD
234extern int gp_futex;
235
236/*
237 * Wake-up waiting synchronize_rcu(). Called from many concurrent threads.
238 */
239static inline void wake_up_gp(void)
240{
ec4e58a3
MD
241 if (unlikely(uatomic_read(&gp_futex) == -1)) {
242 uatomic_set(&gp_futex, 0);
bc6c15bb
MD
243 futex(&gp_futex, FUTEX_WAKE, 1,
244 NULL, NULL, 0);
245 }
246}
247
adcfce54
MD
248static inline int rcu_old_gp_ongoing(long *value)
249{
250 long v;
251
252 if (value == NULL)
253 return 0;
254 /*
255 * Make sure both tests below are done on the same version of *value
256 * to insure consistency.
257 */
258 v = LOAD_SHARED(*value);
259 return (v & RCU_GP_CTR_NEST_MASK) &&
260 ((v ^ urcu_gp_ctr) & RCU_GP_CTR_BIT);
261}
262
263static inline void _rcu_read_lock(void)
264{
265 long tmp;
266
e3b0cef0 267 tmp = urcu_reader.ctr;
adcfce54 268 /* urcu_gp_ctr = RCU_GP_COUNT | (~RCU_GP_CTR_BIT or RCU_GP_CTR_BIT) */
67ef1a2c 269 if (likely(!(tmp & RCU_GP_CTR_NEST_MASK))) {
e3b0cef0 270 _STORE_SHARED(urcu_reader.ctr, _LOAD_SHARED(urcu_gp_ctr));
67ef1a2c
MD
271 /*
272 * Set active readers count for outermost nesting level before
273 * accessing the pointer. See force_mb_all_threads().
274 */
275 reader_barrier();
276 } else {
e3b0cef0 277 _STORE_SHARED(urcu_reader.ctr, tmp + RCU_GP_COUNT);
67ef1a2c 278 }
adcfce54
MD
279}
280
281static inline void _rcu_read_unlock(void)
282{
bc6c15bb
MD
283 long tmp;
284
e3b0cef0 285 tmp = urcu_reader.ctr;
adcfce54
MD
286 /*
287 * Finish using rcu before decrementing the pointer.
288 * See force_mb_all_threads().
289 */
bc6c15bb
MD
290 if (likely((tmp & RCU_GP_CTR_NEST_MASK) == RCU_GP_COUNT)) {
291 reader_barrier();
e3b0cef0
MD
292 _STORE_SHARED(urcu_reader.ctr, urcu_reader.ctr - RCU_GP_COUNT);
293 /* write urcu_reader.ctr before read futex */
bc6c15bb
MD
294 reader_barrier();
295 wake_up_gp();
296 } else {
e3b0cef0 297 _STORE_SHARED(urcu_reader.ctr, urcu_reader.ctr - RCU_GP_COUNT);
bc6c15bb 298 }
adcfce54
MD
299}
300
301/**
302 * _rcu_assign_pointer - assign (publicize) a pointer to a new data structure
303 * meant to be read by RCU read-side critical sections. Returns the assigned
304 * value.
305 *
306 * Documents which pointers will be dereferenced by RCU read-side critical
307 * sections and adds the required memory barriers on architectures requiring
308 * them. It also makes sure the compiler does not reorder code initializing the
309 * data structure before its publication.
310 *
311 * Should match rcu_dereference_pointer().
312 */
313
314#define _rcu_assign_pointer(p, v) \
315 ({ \
316 if (!__builtin_constant_p(v) || \
317 ((v) != NULL)) \
318 wmb(); \
319 STORE_SHARED(p, v); \
320 })
321
4d1ce26f
MD
322/**
323 * _rcu_cmpxchg_pointer - same as rcu_assign_pointer, but tests if the pointer
324 * is as expected by "old". If succeeds, returns the previous pointer to the
325 * data structure, which can be safely freed after waiting for a quiescent state
326 * using synchronize_rcu(). If fails (unexpected value), returns old (which
327 * should not be freed !).
328 */
329
330#define _rcu_cmpxchg_pointer(p, old, _new) \
331 ({ \
332 if (!__builtin_constant_p(_new) || \
333 ((_new) != NULL)) \
334 wmb(); \
ec4e58a3 335 uatomic_cmpxchg(p, old, _new); \
4d1ce26f
MD
336 })
337
adcfce54
MD
338/**
339 * _rcu_xchg_pointer - same as rcu_assign_pointer, but returns the previous
67ef1a2c 340 * pointer to the data structure, which can be safely freed after waiting for a
adcfce54
MD
341 * quiescent state using synchronize_rcu().
342 */
343
344#define _rcu_xchg_pointer(p, v) \
345 ({ \
346 if (!__builtin_constant_p(v) || \
347 ((v) != NULL)) \
348 wmb(); \
ec4e58a3 349 uatomic_xchg(p, v); \
adcfce54
MD
350 })
351
352/*
353 * Exchanges the pointer and waits for quiescent state.
354 * The pointer returned can be freed.
355 */
356#define _rcu_publish_content(p, v) \
357 ({ \
358 void *oldptr; \
359 oldptr = _rcu_xchg_pointer(p, v); \
360 synchronize_rcu(); \
361 oldptr; \
362 })
363
364#endif /* _URCU_STATIC_H */
This page took 0.037591 seconds and 4 git commands to generate.