tests: Use stderr redirection for time output
[urcu.git] / urcu-bp.c
index 387a52bf892df54cfcac0f14868ad70b2e8d1959..6b2875d2566da7e16e5767c171aee56186eb03be 100644 (file)
--- 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,6 +91,9 @@ 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
@@ -103,11 +106,6 @@ static int initialized;
 
 static pthread_key_t urcu_bp_key;
 
-#ifdef DEBUG_YIELD
-unsigned int rcu_yield_active;
-DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
-#endif
-
 struct rcu_gp rcu_gp = { .ctr = RCU_GP_COUNT };
 
 /*
@@ -166,7 +164,7 @@ static void wait_for_readers(struct cds_list_head *input_readers,
                        struct cds_list_head *cur_snap_readers,
                        struct cds_list_head *qsreaders)
 {
-       int wait_loops = 0;
+       unsigned int wait_loops = 0;
        struct rcu_reader *index, *tmp;
 
        /*
@@ -175,7 +173,9 @@ static void wait_for_readers(struct cds_list_head *input_readers,
         * rcu_gp.ctr value.
         */
        for (;;) {
-               wait_loops++;
+               if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
+                       wait_loops++;
+
                cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
                        switch (rcu_reader_state(&index->ctr)) {
                        case RCU_READER_ACTIVE_CURRENT:
@@ -202,8 +202,8 @@ static void wait_for_readers(struct cds_list_head *input_readers,
                if (cds_list_empty(input_readers)) {
                        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();
                }
@@ -454,11 +454,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;
 }
@@ -499,7 +496,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;
@@ -512,11 +509,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();
 }
 
 /*
@@ -526,15 +524,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,
@@ -549,16 +546,21 @@ void rcu_bp_init(void)
 static
 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,
-                       &registry_arena.chunk_list, node) {
-               munmap(chunk, chunk->data_len + sizeof(struct registry_chunk));
+               cds_list_for_each_entry_safe(chunk, tmp,
+                               &registry_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);
 }
 
 /*
This page took 0.024312 seconds and 4 git commands to generate.