Fix: deadlock when thread join is issued in read-side C.S.
[urcu.git] / urcu-qsbr.c
index a35dcfc5aea3674e9747ef69daea0f49def7a972..685efb5297dca06632697d810f5f078bc0aa835d 100644 (file)
 
 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;
 struct rcu_gp rcu_gp = { .ctr = RCU_GP_ONLINE };
 
 /*
@@ -112,6 +126,10 @@ static void wait_gp(void)
                      NULL, NULL, 0);
 }
 
+/*
+ * Always called with rcu_registry lock held. Releases this lock between
+ * iterations and grabs it again. Holds the lock when it returns.
+ */
 static void wait_for_readers(struct cds_list_head *input_readers,
                        struct cds_list_head *cur_snap_readers,
                        struct cds_list_head *qsreaders)
@@ -171,6 +189,8 @@ static void wait_for_readers(struct cds_list_head *input_readers,
                        }
                        break;
                } else {
+                       /* Temporarily unlock the registry lock. */
+                       mutex_unlock(&rcu_registry_lock);
                        if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
                                wait_gp();
                        } else {
@@ -180,6 +200,8 @@ static void wait_for_readers(struct cds_list_head *input_readers,
                                cmm_smp_mb();
 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
                        }
+                       /* Re-lock the registry lock before the next loop. */
+                       mutex_lock(&rcu_registry_lock);
                }
        }
 }
@@ -233,11 +255,15 @@ void synchronize_rcu(void)
         */
        urcu_move_waiters(&waiters, &gp_waiters);
 
+       mutex_lock(&rcu_registry_lock);
+
        if (cds_list_empty(&registry))
                goto out;
 
        /*
         * Wait for readers to observe original parity or be quiescent.
+        * wait_for_readers() can release and grab again rcu_registry_lock
+        * interally.
         */
        wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
 
@@ -279,6 +305,8 @@ 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.
         */
        wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
 
@@ -287,6 +315,7 @@ void synchronize_rcu(void)
         */
        cds_list_splice(&qsreaders, &registry);
 out:
+       mutex_unlock(&rcu_registry_lock);
        mutex_unlock(&rcu_gp_lock);
        urcu_wake_all_waiters(&waiters);
 gp_end:
@@ -339,6 +368,8 @@ void synchronize_rcu(void)
         */
        urcu_move_waiters(&waiters, &gp_waiters);
 
+       mutex_lock(&rcu_registry_lock);
+
        if (cds_list_empty(&registry))
                goto out;
 
@@ -363,6 +394,8 @@ void synchronize_rcu(void)
 
        /*
         * Wait for readers to observe new count of be quiescent.
+        * wait_for_readers() can release and grab again rcu_registry_lock
+        * interally.
         */
        wait_for_readers(&registry, NULL, &qsreaders);
 
@@ -371,6 +404,7 @@ void synchronize_rcu(void)
         */
        cds_list_splice(&qsreaders, &registry);
 out:
+       mutex_unlock(&rcu_registry_lock);
        mutex_unlock(&rcu_gp_lock);
        urcu_wake_all_waiters(&waiters);
 gp_end:
@@ -420,9 +454,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, &registry);
-       mutex_unlock(&rcu_gp_lock);
+       mutex_unlock(&rcu_registry_lock);
        _rcu_thread_online();
 }
 
@@ -433,9 +467,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)
This page took 0.024299 seconds and 4 git commands to generate.