X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Furcu.c;h=c60307e15351241dbb0158b5d48d589852fcca65;hb=90f72b8c054ca4e3b4b45f630549c784e26ce79a;hp=2c9e5d1da18f531393cd603d6c6d33fa57461a2b;hpb=a0ec6d9b6b67e37fc81c98e04008907bc500cde8;p=urcu.git diff --git a/src/urcu.c b/src/urcu.c index 2c9e5d1..c60307e 100644 --- a/src/urcu.c +++ b/src/urcu.c @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include @@ -39,6 +38,7 @@ #include #include +#include #include #include #include @@ -109,9 +109,13 @@ void rcu_init(void) static int init_done; void __attribute__((constructor)) rcu_init(void); -void __attribute__((destructor)) rcu_exit(void); + +static DEFINE_URCU_TLS(int, rcu_signal_was_blocked); #endif +void __attribute__((destructor)) rcu_exit(void); +static void urcu_call_rcu_exit(void); + /* * rcu_gp_lock ensures mutual exclusion between threads calling * synchronize_rcu(). @@ -259,17 +263,25 @@ static void wait_gp(void) smp_mb_master(); /* Temporarily unlock the registry lock. */ mutex_unlock(&rcu_registry_lock); - if (uatomic_read(&rcu_gp.futex) != -1) - goto end; - while (futex_async(&rcu_gp.futex, FUTEX_WAIT, -1, - NULL, NULL, 0)) { + while (uatomic_read(&rcu_gp.futex) == -1) { + if (!futex_async(&rcu_gp.futex, FUTEX_WAIT, -1, NULL, NULL, 0)) { + /* + * Prior queued wakeups queued by unrelated code + * using the same address can cause futex wait to + * return 0 even through the futex value is still + * -1 (spurious wakeups). Check the value again + * in user-space to validate whether it really + * differs from -1. + */ + continue; + } switch (errno) { - case EWOULDBLOCK: + case EAGAIN: /* Value already changed. */ goto end; case EINTR: /* Retry if interrupted by signal. */ - break; /* Get out of switch. */ + break; /* Get out of switch. Check again. */ default: /* Unexpected error. */ urcu_die(errno); @@ -441,7 +453,7 @@ void synchronize_rcu(void) /* * Wait for readers to observe original parity or be quiescent. * wait_for_readers() can release and grab again rcu_registry_lock - * interally. + * internally. */ wait_for_readers(®istry, &cur_snap_readers, &qsreaders); @@ -483,7 +495,7 @@ void synchronize_rcu(void) /* * Wait for readers to observe new parity or be quiescent. * wait_for_readers() can release and grab again rcu_registry_lock - * interally. + * internally. */ wait_for_readers(&cur_snap_readers, NULL, &qsreaders); @@ -529,14 +541,58 @@ int rcu_read_ongoing(void) return _rcu_read_ongoing(); } +#ifdef RCU_SIGNAL +/* + * Make sure the signal used by the urcu-signal flavor is unblocked + * while the thread is registered. + */ +static +void urcu_signal_unblock(void) +{ + sigset_t mask, oldmask; + int ret; + + ret = sigemptyset(&mask); + urcu_posix_assert(!ret); + ret = sigaddset(&mask, SIGRCU); + urcu_posix_assert(!ret); + ret = pthread_sigmask(SIG_UNBLOCK, &mask, &oldmask); + urcu_posix_assert(!ret); + URCU_TLS(rcu_signal_was_blocked) = sigismember(&oldmask, SIGRCU); +} + +static +void urcu_signal_restore(void) +{ + sigset_t mask; + int ret; + + if (!URCU_TLS(rcu_signal_was_blocked)) + return; + ret = sigemptyset(&mask); + urcu_posix_assert(!ret); + ret = sigaddset(&mask, SIGRCU); + urcu_posix_assert(!ret); + ret = pthread_sigmask(SIG_BLOCK, &mask, NULL); + urcu_posix_assert(!ret); +} +#else +static +void urcu_signal_unblock(void) { } +static +void urcu_signal_restore(void) { } +#endif + void rcu_register_thread(void) { + urcu_signal_unblock(); + URCU_TLS(rcu_reader).tid = pthread_self(); - assert(URCU_TLS(rcu_reader).need_mb == 0); - assert(!(URCU_TLS(rcu_reader).ctr & URCU_GP_CTR_NEST_MASK)); + urcu_posix_assert(URCU_TLS(rcu_reader).need_mb == 0); + urcu_posix_assert(!(URCU_TLS(rcu_reader).ctr & URCU_GP_CTR_NEST_MASK)); mutex_lock(&rcu_registry_lock); - assert(!URCU_TLS(rcu_reader).registered); + urcu_posix_assert(!URCU_TLS(rcu_reader).registered); URCU_TLS(rcu_reader).registered = 1; rcu_init(); /* In case gcc does not support constructor attribute */ cds_list_add(&URCU_TLS(rcu_reader).node, ®istry); @@ -546,10 +602,12 @@ void rcu_register_thread(void) void rcu_unregister_thread(void) { mutex_lock(&rcu_registry_lock); - assert(URCU_TLS(rcu_reader).registered); + urcu_posix_assert(URCU_TLS(rcu_reader).registered); URCU_TLS(rcu_reader).registered = 0; cds_list_del(&URCU_TLS(rcu_reader).node); mutex_unlock(&rcu_registry_lock); + + urcu_signal_restore(); } #ifdef RCU_MEMBARRIER @@ -640,21 +698,24 @@ void rcu_init(void) urcu_die(errno); } +/* + * Don't unregister the SIGRCU signal handler anymore, because + * call_rcu threads could still be using it shortly before the + * application exits. + * Assertion disabled because call_rcu threads are now rcu + * readers, and left running at exit. + * urcu_posix_assert(cds_list_empty(®istry)); + */ + +#endif /* #ifdef RCU_SIGNAL */ + void rcu_exit(void) { - /* - * Don't unregister the SIGRCU signal handler anymore, because - * call_rcu threads could still be using it shortly before the - * application exits. - * Assertion disabled because call_rcu threads are now rcu - * readers, and left running at exit. - * assert(cds_list_empty(®istry)); - */ + urcu_call_rcu_exit(); } -#endif /* #ifdef RCU_SIGNAL */ - DEFINE_RCU_FLAVOR(rcu_flavor); #include "urcu-call-rcu-impl.h" #include "urcu-defer-impl.h" +#include "urcu-poll-impl.h"