Commit | Line | Data |
---|---|---|
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 | ||
27b012e2 MD |
20 | /* The "volatile" is due to gcc bugs */ |
21 | #define barrier() __asm__ __volatile__("": : :"memory") | |
22 | ||
23 | /* x86 32/64 specific */ | |
24 | #define mb() asm volatile("mfence":::"memory") | |
25 | #define rmb() asm volatile("lfence":::"memory") | |
26 | #define wmb() asm volatile("sfence" ::: "memory") | |
27 | ||
27b012e2 MD |
28 | static inline void atomic_inc(int *v) |
29 | { | |
30 | asm volatile("lock; incl %0" | |
f69f195a | 31 | : "+m" (*v)); |
27b012e2 MD |
32 | } |
33 | ||
34 | /* Nop everywhere except on alpha. */ | |
35 | #define smp_read_barrier_depends() | |
36 | ||
41718ff9 MD |
37 | /* |
38 | * Prevent the compiler from merging or refetching accesses. The compiler | |
39 | * is also forbidden from reordering successive instances of ACCESS_ONCE(), | |
40 | * but only when the compiler is aware of some particular ordering. One way | |
41 | * to make the compiler aware of ordering is to put the two invocations of | |
42 | * ACCESS_ONCE() in different C statements. | |
43 | * | |
44 | * This macro does absolutely -nothing- to prevent the CPU from reordering, | |
45 | * merging, or refetching absolutely anything at any time. Its main intended | |
46 | * use is to mediate communication between process-level code and irq/NMI | |
47 | * handlers, all running on the same CPU. | |
48 | */ | |
49 | #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x)) | |
50 | ||
51 | /** | |
52 | * rcu_dereference - fetch an RCU-protected pointer in an | |
53 | * RCU read-side critical section. This pointer may later | |
54 | * be safely dereferenced. | |
55 | * | |
56 | * Inserts memory barriers on architectures that require them | |
57 | * (currently only the Alpha), and, more importantly, documents | |
58 | * exactly which pointers are protected by RCU. | |
59 | */ | |
60 | ||
61 | #define rcu_dereference(p) ({ \ | |
62 | typeof(p) _________p1 = ACCESS_ONCE(p); \ | |
63 | smp_read_barrier_depends(); \ | |
64 | (_________p1); \ | |
65 | }) | |
66 | ||
27b012e2 MD |
67 | #define SIGURCU SIGUSR1 |
68 | ||
cf380c2f MD |
69 | #ifdef DEBUG_YIELD |
70 | #include <sched.h> | |
71 | ||
72 | #define YIELD_READ (1 << 0) | |
73 | #define YIELD_WRITE (1 << 1) | |
74 | ||
9d335088 MD |
75 | extern unsigned int yield_active; |
76 | extern unsigned int __thread rand_yield; | |
cf380c2f MD |
77 | |
78 | static inline void debug_yield_read(void) | |
79 | { | |
80 | if (yield_active & YIELD_READ) | |
9d335088 MD |
81 | if (rand_r(&rand_yield) & 0x1) |
82 | sched_yield(); | |
cf380c2f MD |
83 | } |
84 | ||
85 | static inline void debug_yield_write(void) | |
86 | { | |
87 | if (yield_active & YIELD_WRITE) | |
9d335088 MD |
88 | if (rand_r(&rand_yield) & 0x1) |
89 | sched_yield(); | |
90 | } | |
91 | ||
92 | static inline void debug_yield_init(void) | |
93 | { | |
94 | rand_yield = time(NULL) ^ pthread_self(); | |
cf380c2f MD |
95 | } |
96 | #else | |
97 | static inline void debug_yield_read(void) | |
98 | { | |
99 | } | |
100 | ||
101 | static inline void debug_yield_write(void) | |
102 | { | |
9d335088 MD |
103 | } |
104 | ||
105 | static inline void debug_yield_init(void) | |
106 | { | |
107 | ||
cf380c2f MD |
108 | } |
109 | #endif | |
110 | ||
27b012e2 MD |
111 | /* Global quiescent period parity */ |
112 | extern int urcu_qparity; | |
113 | ||
114 | extern int __thread urcu_active_readers[2]; | |
115 | ||
116 | static inline int get_urcu_qparity(void) | |
117 | { | |
118 | return urcu_qparity; | |
119 | } | |
120 | ||
121 | /* | |
c265818b | 122 | * urcu_parity should be declared on the caller's stack. |
27b012e2 | 123 | */ |
c265818b | 124 | static inline void rcu_read_lock(int *urcu_parity) |
27b012e2 | 125 | { |
cf380c2f | 126 | debug_yield_read(); |
c265818b | 127 | *urcu_parity = get_urcu_qparity(); |
cf380c2f | 128 | debug_yield_read(); |
c265818b | 129 | urcu_active_readers[*urcu_parity]++; |
cf380c2f | 130 | debug_yield_read(); |
27b012e2 MD |
131 | /* |
132 | * Increment active readers count before accessing the pointer. | |
133 | * See force_mb_all_threads(). | |
134 | */ | |
135 | barrier(); | |
cf380c2f | 136 | debug_yield_read(); |
27b012e2 MD |
137 | } |
138 | ||
c265818b | 139 | static inline void rcu_read_unlock(int *urcu_parity) |
27b012e2 | 140 | { |
cf380c2f | 141 | debug_yield_read(); |
27b012e2 | 142 | barrier(); |
cf380c2f | 143 | debug_yield_read(); |
27b012e2 MD |
144 | /* |
145 | * Finish using rcu before decrementing the pointer. | |
146 | * See force_mb_all_threads(). | |
147 | */ | |
c265818b | 148 | urcu_active_readers[*urcu_parity]--; |
cf380c2f | 149 | debug_yield_read(); |
27b012e2 MD |
150 | } |
151 | ||
cdcb92bb | 152 | extern void *urcu_publish_content(void **ptr, void *new); |
27b012e2 MD |
153 | |
154 | /* | |
155 | * Reader thread registration. | |
156 | */ | |
157 | extern void urcu_register_thread(void); | |
5e7e64b9 | 158 | extern void urcu_unregister_thread(void); |
27b012e2 MD |
159 | |
160 | #endif /* _URCU_H */ |