4 * Userspace RCU library
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
28 #define _DEFAULT_SOURCE
40 #include <urcu/arch.h>
41 #include <urcu/wfcqueue.h>
42 #include <urcu/map/urcu.h>
43 #include <urcu/static/urcu.h>
44 #include <urcu/pointer.h>
45 #include <urcu/tls-compat.h>
48 #include "urcu-wait.h"
49 #include "urcu-utils.h"
52 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
54 #include <urcu/urcu.h>
58 * If a reader is really non-cooperative and refuses to commit its
59 * rcu_active_readers count to memory (there is no barrier in the reader
60 * per-se), kick it after 10 loops waiting for it.
62 #define KICK_READER_LOOPS 10
65 * Active attempts to check for reader Q.S. before calling futex().
67 #define RCU_QS_ACTIVE_ATTEMPTS 100
69 /* If the headers do not support membarrier system call, fall back on RCU_MB */
70 #ifdef __NR_membarrier
71 # define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
73 # define membarrier(...) -ENOSYS
77 MEMBARRIER_CMD_QUERY
= 0,
78 MEMBARRIER_CMD_SHARED
= (1 << 0),
79 /* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */
80 /* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */
81 MEMBARRIER_CMD_PRIVATE_EXPEDITED
= (1 << 3),
82 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
= (1 << 4),
87 static int urcu_memb_has_sys_membarrier_private_expedited
;
89 #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
91 * Explicitly initialize to zero because we can't alias a non-static
92 * uninitialized variable.
94 int urcu_memb_has_sys_membarrier
= 0;
95 URCU_ATTR_ALIAS("urcu_memb_has_sys_membarrier")
96 extern int rcu_has_sys_membarrier_memb
;
99 void __attribute__((constructor
)) rcu_init(void);
106 URCU_ATTR_ALIAS(urcu_stringify(rcu_init
))
107 void alias_rcu_init(void);
111 static int init_done
;
113 void __attribute__((constructor
)) rcu_init(void);
114 void __attribute__((destructor
)) rcu_exit(void);
118 * rcu_gp_lock ensures mutual exclusion between threads calling
121 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
123 * rcu_registry_lock ensures mutual exclusion between threads
124 * registering and unregistering themselves to/from the registry, and
125 * with threads reading that registry from synchronize_rcu(). However,
126 * this lock is not held all the way through the completion of awaiting
127 * for the grace period. It is sporadically released between iterations
129 * rcu_registry_lock may nest inside rcu_gp_lock.
131 static pthread_mutex_t rcu_registry_lock
= PTHREAD_MUTEX_INITIALIZER
;
132 struct urcu_gp rcu_gp
= { .ctr
= URCU_GP_COUNT
};
133 URCU_ATTR_ALIAS(urcu_stringify(rcu_gp
))
134 extern struct urcu_gp alias_rcu_gp
;
137 * Written to only by each individual reader. Read by both the reader and the
140 DEFINE_URCU_TLS(struct urcu_reader
, rcu_reader
);
141 DEFINE_URCU_TLS_ALIAS(struct urcu_reader
, rcu_reader
, alias_rcu_reader
);
143 static CDS_LIST_HEAD(registry
);
146 * Queue keeping threads awaiting to wait for a grace period. Contains
147 * struct gp_waiters_thread objects.
149 static DEFINE_URCU_WAIT_QUEUE(gp_waiters
);
151 static void mutex_lock(pthread_mutex_t
*mutex
)
155 #ifndef DISTRUST_SIGNALS_EXTREME
156 ret
= pthread_mutex_lock(mutex
);
159 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
160 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
161 if (ret
!= EBUSY
&& ret
!= EINTR
)
163 if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader
).need_mb
)) {
165 _CMM_STORE_SHARED(URCU_TLS(rcu_reader
).need_mb
, 0);
168 (void) poll(NULL
, 0, 10);
170 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
173 static void mutex_unlock(pthread_mutex_t
*mutex
)
177 ret
= pthread_mutex_unlock(mutex
);
182 #ifdef RCU_MEMBARRIER
183 static void smp_mb_master(void)
185 if (caa_likely(urcu_memb_has_sys_membarrier
)) {
186 if (membarrier(urcu_memb_has_sys_membarrier_private_expedited
?
187 MEMBARRIER_CMD_PRIVATE_EXPEDITED
:
188 MEMBARRIER_CMD_SHARED
, 0))
197 static void smp_mb_master(void)
204 static void force_mb_all_readers(void)
206 struct urcu_reader
*index
;
209 * Ask for each threads to execute a cmm_smp_mb() so we can consider the
210 * compiler barriers around rcu read lock as real memory barriers.
212 if (cds_list_empty(®istry
))
215 * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs
216 * a cache flush on architectures with non-coherent cache. Let's play
217 * safe and don't assume anything : we use cmm_smp_mc() to make sure the
218 * cache flush is enforced.
220 cds_list_for_each_entry(index
, ®istry
, node
) {
221 CMM_STORE_SHARED(index
->need_mb
, 1);
222 pthread_kill(index
->tid
, SIGRCU
);
225 * Wait for sighandler (and thus mb()) to execute on every thread.
227 * Note that the pthread_kill() will never be executed on systems
228 * that correctly deliver signals in a timely manner. However, it
229 * is not uncommon for kernels to have bugs that can result in
230 * lost or unduly delayed signals.
232 * If you are seeing the below pthread_kill() executing much at
233 * all, we suggest testing the underlying kernel and filing the
234 * relevant bug report. For Linux kernels, we recommend getting
235 * the Linux Test Project (LTP).
237 cds_list_for_each_entry(index
, ®istry
, node
) {
238 while (CMM_LOAD_SHARED(index
->need_mb
)) {
239 pthread_kill(index
->tid
, SIGRCU
);
240 (void) poll(NULL
, 0, 1);
243 cmm_smp_mb(); /* read ->need_mb before ending the barrier */
246 static void smp_mb_master(void)
248 force_mb_all_readers();
250 #endif /* #ifdef RCU_SIGNAL */
253 * synchronize_rcu() waiting. Single thread.
254 * Always called with rcu_registry lock held. Releases this lock and
255 * grabs it again. Holds the lock when it returns.
257 static void wait_gp(void)
260 * Read reader_gp before read futex. smp_mb_master() needs to
261 * be called with the rcu registry lock held in RCU_SIGNAL
265 /* Temporarily unlock the registry lock. */
266 mutex_unlock(&rcu_registry_lock
);
267 if (uatomic_read(&rcu_gp
.futex
) != -1)
269 while (futex_async(&rcu_gp
.futex
, FUTEX_WAIT
, -1,
273 /* Value already changed. */
276 /* Retry if interrupted by signal. */
277 break; /* Get out of switch. */
279 /* Unexpected error. */
285 * Re-lock the registry lock before the next loop.
287 mutex_lock(&rcu_registry_lock
);
291 * Always called with rcu_registry lock held. Releases this lock between
292 * iterations and grabs it again. Holds the lock when it returns.
294 static void wait_for_readers(struct cds_list_head
*input_readers
,
295 struct cds_list_head
*cur_snap_readers
,
296 struct cds_list_head
*qsreaders
)
298 unsigned int wait_loops
= 0;
299 struct urcu_reader
*index
, *tmp
;
300 #ifdef HAS_INCOHERENT_CACHES
301 unsigned int wait_gp_loops
= 0;
302 #endif /* HAS_INCOHERENT_CACHES */
305 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
306 * indicate quiescence (not nested), or observe the current
310 if (wait_loops
< RCU_QS_ACTIVE_ATTEMPTS
)
312 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
313 uatomic_dec(&rcu_gp
.futex
);
314 /* Write futex before read reader_gp */
318 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
319 switch (urcu_common_reader_state(&rcu_gp
, &index
->ctr
)) {
320 case URCU_READER_ACTIVE_CURRENT
:
321 if (cur_snap_readers
) {
322 cds_list_move(&index
->node
,
327 case URCU_READER_INACTIVE
:
328 cds_list_move(&index
->node
, qsreaders
);
330 case URCU_READER_ACTIVE_OLD
:
332 * Old snapshot. Leaving node in
333 * input_readers will make us busy-loop
334 * until the snapshot becomes current or
335 * the reader becomes inactive.
341 #ifndef HAS_INCOHERENT_CACHES
342 if (cds_list_empty(input_readers
)) {
343 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
344 /* Read reader_gp before write futex */
346 uatomic_set(&rcu_gp
.futex
, 0);
350 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
351 /* wait_gp unlocks/locks registry lock. */
354 /* Temporarily unlock the registry lock. */
355 mutex_unlock(&rcu_registry_lock
);
358 * Re-lock the registry lock before the
361 mutex_lock(&rcu_registry_lock
);
364 #else /* #ifndef HAS_INCOHERENT_CACHES */
366 * BUSY-LOOP. Force the reader thread to commit its
367 * URCU_TLS(rcu_reader).ctr update to memory if we wait
370 if (cds_list_empty(input_readers
)) {
371 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
372 /* Read reader_gp before write futex */
374 uatomic_set(&rcu_gp
.futex
, 0);
378 if (wait_gp_loops
== KICK_READER_LOOPS
) {
382 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
383 /* wait_gp unlocks/locks registry lock. */
387 /* Temporarily unlock the registry lock. */
388 mutex_unlock(&rcu_registry_lock
);
391 * Re-lock the registry lock before the
394 mutex_lock(&rcu_registry_lock
);
397 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
401 void synchronize_rcu(void)
403 CDS_LIST_HEAD(cur_snap_readers
);
404 CDS_LIST_HEAD(qsreaders
);
405 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
406 struct urcu_waiters waiters
;
409 * Add ourself to gp_waiters queue of threads awaiting to wait
410 * for a grace period. Proceed to perform the grace period only
411 * if we are the first thread added into the queue.
412 * The implicit memory barrier before urcu_wait_add()
413 * orders prior memory accesses of threads put into the wait
414 * queue before their insertion into the wait queue.
416 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
417 /* Not first in queue: will be awakened by another thread. */
418 urcu_adaptative_busy_wait(&wait
);
419 /* Order following memory accesses after grace period. */
423 /* We won't need to wake ourself up */
424 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
426 mutex_lock(&rcu_gp_lock
);
429 * Move all waiters into our local queue.
431 urcu_move_waiters(&waiters
, &gp_waiters
);
433 mutex_lock(&rcu_registry_lock
);
435 if (cds_list_empty(®istry
))
439 * All threads should read qparity before accessing data structure
440 * where new ptr points to. Must be done within rcu_registry_lock
441 * because it iterates on reader threads.
443 /* Write new ptr before changing the qparity */
447 * Wait for readers to observe original parity or be quiescent.
448 * wait_for_readers() can release and grab again rcu_registry_lock
451 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
454 * Must finish waiting for quiescent state for original parity before
455 * committing next rcu_gp.ctr update to memory. Failure to do so could
456 * result in the writer waiting forever while new readers are always
457 * accessing data (no progress). Enforce compiler-order of load
458 * URCU_TLS(rcu_reader).ctr before store to rcu_gp.ctr.
463 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
464 * model easier to understand. It does not have a big performance impact
465 * anyway, given this is the write-side.
469 /* Switch parity: 0 -> 1, 1 -> 0 */
470 CMM_STORE_SHARED(rcu_gp
.ctr
, rcu_gp
.ctr
^ URCU_GP_CTR_PHASE
);
473 * Must commit rcu_gp.ctr update to memory before waiting for quiescent
474 * state. Failure to do so could result in the writer waiting forever
475 * while new readers are always accessing data (no progress). Enforce
476 * compiler-order of store to rcu_gp.ctr before load rcu_reader ctr.
482 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
483 * model easier to understand. It does not have a big performance impact
484 * anyway, given this is the write-side.
489 * Wait for readers to observe new parity or be quiescent.
490 * wait_for_readers() can release and grab again rcu_registry_lock
493 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
496 * Put quiescent reader list back into registry.
498 cds_list_splice(&qsreaders
, ®istry
);
501 * Finish waiting for reader threads before letting the old ptr
502 * being freed. Must be done within rcu_registry_lock because it
503 * iterates on reader threads.
507 mutex_unlock(&rcu_registry_lock
);
508 mutex_unlock(&rcu_gp_lock
);
511 * Wakeup waiters only after we have completed the grace period
512 * and have ensured the memory barriers at the end of the grace
513 * period have been issued.
515 urcu_wake_all_waiters(&waiters
);
517 URCU_ATTR_ALIAS(urcu_stringify(synchronize_rcu
))
518 void alias_synchronize_rcu();
521 * library wrappers to be used by non-LGPL compatible source code.
524 void rcu_read_lock(void)
528 URCU_ATTR_ALIAS(urcu_stringify(rcu_read_lock
))
529 void alias_rcu_read_lock();
531 void rcu_read_unlock(void)
535 URCU_ATTR_ALIAS(urcu_stringify(rcu_read_unlock
))
536 void alias_rcu_read_unlock();
538 int rcu_read_ongoing(void)
540 return _rcu_read_ongoing();
542 URCU_ATTR_ALIAS(urcu_stringify(rcu_read_ongoing
))
543 void alias_rcu_read_ongoing();
545 void rcu_register_thread(void)
547 URCU_TLS(rcu_reader
).tid
= pthread_self();
548 assert(URCU_TLS(rcu_reader
).need_mb
== 0);
549 assert(!(URCU_TLS(rcu_reader
).ctr
& URCU_GP_CTR_NEST_MASK
));
551 mutex_lock(&rcu_registry_lock
);
552 assert(!URCU_TLS(rcu_reader
).registered
);
553 URCU_TLS(rcu_reader
).registered
= 1;
554 rcu_init(); /* In case gcc does not support constructor attribute */
555 cds_list_add(&URCU_TLS(rcu_reader
).node
, ®istry
);
556 mutex_unlock(&rcu_registry_lock
);
558 URCU_ATTR_ALIAS(urcu_stringify(rcu_register_thread
))
559 void alias_rcu_register_thread();
561 void rcu_unregister_thread(void)
563 mutex_lock(&rcu_registry_lock
);
564 assert(URCU_TLS(rcu_reader
).registered
);
565 URCU_TLS(rcu_reader
).registered
= 0;
566 cds_list_del(&URCU_TLS(rcu_reader
).node
);
567 mutex_unlock(&rcu_registry_lock
);
569 URCU_ATTR_ALIAS(urcu_stringify(rcu_unregister_thread
))
570 void alias_rcu_unregister_thread();
572 #ifdef RCU_MEMBARRIER
574 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
576 void rcu_sys_membarrier_status(bool available
)
583 void rcu_sys_membarrier_status(bool available
)
587 urcu_memb_has_sys_membarrier
= 1;
592 void rcu_sys_membarrier_init(void)
594 bool available
= false;
597 mask
= membarrier(MEMBARRIER_CMD_QUERY
, 0);
599 if (mask
& MEMBARRIER_CMD_PRIVATE_EXPEDITED
) {
600 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
, 0))
602 urcu_memb_has_sys_membarrier_private_expedited
= 1;
604 } else if (mask
& MEMBARRIER_CMD_SHARED
) {
608 rcu_sys_membarrier_status(available
);
616 rcu_sys_membarrier_init();
618 URCU_ATTR_ALIAS(urcu_stringify(rcu_init
))
619 void alias_rcu_init(void);
623 static void sigrcu_handler(int signo
, siginfo_t
*siginfo
, void *context
)
626 * Executing this cmm_smp_mb() is the only purpose of this signal handler.
627 * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is
631 _CMM_STORE_SHARED(URCU_TLS(rcu_reader
).need_mb
, 0);
636 * rcu_init constructor. Called when the library is linked, but also when
637 * reader threads are calling rcu_register_thread().
638 * Should only be called by a single thread at a given time. This is ensured by
639 * holing the rcu_registry_lock from rcu_register_thread() or by running
640 * at library load time, which should not be executed by multiple
641 * threads nor concurrently with rcu_register_thread() anyway.
645 struct sigaction act
;
652 act
.sa_sigaction
= sigrcu_handler
;
653 act
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
654 sigemptyset(&act
.sa_mask
);
655 ret
= sigaction(SIGRCU
, &act
, NULL
);
659 URCU_ATTR_ALIAS(urcu_stringify(rcu_init
))
660 void alias_rcu_init(void);
665 * Don't unregister the SIGRCU signal handler anymore, because
666 * call_rcu threads could still be using it shortly before the
668 * Assertion disabled because call_rcu threads are now rcu
669 * readers, and left running at exit.
670 * assert(cds_list_empty(®istry));
673 URCU_ATTR_ALIAS(urcu_stringify(rcu_exit
))
674 void alias_rcu_exit(void);
676 #endif /* #ifdef RCU_SIGNAL */
678 DEFINE_RCU_FLAVOR(rcu_flavor
);
679 DEFINE_RCU_FLAVOR_ALIAS(rcu_flavor
, alias_rcu_flavor
);
681 #include "urcu-call-rcu-impl.h"
682 #include "urcu-defer-impl.h"