Remove debug yield statements
[urcu.git] / urcu.h
CommitLineData
27b012e2
MD
1#ifndef _URCU_H
2#define _URCU_H
3
b257a10b
MD
4/*
5 * urcu.h
6 *
7 * Userspace RCU header
8 *
9 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
10 *
5e7e64b9
MD
11 * Credits for Paul e. McKenney <paulmck@linux.vnet.ibm.com>
12 * for inspiration coming from the Linux kernel RCU and rcu-preempt.
13 *
14 * The barrier, mb, rmb, wmb, atomic_inc, smp_read_barrier_depends, ACCESS_ONCE
15 * and rcu_dereference primitives come from the Linux kernel.
16 *
b257a10b
MD
17 * Distributed under GPLv2
18 */
19
1430ee0b 20#include <stdlib.h>
69a757c9 21#include <pthread.h>
1430ee0b 22
27b012e2
MD
23/* The "volatile" is due to gcc bugs */
24#define barrier() __asm__ __volatile__("": : :"memory")
25
5b1da0c8
MD
26#define likely(x) __builtin_expect(!!(x), 1)
27#define unlikely(x) __builtin_expect(!!(x), 0)
28
27b012e2
MD
29/* x86 32/64 specific */
30#define mb() asm volatile("mfence":::"memory")
31#define rmb() asm volatile("lfence":::"memory")
32#define wmb() asm volatile("sfence" ::: "memory")
33
b715b99e
MD
34/* Assume SMP machine, given we don't have this information */
35#define CONFIG_SMP 1
36
37#ifdef CONFIG_SMP
38#define smp_mb() mb()
39#define smp_rmb() rmb()
40#define smp_wmb() wmb()
41#else
42#define smp_mb() barrier()
43#define smp_rmb() barrier()
44#define smp_wmb() barrier()
45#endif
46
27b012e2
MD
47static inline void atomic_inc(int *v)
48{
49 asm volatile("lock; incl %0"
f69f195a 50 : "+m" (*v));
27b012e2
MD
51}
52
f4a486ac
MD
53#define xchg(ptr, v) \
54 ((__typeof__(*(ptr)))__xchg((unsigned long)(v), (ptr), sizeof(*(ptr))))
55
56struct __xchg_dummy {
57 unsigned long a[100];
58};
59#define __xg(x) ((struct __xchg_dummy *)(x))
60
61/*
62 * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
63 * Note 2: xchg has side effect, so that attribute volatile is necessary,
64 * but generally the primitive is invalid, *ptr is output argument. --ANK
65 */
66static inline unsigned long __xchg(unsigned long x, volatile void *ptr,
67 int size)
68{
69 switch (size) {
70 case 1:
71 asm volatile("xchgb %b0,%1"
72 : "=q" (x)
73 : "m" (*__xg(ptr)), "0" (x)
74 : "memory");
75 break;
76 case 2:
77 asm volatile("xchgw %w0,%1"
78 : "=r" (x)
79 : "m" (*__xg(ptr)), "0" (x)
80 : "memory");
81 break;
82 case 4:
5b1da0c8
MD
83 asm volatile("xchgl %k0,%1"
84 : "=r" (x)
85 : "m" (*__xg(ptr)), "0" (x)
86 : "memory");
87 break;
88 case 8:
89 asm volatile("xchgq %0,%1"
f4a486ac
MD
90 : "=r" (x)
91 : "m" (*__xg(ptr)), "0" (x)
92 : "memory");
93 break;
94 }
95 return x;
96}
97
27b012e2
MD
98/* Nop everywhere except on alpha. */
99#define smp_read_barrier_depends()
100
41718ff9
MD
101/*
102 * Prevent the compiler from merging or refetching accesses. The compiler
103 * is also forbidden from reordering successive instances of ACCESS_ONCE(),
104 * but only when the compiler is aware of some particular ordering. One way
105 * to make the compiler aware of ordering is to put the two invocations of
106 * ACCESS_ONCE() in different C statements.
107 *
108 * This macro does absolutely -nothing- to prevent the CPU from reordering,
109 * merging, or refetching absolutely anything at any time. Its main intended
110 * use is to mediate communication between process-level code and irq/NMI
111 * handlers, all running on the same CPU.
112 */
113#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
114
115/**
116 * rcu_dereference - fetch an RCU-protected pointer in an
117 * RCU read-side critical section. This pointer may later
118 * be safely dereferenced.
119 *
120 * Inserts memory barriers on architectures that require them
121 * (currently only the Alpha), and, more importantly, documents
122 * exactly which pointers are protected by RCU.
123 */
124
125#define rcu_dereference(p) ({ \
126 typeof(p) _________p1 = ACCESS_ONCE(p); \
127 smp_read_barrier_depends(); \
128 (_________p1); \
129 })
130
27b012e2
MD
131#define SIGURCU SIGUSR1
132
cf380c2f
MD
133#ifdef DEBUG_YIELD
134#include <sched.h>
9b171f46
MD
135#include <time.h>
136#include <pthread.h>
bb488185 137#include <unistd.h>
cf380c2f
MD
138
139#define YIELD_READ (1 << 0)
140#define YIELD_WRITE (1 << 1)
141
bb488185
MD
142/* Updates without DEBUG_FULL_MB are much slower. Account this in the delay */
143#ifdef DEBUG_FULL_MB
144/* maximum sleep delay, in us */
145#define MAX_SLEEP 50
146#else
147#define MAX_SLEEP 30000
148#endif
149
9d335088
MD
150extern unsigned int yield_active;
151extern unsigned int __thread rand_yield;
cf380c2f
MD
152
153static inline void debug_yield_read(void)
154{
155 if (yield_active & YIELD_READ)
9d335088 156 if (rand_r(&rand_yield) & 0x1)
bb488185 157 usleep(rand_r(&rand_yield) % MAX_SLEEP);
cf380c2f
MD
158}
159
160static inline void debug_yield_write(void)
161{
162 if (yield_active & YIELD_WRITE)
9d335088 163 if (rand_r(&rand_yield) & 0x1)
bb488185 164 usleep(rand_r(&rand_yield) % MAX_SLEEP);
9d335088
MD
165}
166
167static inline void debug_yield_init(void)
168{
169 rand_yield = time(NULL) ^ pthread_self();
cf380c2f
MD
170}
171#else
172static inline void debug_yield_read(void)
173{
174}
175
176static inline void debug_yield_write(void)
177{
9d335088
MD
178}
179
180static inline void debug_yield_init(void)
181{
182
cf380c2f
MD
183}
184#endif
185
bb488185
MD
186#ifdef DEBUG_FULL_MB
187static inline void read_barrier()
188{
b715b99e 189 smp_mb();
bb488185
MD
190}
191#else
192static inline void read_barrier()
193{
194 barrier();
195}
196#endif
197
1430ee0b 198/*
4917a879
MD
199 * The trick here is that RCU_GP_CTR_BIT must be a multiple of 8 so we can use a
200 * full 8-bits, 16-bits or 32-bits bitmask for the lower order bits.
1430ee0b 201 */
6e32665b 202#define RCU_GP_COUNT (1UL << 0)
4917a879 203/* Use the amount of bits equal to half of the architecture long size */
6e32665b 204#define RCU_GP_CTR_BIT (1UL << (sizeof(long) << 2))
1430ee0b
MD
205#define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1)
206
5b1da0c8
MD
207/*
208 * Global quiescent period counter with low-order bits unused.
209 * Using a int rather than a char to eliminate false register dependencies
210 * causing stalls on some architectures.
211 */
6e8b8429 212extern long urcu_gp_ctr;
27b012e2 213
6e8b8429 214extern long __thread urcu_active_readers;
27b012e2 215
128166c9 216static inline int rcu_old_gp_ongoing(long *value)
27b012e2 217{
6e8b8429 218 long v;
1430ee0b
MD
219
220 if (value == NULL)
221 return 0;
9598a481
MD
222 /*
223 * Make sure both tests below are done on the same version of *value
224 * to insure consistency.
225 */
1430ee0b 226 v = ACCESS_ONCE(*value);
1430ee0b 227 return (v & RCU_GP_CTR_NEST_MASK) &&
9598a481 228 ((v ^ urcu_gp_ctr) & RCU_GP_CTR_BIT);
27b012e2
MD
229}
230
1430ee0b 231static inline void rcu_read_lock(void)
27b012e2 232{
6e8b8429 233 long tmp;
1430ee0b 234
1430ee0b 235 tmp = urcu_active_readers;
3a9e6e9d 236 /* urcu_gp_ctr = RCU_GP_COUNT | (~RCU_GP_CTR_BIT or RCU_GP_CTR_BIT) */
5b1da0c8 237 if (likely(!(tmp & RCU_GP_CTR_NEST_MASK)))
128166c9 238 urcu_active_readers = urcu_gp_ctr;
1430ee0b
MD
239 else
240 urcu_active_readers = tmp + RCU_GP_COUNT;
27b012e2
MD
241 /*
242 * Increment active readers count before accessing the pointer.
243 * See force_mb_all_threads().
244 */
bb488185 245 read_barrier();
27b012e2
MD
246}
247
1430ee0b 248static inline void rcu_read_unlock(void)
27b012e2 249{
bb488185 250 read_barrier();
27b012e2
MD
251 /*
252 * Finish using rcu before decrementing the pointer.
253 * See force_mb_all_threads().
254 */
1430ee0b 255 urcu_active_readers -= RCU_GP_COUNT;
27b012e2
MD
256}
257
e462817e
MD
258/**
259 * rcu_assign_pointer - assign (publicize) a pointer to a newly
260 * initialized structure that will be dereferenced by RCU read-side
261 * critical sections. Returns the value assigned.
262 *
263 * Inserts memory barriers on architectures that require them
264 * (pretty much all of them other than x86), and also prevents
265 * the compiler from reordering the code that initializes the
266 * structure after the pointer assignment. More importantly, this
267 * call documents which pointers will be dereferenced by RCU read-side
268 * code.
269 */
270
271#define rcu_assign_pointer(p, v) \
272 ({ \
273 if (!__builtin_constant_p(v) || \
274 ((v) != NULL)) \
275 wmb(); \
276 (p) = (v); \
277 })
278
f4a486ac
MD
279#define rcu_xchg_pointer(p, v) \
280 ({ \
281 if (!__builtin_constant_p(v) || \
282 ((v) != NULL)) \
283 wmb(); \
284 xchg(p, v); \
285 })
286
e462817e 287extern void synchronize_rcu(void);
27b012e2 288
f4a486ac
MD
289/*
290 * Exchanges the pointer and waits for quiescent state.
291 * The pointer returned can be freed.
292 */
293#define urcu_publish_content(p, v) \
294 ({ \
295 void *oldptr; \
f4a486ac
MD
296 oldptr = rcu_xchg_pointer(p, v); \
297 synchronize_rcu(); \
298 oldptr; \
299 })
300
27b012e2
MD
301/*
302 * Reader thread registration.
303 */
304extern void urcu_register_thread(void);
5e7e64b9 305extern void urcu_unregister_thread(void);
27b012e2
MD
306
307#endif /* _URCU_H */
This page took 0.052566 seconds and 4 git commands to generate.