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
;
53 * Global grace period counter.
54 * Contains the current RCU_GP_CTR_BIT.
55 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
56 * Written to only by writer with mutex taken. Read by both writer and readers.
58 long urcu_gp_ctr
= RCU_GP_COUNT
;
61 * Written to only by each individual reader. Read by both the reader and the
64 struct urcu_reader_status __thread urcu_reader_status
;
66 /* Thread IDs of registered readers */
67 #define INIT_NUM_THREADS 4
69 struct reader_registry
{
71 struct urcu_reader_status
*urcu_reader_status
;
76 unsigned int yield_active
;
77 unsigned int __thread rand_yield
;
80 static struct reader_registry
*registry
;
81 static char __thread need_mb
;
82 static int num_readers
, alloc_readers
;
84 static void internal_urcu_lock(void)
88 #ifndef DISTRUST_SIGNALS_EXTREME
89 ret
= pthread_mutex_lock(&urcu_mutex
);
91 perror("Error in pthread mutex lock");
94 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
95 while ((ret
= pthread_mutex_trylock(&urcu_mutex
)) != 0) {
96 if (ret
!= EBUSY
&& ret
!= EINTR
) {
97 printf("ret = %d, errno = %d\n", ret
, errno
);
98 perror("Error in pthread mutex lock");
108 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
111 static void internal_urcu_unlock(void)
115 ret
= pthread_mutex_unlock(&urcu_mutex
);
117 perror("Error in pthread mutex unlock");
123 * called with urcu_mutex held.
125 static void switch_next_urcu_qparity(void)
127 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_CTR_BIT
);
131 #ifdef HAS_INCOHERENT_CACHES
132 static void force_mb_single_thread(struct reader_registry
*index
)
136 #endif /* #ifdef HAS_INCOHERENT_CACHES */
138 static void force_mb_all_threads(void)
142 #else /* #ifdef URCU_MB */
143 #ifdef HAS_INCOHERENT_CACHES
144 static void force_mb_single_thread(struct reader_registry
*index
)
148 * pthread_kill has a smp_mb(). But beware, we assume it performs
149 * a cache flush on architectures with non-coherent cache. Let's play
150 * safe and don't assume anything : we use smp_mc() to make sure the
151 * cache flush is enforced.
154 smp_mc(); /* write ->need_mb before sending the signals */
155 pthread_kill(index
->tid
, SIGURCU
);
158 * Wait for sighandler (and thus mb()) to execute on every thread.
161 while (*index
->need_mb
) {
164 smp_mb(); /* read ->need_mb before ending the barrier */
166 #endif /* #ifdef HAS_INCOHERENT_CACHES */
168 static void force_mb_all_threads(void)
170 struct reader_registry
*index
;
172 * Ask for each threads to execute a smp_mb() so we can consider the
173 * compiler barriers around rcu read lock as real memory barriers.
178 * pthread_kill has a smp_mb(). But beware, we assume it performs
179 * a cache flush on architectures with non-coherent cache. Let's play
180 * safe and don't assume anything : we use smp_mc() to make sure the
181 * cache flush is enforced.
183 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
185 smp_mc(); /* write need_mb before sending the signal */
186 pthread_kill(index
->tid
, SIGURCU
);
189 * Wait for sighandler (and thus mb()) to execute on every thread.
191 * Note that the pthread_kill() will never be executed on systems
192 * that correctly deliver signals in a timely manner. However, it
193 * is not uncommon for kernels to have bugs that can result in
194 * lost or unduly delayed signals.
196 * If you are seeing the below pthread_kill() executing much at
197 * all, we suggest testing the underlying kernel and filing the
198 * relevant bug report. For Linux kernels, we recommend getting
199 * the Linux Test Project (LTP).
201 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
202 while (*index
->need_mb
) {
203 pthread_kill(index
->tid
, SIGURCU
);
207 smp_mb(); /* read ->need_mb before ending the barrier */
209 #endif /* #else #ifdef URCU_MB */
211 void wait_for_quiescent_state(void)
213 struct reader_registry
*index
;
218 * Wait for each thread active_readers count to become 0.
220 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
223 if (likely(!rcu_old_gp_ongoing(
224 &index
->urcu_reader_status
->active_readers
)))
227 index
->urcu_reader_status
->gp_waiting
= 1;
228 #ifndef HAS_INCOHERENT_CACHES
229 while (rcu_old_gp_ongoing(
230 &index
->urcu_reader_status
->active_readers
)) {
231 if (wait_loops
++ == RCU_QS_ACTIVE_ATTEMPTS
) {
232 sched_yield(); /* ideally sched_yield_to() */
237 #else /* #ifndef HAS_INCOHERENT_CACHES */
239 * BUSY-LOOP. Force the reader thread to commit its
240 * active_readers update to memory if we wait for too long.
242 while (rcu_old_gp_ongoing(
243 &index
->urcu_reader_status
->active_readers
)) {
244 switch (wait_loops
++) {
245 case RCU_QS_ACTIVE_ATTEMPTS
:
246 sched_yield(); /* ideally sched_yield_to() */
248 case KICK_READER_LOOPS
:
249 force_mb_single_thread(index
);
256 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
257 index
->urcu_reader_status
->gp_waiting
= 0;
261 void synchronize_rcu(void)
263 internal_urcu_lock();
265 /* All threads should read qparity before accessing data structure
266 * where new ptr points to. Must be done within internal_urcu_lock
267 * because it iterates on reader threads.*/
268 /* Write new ptr before changing the qparity */
269 force_mb_all_threads();
271 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_ONGOING
);
273 switch_next_urcu_qparity(); /* 0 -> 1 */
276 * Must commit qparity update to memory before waiting for parity
277 * 0 quiescent state. Failure to do so could result in the writer
278 * waiting forever while new readers are always accessing data (no
280 * Ensured by STORE_SHARED and LOAD_SHARED.
284 * Adding a smp_mb() which is _not_ formally required, but makes the
285 * model easier to understand. It does not have a big performance impact
286 * anyway, given this is the write-side.
291 * Wait for previous parity to be empty of readers.
293 wait_for_quiescent_state(); /* Wait readers in parity 0 */
296 * Must finish waiting for quiescent state for parity 0 before
297 * committing qparity update to memory. Failure to do so could result in
298 * the writer waiting forever while new readers are always accessing
299 * data (no progress).
300 * Ensured by STORE_SHARED and LOAD_SHARED.
304 * Adding a smp_mb() which is _not_ formally required, but makes the
305 * model easier to understand. It does not have a big performance impact
306 * anyway, given this is the write-side.
310 switch_next_urcu_qparity(); /* 1 -> 0 */
313 * Must commit qparity update to memory before waiting for parity
314 * 1 quiescent state. Failure to do so could result in the writer
315 * waiting forever while new readers are always accessing data (no
317 * Ensured by STORE_SHARED and LOAD_SHARED.
321 * Adding a smp_mb() which is _not_ formally required, but makes the
322 * model easier to understand. It does not have a big performance impact
323 * anyway, given this is the write-side.
328 * Wait for previous parity to be empty of readers.
330 wait_for_quiescent_state(); /* Wait readers in parity 1 */
332 STORE_SHARED(urcu_gp_ctr
, urcu_gp_ctr
^ RCU_GP_ONGOING
);
334 /* Finish waiting for reader threads before letting the old ptr being
335 * freed. Must be done within internal_urcu_lock because it iterates on
337 force_mb_all_threads();
339 internal_urcu_unlock();
343 * library wrappers to be used by non-LGPL compatible source code.
346 void rcu_read_lock(void)
351 void rcu_read_unlock(void)
356 void *rcu_dereference(void *p
)
358 return _rcu_dereference(p
);
361 void *rcu_assign_pointer_sym(void **p
, void *v
)
364 return STORE_SHARED(p
, v
);
367 void *rcu_xchg_pointer_sym(void **p
, void *v
)
373 void *rcu_cmpxchg_pointer_sym(void **p
, void *old
, void *_new
)
376 return cmpxchg(p
, old
, _new
);
379 void *rcu_publish_content_sym(void **p
, void *v
)
383 oldptr
= _rcu_xchg_pointer(p
, v
);
388 static void rcu_add_reader(pthread_t id
)
390 struct reader_registry
*oldarray
;
393 alloc_readers
= INIT_NUM_THREADS
;
396 malloc(sizeof(struct reader_registry
) * alloc_readers
);
398 if (alloc_readers
< num_readers
+ 1) {
400 registry
= malloc(sizeof(struct reader_registry
)
401 * (alloc_readers
<< 1));
402 memcpy(registry
, oldarray
,
403 sizeof(struct reader_registry
) * alloc_readers
);
407 registry
[num_readers
].tid
= id
;
408 /* reference to the TLS of _this_ reader thread. */
409 registry
[num_readers
].urcu_reader_status
= &urcu_reader_status
;
410 registry
[num_readers
].need_mb
= &need_mb
;
415 * Never shrink (implementation limitation).
416 * This is O(nb threads). Eventually use a hash table.
418 static void rcu_remove_reader(pthread_t id
)
420 struct reader_registry
*index
;
422 assert(registry
!= NULL
);
423 for (index
= registry
; index
< registry
+ num_readers
; index
++) {
424 if (pthread_equal(index
->tid
, id
)) {
425 memcpy(index
, ®istry
[num_readers
- 1],
426 sizeof(struct reader_registry
));
427 registry
[num_readers
- 1].tid
= 0;
428 registry
[num_readers
- 1].urcu_reader_status
= NULL
;
433 /* Hrm not found, forgot to register ? */
437 void rcu_register_thread(void)
439 internal_urcu_lock();
440 urcu_init(); /* In case gcc does not support constructor attribute */
441 rcu_add_reader(pthread_self());
442 internal_urcu_unlock();
445 void rcu_unregister_thread(void)
447 internal_urcu_lock();
448 rcu_remove_reader(pthread_self());
449 internal_urcu_unlock();
453 static void sigurcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
456 * Executing this smp_mb() is the only purpose of this signal handler.
457 * It punctually promotes barrier() into smp_mb() on every thread it is
466 * urcu_init constructor. Called when the library is linked, but also when
467 * reader threads are calling rcu_register_thread().
468 * Should only be called by a single thread at a given time. This is ensured by
469 * holing the internal_urcu_lock() from rcu_register_thread() or by running at
470 * library load time, which should not be executed by multiple threads nor
471 * concurrently with rcu_register_thread() anyway.
475 struct sigaction act
;
482 act
.sa_sigaction
= sigurcu_handler
;
483 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
484 sigemptyset(&act
.sa_mask
);
485 ret
= sigaction(SIGURCU
, &act
, NULL
);
487 perror("Error in sigaction");
494 struct sigaction act
;
497 ret
= sigaction(SIGURCU
, NULL
, &act
);
499 perror("Error in sigaction");
502 assert(act
.sa_sigaction
== sigurcu_handler
);
505 #endif /* #ifndef URCU_MB */