4 * Userspace RCU library
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
8 * Distributed under GPLv2
21 pthread_mutex_t urcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
24 * Global grace period counter.
25 * Contains the current RCU_GP_CTR_BIT.
26 * Also has a RCU_GP_CTR_BIT of 1, to accelerate the reader fast path.
27 * Written to only by writer with mutex taken. Read by both writer and readers.
29 long urcu_gp_ctr
= RCU_GP_COUNT
;
32 * Written to only by each individual reader. Read by both the reader and the
35 long __thread urcu_active_readers
;
37 /* Thread IDs of registered readers */
38 #define INIT_NUM_THREADS 4
40 struct reader_registry
{
42 long *urcu_active_readers
;
47 unsigned int yield_active
;
48 unsigned int __thread rand_yield
;
51 static struct reader_registry
*registry
;
52 static char __thread need_mb
;
53 static int num_readers
, alloc_readers
;
55 void internal_urcu_lock(void)
59 #ifndef DISTRUST_SIGNALS_EXTREME
60 ret
= pthread_mutex_lock(&urcu_mutex
);
62 perror("Error in pthread mutex lock");
65 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
66 while ((ret
= pthread_mutex_trylock(&urcu_mutex
)) != 0) {
67 if (ret
!= EBUSY
&& ret
!= EINTR
) {
68 printf("ret = %d, errno = %d\n", ret
, errno
);
69 perror("Error in pthread mutex lock");
79 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
82 void internal_urcu_unlock(void)
86 ret
= pthread_mutex_unlock(&urcu_mutex
);
88 perror("Error in pthread mutex unlock");
94 * called with urcu_mutex held.
96 static void switch_next_urcu_qparity(void)
98 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_CTR_BIT
);
102 static void force_mb_single_thread(struct reader_registry
*index
)
107 static void force_mb_all_threads(void)
113 static void force_mb_single_thread(struct reader_registry
*index
)
117 * pthread_kill has a smp_mb(). But beware, we assume it performs
118 * a cache flush on architectures with non-coherent cache. Let's play
119 * safe and don't assume anything : we use smp_mc() to make sure the
120 * cache flush is enforced.
123 smp_mc(); /* write ->need_mb before sending the signals */
124 pthread_kill(index
->tid
, SIGURCU
);
127 * Wait for sighandler (and thus mb()) to execute on every thread.
130 while (*index
->need_mb
) {
133 smp_mb(); /* read ->need_mb before ending the barrier */
136 static void force_mb_all_threads(void)
138 struct reader_registry
*index
;
140 * Ask for each threads to execute a smp_mb() so we can consider the
141 * compiler barriers around rcu read lock as real memory barriers.
146 * pthread_kill has a smp_mb(). But beware, we assume it performs
147 * a cache flush on architectures with non-coherent cache. Let's play
148 * safe and don't assume anything : we use smp_mc() to make sure the
149 * cache flush is enforced.
151 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
153 smp_mc(); /* write need_mb before sending the signal */
154 pthread_kill(index
->tid
, SIGURCU
);
157 * Wait for sighandler (and thus mb()) to execute on every thread.
159 * Note that the pthread_kill() will never be executed on systems
160 * that correctly deliver signals in a timely manner. However, it
161 * is not uncommon for kernels to have bugs that can result in
162 * lost or unduly delayed signals.
164 * If you are seeing the below pthread_kill() executing much at
165 * all, we suggest testing the underlying kernel and filing the
166 * relevant bug report. For Linux kernels, we recommend getting
167 * the Linux Test Project (LTP).
169 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
170 while (*index
->need_mb
) {
171 pthread_kill(index
->tid
, SIGURCU
);
175 smp_mb(); /* read ->need_mb before ending the barrier */
179 void wait_for_quiescent_state(void)
181 struct reader_registry
*index
;
186 * Wait for each thread urcu_active_readers count to become 0.
188 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
191 * BUSY-LOOP. Force the reader thread to commit its
192 * urcu_active_readers update to memory if we wait for too long.
194 while (rcu_old_gp_ongoing(index
->urcu_active_readers
)) {
195 if (wait_loops
++ == KICK_READER_LOOPS
) {
196 force_mb_single_thread(index
);
205 void synchronize_rcu(void)
207 internal_urcu_lock();
209 /* All threads should read qparity before accessing data structure
210 * where new ptr points to. Must be done within internal_urcu_lock
211 * because it iterates on reader threads.*/
212 /* Write new ptr before changing the qparity */
213 force_mb_all_threads();
215 switch_next_urcu_qparity(); /* 0 -> 1 */
218 * Must commit qparity update to memory before waiting for parity
219 * 0 quiescent state. Failure to do so could result in the writer
220 * waiting forever while new readers are always accessing data (no
222 * Ensured by STORE_SHARED and LOAD_SHARED.
226 * Wait for previous parity to be empty of readers.
228 wait_for_quiescent_state(); /* Wait readers in parity 0 */
231 * Must finish waiting for quiescent state for parity 0 before
232 * committing qparity update to memory. Failure to do so could result in
233 * the writer waiting forever while new readers are always accessing
234 * data (no progress).
235 * Ensured by STORE_SHARED and LOAD_SHARED.
238 switch_next_urcu_qparity(); /* 1 -> 0 */
241 * Must commit qparity update to memory before waiting for parity
242 * 1 quiescent state. Failure to do so could result in the writer
243 * waiting forever while new readers are always accessing data (no
245 * Ensured by STORE_SHARED and LOAD_SHARED.
249 * Wait for previous parity to be empty of readers.
251 wait_for_quiescent_state(); /* Wait readers in parity 1 */
253 /* Finish waiting for reader threads before letting the old ptr being
254 * freed. Must be done within internal_urcu_lock because it iterates on
256 force_mb_all_threads();
258 internal_urcu_unlock();
261 void urcu_add_reader(pthread_t id
)
263 struct reader_registry
*oldarray
;
266 alloc_readers
= INIT_NUM_THREADS
;
269 malloc(sizeof(struct reader_registry
) * alloc_readers
);
271 if (alloc_readers
< num_readers
+ 1) {
273 registry
= malloc(sizeof(struct reader_registry
)
274 * (alloc_readers
<< 1));
275 memcpy(registry
, oldarray
,
276 sizeof(struct reader_registry
) * alloc_readers
);
280 registry
[num_readers
].tid
= id
;
281 /* reference to the TLS of _this_ reader thread. */
282 registry
[num_readers
].urcu_active_readers
= &urcu_active_readers
;
283 registry
[num_readers
].need_mb
= &need_mb
;
288 * Never shrink (implementation limitation).
289 * This is O(nb threads). Eventually use a hash table.
291 void urcu_remove_reader(pthread_t id
)
293 struct reader_registry
*index
;
295 assert(registry
!= NULL
);
296 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
297 if (pthread_equal(index
->tid
, id
)) {
298 memcpy(index
, ®istry
[num_readers
- 1],
299 sizeof(struct reader_registry
));
300 registry
[num_readers
- 1].tid
= 0;
301 registry
[num_readers
- 1].urcu_active_readers
= NULL
;
306 /* Hrm not found, forgot to register ? */
310 void urcu_register_thread(void)
312 internal_urcu_lock();
313 urcu_add_reader(pthread_self());
314 internal_urcu_unlock();
317 void urcu_unregister_thread(void)
319 internal_urcu_lock();
320 urcu_remove_reader(pthread_self());
321 internal_urcu_unlock();
324 #ifndef DEBUG_FULL_MB
325 void sigurcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
328 * Executing this smp_mb() is the only purpose of this signal handler.
329 * It punctually promotes barrier() into smp_mb() on every thread it is
337 void __attribute__((constructor
)) urcu_init(void)
339 struct sigaction act
;
342 act
.sa_sigaction
= sigurcu_handler
;
343 ret
= sigaction(SIGURCU
, &act
, NULL
);
345 perror("Error in sigaction");
350 void __attribute__((destructor
)) urcu_exit(void)
352 struct sigaction act
;
355 ret
= sigaction(SIGURCU
, NULL
, &act
);
357 perror("Error in sigaction");
360 assert(act
.sa_sigaction
== sigurcu_handler
);