X-Git-Url: http://git.liburcu.org/?a=blobdiff_plain;f=urcu-bp.c;h=280dd0dac4c5e00b9775e73f7b86f3a5507e5ca5;hb=6278a922c993b7e2a2ed4e0e3e75fb642271f3ba;hp=71474bcbcb6fe85494635c004bfdeb0d5f7a3aae;hpb=e038e286679be66f3d8096ea9c7224a1f282a9c3;p=userspace-rcu.git diff --git a/urcu-bp.c b/urcu-bp.c index 71474bc..280dd0d 100644 --- a/urcu-bp.c +++ b/urcu-bp.c @@ -79,8 +79,8 @@ void *mremap_wrapper(void *old_address, size_t old_size, } #endif -/* Sleep delay in us */ -#define RCU_SLEEP_DELAY 1000 +/* Sleep delay in ms */ +#define RCU_SLEEP_DELAY_MS 10 #define INIT_NR_THREADS 8 #define ARENA_INIT_ALLOC \ sizeof(struct registry_chunk) \ @@ -91,10 +91,13 @@ void *mremap_wrapper(void *old_address, size_t old_size, */ #define RCU_QS_ACTIVE_ATTEMPTS 100 +static +int rcu_bp_refcount; + static void __attribute__((constructor)) rcu_bp_init(void); static -void __attribute__((destructor)) rcu_bp_exit(void); +void __attribute__((destructor)) _rcu_bp_exit(void); static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER; @@ -105,7 +108,7 @@ static pthread_key_t urcu_bp_key; #ifdef DEBUG_YIELD unsigned int yield_active; -DEFINE_URCU_TLS(unsigned int, rand_yield); +__DEFINE_URCU_TLS_GLOBAL(unsigned int, rand_yield); #endif /* @@ -120,7 +123,7 @@ long rcu_gp_ctr = RCU_GP_COUNT; * Pointer to registry elements. Written to only by each individual reader. Read * by both the reader and the writers. */ -DEFINE_URCU_TLS(struct rcu_reader *, rcu_reader); +__DEFINE_URCU_TLS_GLOBAL(struct rcu_reader *, rcu_reader); static CDS_LIST_HEAD(registry); @@ -171,7 +174,7 @@ static void mutex_unlock(pthread_mutex_t *mutex) void update_counter_and_wait(void) { CDS_LIST_HEAD(qsreaders); - int wait_loops = 0; + unsigned int wait_loops = 0; struct rcu_reader *index, *tmp; /* Switch parity: 0 -> 1, 1 -> 0 */ @@ -195,7 +198,9 @@ void update_counter_and_wait(void) * Wait for each thread rcu_reader.ctr count to become 0. */ for (;;) { - wait_loops++; + if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS) + wait_loops++; + cds_list_for_each_entry_safe(index, tmp, ®istry, node) { if (!rcu_old_gp_ongoing(&index->ctr)) cds_list_move(&index->node, &qsreaders); @@ -204,8 +209,8 @@ void update_counter_and_wait(void) if (cds_list_empty(®istry)) { break; } else { - if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) - usleep(RCU_SLEEP_DELAY); + if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) + (void) poll(NULL, 0, RCU_SLEEP_DELAY_MS); else caa_cpu_relax(); } @@ -429,11 +434,8 @@ struct registry_chunk *find_chunk(struct rcu_reader *rcu_reader_reg) /* Called with signals off and mutex locked */ static -void remove_thread(void) +void remove_thread(struct rcu_reader *rcu_reader_reg) { - struct rcu_reader *rcu_reader_reg; - - rcu_reader_reg = URCU_TLS(rcu_reader); cleanup_thread(find_chunk(rcu_reader_reg), rcu_reader_reg); URCU_TLS(rcu_reader) = NULL; } @@ -474,7 +476,7 @@ end: /* Disable signals, take mutex, remove from registry */ static -void rcu_bp_unregister(void) +void rcu_bp_unregister(struct rcu_reader *rcu_reader_reg) { sigset_t newmask, oldmask; int ret; @@ -487,11 +489,12 @@ void rcu_bp_unregister(void) abort(); mutex_lock(&rcu_gp_lock); - remove_thread(); + remove_thread(rcu_reader_reg); mutex_unlock(&rcu_gp_lock); ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL); if (ret) abort(); + _rcu_bp_exit(); } /* @@ -501,15 +504,14 @@ void rcu_bp_unregister(void) static void urcu_bp_thread_exit_notifier(void *rcu_key) { - assert(rcu_key == URCU_TLS(rcu_reader)); - rcu_bp_unregister(); + rcu_bp_unregister(rcu_key); } static void rcu_bp_init(void) { mutex_lock(&init_lock); - if (!initialized) { + if (!rcu_bp_refcount++) { int ret; ret = pthread_key_create(&urcu_bp_key, @@ -522,18 +524,32 @@ void rcu_bp_init(void) } static -void rcu_bp_exit(void) +void _rcu_bp_exit(void) { - struct registry_chunk *chunk, *tmp; - int ret; + mutex_lock(&init_lock); + if (!--rcu_bp_refcount) { + struct registry_chunk *chunk, *tmp; + int ret; - cds_list_for_each_entry_safe(chunk, tmp, - ®istry_arena.chunk_list, node) { - munmap(chunk, chunk->data_len + sizeof(struct registry_chunk)); + cds_list_for_each_entry_safe(chunk, tmp, + ®istry_arena.chunk_list, node) { + munmap(chunk, chunk->data_len + + sizeof(struct registry_chunk)); + } + ret = pthread_key_delete(urcu_bp_key); + if (ret) + abort(); } - ret = pthread_key_delete(urcu_bp_key); - if (ret) - abort(); + mutex_unlock(&init_lock); +} + +/* + * Keep ABI compability within stable versions. This has never been + * exposed through a header, but needs to stay in the .so until the + * soname is bumped. + */ +void rcu_bp_exit(void) +{ } /*