4 * Userspace RCU library
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
35 #include "urcu-static.h"
36 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
42 void __attribute__((constructor
)) urcu_init(void);
43 void __attribute__((destructor
)) urcu_exit(void);
50 static pthread_mutex_t urcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
55 * Global grace period counter.
56 * Contains the current RCU_GP_CTR_BIT.
57 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
58 * Written to only by writer with mutex taken. Read by both writer and readers.
60 long urcu_gp_ctr
= RCU_GP_COUNT
;
63 * Written to only by each individual reader. Read by both the reader and the
66 long __thread urcu_active_readers
;
68 /* Thread IDs of registered readers */
69 #define INIT_NUM_THREADS 4
71 struct reader_registry
{
73 long *urcu_active_readers
;
78 unsigned int yield_active
;
79 unsigned int __thread rand_yield
;
82 static struct reader_registry
*registry
;
83 static char __thread need_mb
;
84 static int num_readers
, alloc_readers
;
86 static void internal_urcu_lock(void)
90 #ifndef DISTRUST_SIGNALS_EXTREME
91 ret
= pthread_mutex_lock(&urcu_mutex
);
93 perror("Error in pthread mutex lock");
96 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
97 while ((ret
= pthread_mutex_trylock(&urcu_mutex
)) != 0) {
98 if (ret
!= EBUSY
&& ret
!= EINTR
) {
99 printf("ret = %d, errno = %d\n", ret
, errno
);
100 perror("Error in pthread mutex lock");
110 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
113 static void internal_urcu_unlock(void)
117 ret
= pthread_mutex_unlock(&urcu_mutex
);
119 perror("Error in pthread mutex unlock");
125 * called with urcu_mutex held.
127 static void switch_next_urcu_qparity(void)
129 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_CTR_BIT
);
133 static void force_mb_single_thread(struct reader_registry
*index
)
138 static void force_mb_all_threads(void)
142 #else /* #ifdef URCU_MB */
143 static void force_mb_single_thread(struct reader_registry
*index
)
147 * pthread_kill has a smp_mb(). But beware, we assume it performs
148 * a cache flush on architectures with non-coherent cache. Let's play
149 * safe and don't assume anything : we use smp_mc() to make sure the
150 * cache flush is enforced.
153 smp_mc(); /* write ->need_mb before sending the signals */
154 pthread_kill(index
->tid
, SIGURCU
);
157 * Wait for sighandler (and thus mb()) to execute on every thread.
160 while (*index
->need_mb
) {
163 smp_mb(); /* read ->need_mb before ending the barrier */
166 static void force_mb_all_threads(void)
168 struct reader_registry
*index
;
170 * Ask for each threads to execute a smp_mb() so we can consider the
171 * compiler barriers around rcu read lock as real memory barriers.
176 * pthread_kill has a smp_mb(). But beware, we assume it performs
177 * a cache flush on architectures with non-coherent cache. Let's play
178 * safe and don't assume anything : we use smp_mc() to make sure the
179 * cache flush is enforced.
181 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
183 smp_mc(); /* write need_mb before sending the signal */
184 pthread_kill(index
->tid
, SIGURCU
);
187 * Wait for sighandler (and thus mb()) to execute on every thread.
189 * Note that the pthread_kill() will never be executed on systems
190 * that correctly deliver signals in a timely manner. However, it
191 * is not uncommon for kernels to have bugs that can result in
192 * lost or unduly delayed signals.
194 * If you are seeing the below pthread_kill() executing much at
195 * all, we suggest testing the underlying kernel and filing the
196 * relevant bug report. For Linux kernels, we recommend getting
197 * the Linux Test Project (LTP).
199 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
200 while (*index
->need_mb
) {
201 pthread_kill(index
->tid
, SIGURCU
);
205 smp_mb(); /* read ->need_mb before ending the barrier */
207 #endif /* #else #ifdef URCU_MB */
210 * synchronize_rcu() waiting. Single thread.
212 static void wait_gp(struct reader_registry
*index
)
214 atomic_dec(&gp_futex
);
215 force_mb_single_thread(index
); /* Write futex before read reader_gp */
216 if (!rcu_old_gp_ongoing(index
->urcu_active_readers
)) {
217 /* Read reader_gp before write futex */
218 force_mb_single_thread(index
);
219 /* Callbacks are queued, don't wait. */
220 atomic_set(&gp_futex
, 0);
222 /* Read reader_gp before read futex */
223 force_mb_single_thread(index
);
224 if (atomic_read(&gp_futex
) == -1)
225 futex(&gp_futex
, FUTEX_WAIT
, -1,
230 void wait_for_quiescent_state(void)
232 struct reader_registry
*index
;
237 * Wait for each thread urcu_active_readers count to become 0.
239 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
241 #ifndef HAS_INCOHERENT_CACHES
242 while (rcu_old_gp_ongoing(index
->urcu_active_readers
)) {
243 if (wait_loops
++ == RCU_QS_ACTIVE_ATTEMPTS
) {
249 #else /* #ifndef HAS_INCOHERENT_CACHES */
251 * BUSY-LOOP. Force the reader thread to commit its
252 * urcu_active_readers update to memory if we wait for too long.
254 while (rcu_old_gp_ongoing(index
->urcu_active_readers
)) {
255 switch (wait_loops
++) {
256 case RCU_QS_ACTIVE_ATTEMPTS
:
259 case KICK_READER_LOOPS
:
260 force_mb_single_thread(index
);
267 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
271 void synchronize_rcu(void)
273 internal_urcu_lock();
275 /* All threads should read qparity before accessing data structure
276 * where new ptr points to. Must be done within internal_urcu_lock
277 * because it iterates on reader threads.*/
278 /* Write new ptr before changing the qparity */
279 force_mb_all_threads();
281 switch_next_urcu_qparity(); /* 0 -> 1 */
284 * Must commit qparity update to memory before waiting for parity
285 * 0 quiescent state. Failure to do so could result in the writer
286 * waiting forever while new readers are always accessing data (no
288 * Ensured by STORE_SHARED and LOAD_SHARED.
292 * Adding a smp_mb() which is _not_ formally required, but makes the
293 * model easier to understand. It does not have a big performance impact
294 * anyway, given this is the write-side.
299 * Wait for previous parity to be empty of readers.
301 wait_for_quiescent_state(); /* Wait readers in parity 0 */
304 * Must finish waiting for quiescent state for parity 0 before
305 * committing qparity update to memory. Failure to do so could result in
306 * the writer waiting forever while new readers are always accessing
307 * data (no progress).
308 * Ensured by STORE_SHARED and LOAD_SHARED.
312 * Adding a smp_mb() which is _not_ formally required, but makes the
313 * model easier to understand. It does not have a big performance impact
314 * anyway, given this is the write-side.
318 switch_next_urcu_qparity(); /* 1 -> 0 */
321 * Must commit qparity update to memory before waiting for parity
322 * 1 quiescent state. Failure to do so could result in the writer
323 * waiting forever while new readers are always accessing data (no
325 * Ensured by STORE_SHARED and LOAD_SHARED.
329 * Adding a smp_mb() which is _not_ formally required, but makes the
330 * model easier to understand. It does not have a big performance impact
331 * anyway, given this is the write-side.
336 * Wait for previous parity to be empty of readers.
338 wait_for_quiescent_state(); /* Wait readers in parity 1 */
340 /* Finish waiting for reader threads before letting the old ptr being
341 * freed. Must be done within internal_urcu_lock because it iterates on
343 force_mb_all_threads();
345 internal_urcu_unlock();
349 * library wrappers to be used by non-LGPL compatible source code.
352 void rcu_read_lock(void)
357 void rcu_read_unlock(void)
362 void *rcu_dereference(void *p
)
364 return _rcu_dereference(p
);
367 void *rcu_assign_pointer_sym(void **p
, void *v
)
370 return STORE_SHARED(p
, v
);
373 void *rcu_xchg_pointer_sym(void **p
, void *v
)
379 void *rcu_cmpxchg_pointer_sym(void **p
, void *old
, void *_new
)
382 return cmpxchg(p
, old
, _new
);
385 void *rcu_publish_content_sym(void **p
, void *v
)
389 oldptr
= _rcu_xchg_pointer(p
, v
);
394 static void rcu_add_reader(pthread_t id
)
396 struct reader_registry
*oldarray
;
399 alloc_readers
= INIT_NUM_THREADS
;
402 malloc(sizeof(struct reader_registry
) * alloc_readers
);
404 if (alloc_readers
< num_readers
+ 1) {
406 registry
= malloc(sizeof(struct reader_registry
)
407 * (alloc_readers
<< 1));
408 memcpy(registry
, oldarray
,
409 sizeof(struct reader_registry
) * alloc_readers
);
413 registry
[num_readers
].tid
= id
;
414 /* reference to the TLS of _this_ reader thread. */
415 registry
[num_readers
].urcu_active_readers
= &urcu_active_readers
;
416 registry
[num_readers
].need_mb
= &need_mb
;
421 * Never shrink (implementation limitation).
422 * This is O(nb threads). Eventually use a hash table.
424 static void rcu_remove_reader(pthread_t id
)
426 struct reader_registry
*index
;
428 assert(registry
!= NULL
);
429 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
430 if (pthread_equal(index
->tid
, id
)) {
431 memcpy(index
, ®istry
[num_readers
- 1],
432 sizeof(struct reader_registry
));
433 registry
[num_readers
- 1].tid
= 0;
434 registry
[num_readers
- 1].urcu_active_readers
= NULL
;
439 /* Hrm not found, forgot to register ? */
443 void rcu_register_thread(void)
445 internal_urcu_lock();
446 urcu_init(); /* In case gcc does not support constructor attribute */
447 rcu_add_reader(pthread_self());
448 internal_urcu_unlock();
451 void rcu_unregister_thread(void)
453 internal_urcu_lock();
454 rcu_remove_reader(pthread_self());
455 internal_urcu_unlock();
459 static void sigurcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
462 * Executing this smp_mb() is the only purpose of this signal handler.
463 * It punctually promotes barrier() into smp_mb() on every thread it is
472 * urcu_init constructor. Called when the library is linked, but also when
473 * reader threads are calling rcu_register_thread().
474 * Should only be called by a single thread at a given time. This is ensured by
475 * holing the internal_urcu_lock() from rcu_register_thread() or by running at
476 * library load time, which should not be executed by multiple threads nor
477 * concurrently with rcu_register_thread() anyway.
481 struct sigaction act
;
488 act
.sa_sigaction
= sigurcu_handler
;
489 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
490 sigemptyset(&act
.sa_mask
);
491 ret
= sigaction(SIGURCU
, &act
, NULL
);
493 perror("Error in sigaction");
500 struct sigaction act
;
503 ret
= sigaction(SIGURCU
, NULL
, &act
);
505 perror("Error in sigaction");
508 assert(act
.sa_sigaction
== sigurcu_handler
);
511 #endif /* #ifndef URCU_MB */