X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=urcu-qsbr.c;h=52cb04aee9ff1b1b5a7b2caef45d6d5b5317ba77;hb=11eb040f24e020d05d65983d0f87f79b000c7b9f;hp=ec483d925c0bdbfc85e8a0dfeb1690849a940705;hpb=1745be1a65fcf0398247b03ed30efeeeba52482a;p=urcu.git diff --git a/urcu-qsbr.c b/urcu-qsbr.c index ec483d9..52cb04a 100644 --- a/urcu-qsbr.c +++ b/urcu-qsbr.c @@ -51,7 +51,21 @@ void __attribute__((destructor)) rcu_exit(void); +/* + * rcu_gp_lock ensures mutual exclusion between threads calling + * synchronize_rcu(). + */ static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER; +/* + * rcu_registry_lock ensures mutual exclusion between threads + * registering and unregistering themselves to/from the registry, and + * with threads reading that registry from synchronize_rcu(). However, + * this lock is not held all the way through the completion of awaiting + * for the grace period. It is sporadically released between iterations + * on the registry. + * rcu_registry_lock may nest inside rcu_gp_lock. + */ +static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER; int32_t gp_futex; @@ -111,15 +125,32 @@ static void wait_gp(void) { /* Read reader_gp before read futex */ cmm_smp_rmb(); - if (uatomic_read(&gp_futex) == -1) - futex_noasync(&gp_futex, FUTEX_WAIT, -1, - NULL, NULL, 0); + if (uatomic_read(&gp_futex) != -1) + return; + while (futex_noasync(&gp_futex, FUTEX_WAIT, -1, + NULL, NULL, 0)) { + switch (errno) { + case EWOULDBLOCK: + /* Value already changed. */ + return; + case EINTR: + /* Retry if interrupted by signal. */ + break; /* Get out of switch. */ + default: + /* Unexpected error. */ + urcu_die(errno); + } + } } +/* + * Always called with rcu_registry lock held. Releases this lock between + * iterations and grabs it again. Holds the lock when it returns. + */ static void update_counter_and_wait(void) { CDS_LIST_HEAD(qsreaders); - int wait_loops = 0; + unsigned int wait_loops = 0; struct rcu_reader *index, *tmp; #if (CAA_BITS_PER_LONG < 64) @@ -150,7 +181,8 @@ static void update_counter_and_wait(void) * Wait for each thread rcu_reader_qs_gp count to become 0. */ for (;;) { - wait_loops++; + if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS) + wait_loops++; if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { uatomic_set(&gp_futex, -1); /* @@ -177,6 +209,8 @@ static void update_counter_and_wait(void) } break; } else { + /* Temporarily unlock the registry lock. */ + mutex_unlock(&rcu_registry_lock); if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { wait_gp(); } else { @@ -186,6 +220,8 @@ static void update_counter_and_wait(void) cmm_smp_mb(); #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ } + /* Re-lock the registry lock before the next loop. */ + mutex_lock(&rcu_registry_lock); } } /* put back the reader list in the registry */ @@ -218,12 +254,15 @@ void synchronize_rcu(void) cmm_smp_mb(); mutex_lock(&rcu_gp_lock); + mutex_lock(&rcu_registry_lock); if (cds_list_empty(®istry)) goto out; /* * Wait for previous parity to be empty of readers. + * update_counter_and_wait() can release and grab again + * rcu_registry_lock interally. */ update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */ @@ -246,9 +285,12 @@ void synchronize_rcu(void) /* * Wait for previous parity to be empty of readers. + * update_counter_and_wait() can release and grab again + * rcu_registry_lock interally. */ update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */ out: + mutex_unlock(&rcu_registry_lock); mutex_unlock(&rcu_gp_lock); /* @@ -278,10 +320,16 @@ void synchronize_rcu(void) cmm_smp_mb(); mutex_lock(&rcu_gp_lock); + mutex_lock(&rcu_registry_lock); if (cds_list_empty(®istry)) goto out; + /* + * update_counter_and_wait() can release and grab again + * rcu_registry_lock interally. + */ update_counter_and_wait(); out: + mutex_unlock(&rcu_registry_lock); mutex_unlock(&rcu_gp_lock); if (was_online) @@ -325,9 +373,9 @@ void rcu_register_thread(void) URCU_TLS(rcu_reader).tid = pthread_self(); assert(URCU_TLS(rcu_reader).ctr == 0); - mutex_lock(&rcu_gp_lock); + mutex_lock(&rcu_registry_lock); cds_list_add(&URCU_TLS(rcu_reader).node, ®istry); - mutex_unlock(&rcu_gp_lock); + mutex_unlock(&rcu_registry_lock); _rcu_thread_online(); } @@ -338,9 +386,9 @@ void rcu_unregister_thread(void) * with a waiting writer. */ _rcu_thread_offline(); - mutex_lock(&rcu_gp_lock); + mutex_lock(&rcu_registry_lock); cds_list_del(&URCU_TLS(rcu_reader).node); - mutex_unlock(&rcu_gp_lock); + mutex_unlock(&rcu_registry_lock); } void rcu_exit(void)