9 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
11 * Credits for Paul e. McKenney <paulmck@linux.vnet.ibm.com>
12 * for inspiration coming from the Linux kernel RCU and rcu-preempt.
14 * The barrier, mb, rmb, wmb, atomic_inc, smp_read_barrier_depends, ACCESS_ONCE
15 * and rcu_dereference primitives come from the Linux kernel.
17 * Distributed under GPLv2
23 /* The "volatile" is due to gcc bugs */
24 #define barrier() __asm__ __volatile__("": : :"memory")
26 #define likely(x) __builtin_expect(!!(x), 1)
27 #define unlikely(x) __builtin_expect(!!(x), 0)
30 * Assume the architecture has coherent caches. Blackfin will want this unset.
32 #define CONFIG_HAVE_MEM_COHERENCY 1
34 /* Assume P4 or newer */
35 #define CONFIG_HAVE_FENCE 1
37 /* Assume SMP machine, given we don't have this information */
41 #ifdef CONFIG_HAVE_MEM_COHERENCY
43 * Caches are coherent, no need to flush them.
45 #define mc() barrier()
46 #define rmc() barrier()
47 #define wmc() barrier()
49 #error "The architecture must create its own cache flush primitives"
50 #define mc() arch_cache_flush()
51 #define rmc() arch_cache_flush_read()
52 #define wmc() arch_cache_flush_write()
56 #ifdef CONFIG_HAVE_MEM_COHERENCY
58 /* x86 32/64 specific */
59 #ifdef CONFIG_HAVE_FENCE
60 #define mb() asm volatile("mfence":::"memory")
61 #define rmb() asm volatile("lfence":::"memory")
62 #define wmb() asm volatile("sfence"::: "memory")
65 * Some non-Intel clones support out of order store. wmb() ceases to be a
68 #define mb() asm volatile("lock; addl $0,0(%%esp)":::"memory")
69 #define rmb() asm volatile("lock; addl $0,0(%%esp)":::"memory")
70 #define wmb() asm volatile("lock; addl $0,0(%%esp)"::: "memory")
73 #else /* !CONFIG_HAVE_MEM_COHERENCY */
76 * Without cache coherency, the memory barriers become cache flushes.
82 #endif /* !CONFIG_HAVE_MEM_COHERENCY */
87 #define smp_rmb() rmb()
88 #define smp_wmb() wmb()
90 #define smp_rmc() rmc()
91 #define smp_wmc() wmc()
93 #define smp_mb() barrier()
94 #define smp_rmb() barrier()
95 #define smp_wmb() barrier()
96 #define smp_mc() barrier()
97 #define smp_rmc() barrier()
98 #define smp_wmc() barrier()
101 /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
102 static inline void rep_nop(void)
104 asm volatile("rep; nop" ::: "memory");
107 static inline void cpu_relax(void)
112 static inline void atomic_inc(int *v
)
114 asm volatile("lock; incl %0"
118 #define xchg(ptr, v) \
119 ((__typeof__(*(ptr)))__xchg((unsigned long)(v), (ptr), sizeof(*(ptr))))
121 struct __xchg_dummy
{
122 unsigned long a
[100];
124 #define __xg(x) ((struct __xchg_dummy *)(x))
127 * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
128 * Note 2: xchg has side effect, so that attribute volatile is necessary,
129 * but generally the primitive is invalid, *ptr is output argument. --ANK
130 * x is considered local, ptr is considered remote.
132 static inline unsigned long __xchg(unsigned long x
, volatile void *ptr
,
137 asm volatile("xchgb %b0,%1"
139 : "m" (*__xg(ptr
)), "0" (x
)
143 asm volatile("xchgw %w0,%1"
145 : "m" (*__xg(ptr
)), "0" (x
)
149 asm volatile("xchgl %k0,%1"
151 : "m" (*__xg(ptr
)), "0" (x
)
155 asm volatile("xchgq %0,%1"
157 : "m" (*__xg(ptr
)), "0" (x
)
165 /* Nop everywhere except on alpha. */
166 #define smp_read_barrier_depends()
169 * Prevent the compiler from merging or refetching accesses. The compiler
170 * is also forbidden from reordering successive instances of ACCESS_ONCE(),
171 * but only when the compiler is aware of some particular ordering. One way
172 * to make the compiler aware of ordering is to put the two invocations of
173 * ACCESS_ONCE() in different C statements.
175 * This macro does absolutely -nothing- to prevent the CPU from reordering,
176 * merging, or refetching absolutely anything at any time. Its main intended
177 * use is to mediate communication between process-level code and irq/NMI
178 * handlers, all running on the same CPU.
180 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
183 * Load a data from remote memory, doing a cache flush if required.
185 #define LOAD_REMOTE(p) ({ \
187 typeof(p) _________p1 = ACCESS_ONCE(p); \
192 * Store v into x, where x is located in remote memory. Performs the required
193 * cache flush after writing.
195 #define STORE_REMOTE(x, v) \
202 * rcu_dereference - fetch an RCU-protected pointer in an
203 * RCU read-side critical section. This pointer may later
204 * be safely dereferenced.
206 * Inserts memory barriers on architectures that require them
207 * (currently only the Alpha), and, more importantly, documents
208 * exactly which pointers are protected by RCU.
211 #define rcu_dereference(p) ({ \
212 typeof(p) _________p1 = LOAD_REMOTE(p); \
213 smp_read_barrier_depends(); \
219 #define SIGURCU SIGUSR1
222 * If a reader is really non-cooperative and refuses to commit its
223 * urcu_active_readers count to memory (there is no barrier in the reader
224 * per-se), kick it after a few loops waiting for it.
226 #define KICK_READER_LOOPS 10000
234 #define YIELD_READ (1 << 0)
235 #define YIELD_WRITE (1 << 1)
237 /* Updates without DEBUG_FULL_MB are much slower. Account this in the delay */
239 /* maximum sleep delay, in us */
242 #define MAX_SLEEP 30000
245 extern unsigned int yield_active
;
246 extern unsigned int __thread rand_yield
;
248 static inline void debug_yield_read(void)
250 if (yield_active
& YIELD_READ
)
251 if (rand_r(&rand_yield
) & 0x1)
252 usleep(rand_r(&rand_yield
) % MAX_SLEEP
);
255 static inline void debug_yield_write(void)
257 if (yield_active
& YIELD_WRITE
)
258 if (rand_r(&rand_yield
) & 0x1)
259 usleep(rand_r(&rand_yield
) % MAX_SLEEP
);
262 static inline void debug_yield_init(void)
264 rand_yield
= time(NULL
) ^ pthread_self();
267 static inline void debug_yield_read(void)
271 static inline void debug_yield_write(void)
275 static inline void debug_yield_init(void)
282 static inline void reader_barrier()
287 static inline void reader_barrier()
294 * The trick here is that RCU_GP_CTR_BIT must be a multiple of 8 so we can use a
295 * full 8-bits, 16-bits or 32-bits bitmask for the lower order bits.
297 #define RCU_GP_COUNT (1UL << 0)
298 /* Use the amount of bits equal to half of the architecture long size */
299 #define RCU_GP_CTR_BIT (1UL << (sizeof(long) << 2))
300 #define RCU_GP_CTR_NEST_MASK (RCU_GP_CTR_BIT - 1)
303 * Global quiescent period counter with low-order bits unused.
304 * Using a int rather than a char to eliminate false register dependencies
305 * causing stalls on some architectures.
307 extern long urcu_gp_ctr
;
309 extern long __thread urcu_active_readers
;
311 static inline int rcu_old_gp_ongoing(long *value
)
318 * Make sure both tests below are done on the same version of *value
319 * to insure consistency.
321 v
= LOAD_REMOTE(*value
);
322 return (v
& RCU_GP_CTR_NEST_MASK
) &&
323 ((v
^ urcu_gp_ctr
) & RCU_GP_CTR_BIT
);
326 static inline void rcu_read_lock(void)
330 tmp
= urcu_active_readers
;
331 /* urcu_gp_ctr = RCU_GP_COUNT | (~RCU_GP_CTR_BIT or RCU_GP_CTR_BIT) */
333 * The data dependency "read urcu_gp_ctr, write urcu_active_readers",
334 * serializes those two memory operations. We are not using STORE_REMOTE
335 * and LOAD_REMOTE here (although we should) because the writer will
336 * wake us up with a signal which does a flush in its handler to perform
337 * urcu_gp_ctr re-read and urcu_active_readers commit to main memory.
339 if (likely(!(tmp
& RCU_GP_CTR_NEST_MASK
)))
340 urcu_active_readers
= ACCESS_ONCE(urcu_gp_ctr
);
342 urcu_active_readers
= tmp
+ RCU_GP_COUNT
;
344 * Increment active readers count before accessing the pointer.
345 * See force_mb_all_threads().
350 static inline void rcu_read_unlock(void)
354 * Finish using rcu before decrementing the pointer.
355 * See force_mb_all_threads().
357 urcu_active_readers
-= RCU_GP_COUNT
;
361 * rcu_assign_pointer - assign (publicize) a pointer to a newly
362 * initialized structure that will be dereferenced by RCU read-side
363 * critical sections. Returns the value assigned.
365 * Inserts memory barriers on architectures that require them
366 * (pretty much all of them other than x86), and also prevents
367 * the compiler from reordering the code that initializes the
368 * structure after the pointer assignment. More importantly, this
369 * call documents which pointers will be dereferenced by RCU read-side
373 #define rcu_assign_pointer(p, v) \
375 if (!__builtin_constant_p(v) || \
382 #define rcu_xchg_pointer(p, v) \
384 if (!__builtin_constant_p(v) || \
390 extern void synchronize_rcu(void);
393 * Exchanges the pointer and waits for quiescent state.
394 * The pointer returned can be freed.
396 #define urcu_publish_content(p, v) \
399 oldptr = rcu_xchg_pointer(p, v); \
405 * Reader thread registration.
407 extern void urcu_register_thread(void);
408 extern void urcu_unregister_thread(void);