Add rcu_read_ongoing() API to each urcu flavor
[urcu.git] / urcu-qsbr.c
index eb167d128be77125ce450b1bac808870fb994975..786a80d6b16b1936dc62b71b43db18850854dd41 100644 (file)
@@ -43,6 +43,7 @@
 #include "urcu/tls-compat.h"
 
 #include "urcu-die.h"
+#include "urcu-wait.h"
 
 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
 #undef _LGPL_SOURCE
@@ -78,6 +79,12 @@ DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
 
 static CDS_LIST_HEAD(registry);
 
+/*
+ * Queue keeping threads awaiting to wait for a grace period. Contains
+ * struct gp_waiters_thread objects.
+ */
+static DEFINE_URCU_WAIT_QUEUE(gp_waiters);
+
 static void mutex_lock(pthread_mutex_t *mutex)
 {
        int ret;
@@ -116,9 +123,10 @@ static void wait_gp(void)
                      NULL, NULL, 0);
 }
 
-static void wait_for_readers(void)
+static void wait_for_readers(struct cds_list_head *input_readers,
+                       struct cds_list_head *cur_snap_readers,
+                       struct cds_list_head *qsreaders)
 {
-       CDS_LIST_HEAD(qsreaders);
        int wait_loops = 0;
        struct rcu_reader *index, *tmp;
 
@@ -136,18 +144,36 @@ static void wait_for_readers(void)
                         * reads them in the opposite order).
                         */
                        cmm_smp_wmb();
-                       cds_list_for_each_entry(index, &registry, node) {
+                       cds_list_for_each_entry(index, input_readers, node) {
                                _CMM_STORE_SHARED(index->waiting, 1);
                        }
                        /* Write futex before read reader_gp */
                        cmm_smp_mb();
                }
-               cds_list_for_each_entry_safe(index, tmp, &registry, node) {
-                       if (!rcu_gp_ongoing(&index->ctr))
-                               cds_list_move(&index->node, &qsreaders);
+               cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
+                       switch (rcu_reader_state(&index->ctr)) {
+                       case RCU_READER_ACTIVE_CURRENT:
+                               if (cur_snap_readers) {
+                                       cds_list_move(&index->node,
+                                               cur_snap_readers);
+                                       break;
+                               }
+                               /* Fall-through */
+                       case RCU_READER_INACTIVE:
+                               cds_list_move(&index->node, qsreaders);
+                               break;
+                       case RCU_READER_ACTIVE_OLD:
+                               /*
+                                * Old snapshot. Leaving node in
+                                * input_readers will make us busy-loop
+                                * until the snapshot becomes current or
+                                * the reader becomes inactive.
+                                */
+                               break;
+                       }
                }
 
-               if (cds_list_empty(&registry)) {
+               if (cds_list_empty(input_readers)) {
                        if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
                                /* Read reader_gp before write futex */
                                cmm_smp_mb();
@@ -166,8 +192,6 @@ static void wait_for_readers(void)
                        }
                }
        }
-       /* put back the reader list in the registry */
-       cds_list_splice(&qsreaders, &registry);
 }
 
 /*
@@ -178,9 +202,13 @@ static void wait_for_readers(void)
 #if (CAA_BITS_PER_LONG < 64)
 void synchronize_rcu(void)
 {
+       CDS_LIST_HEAD(cur_snap_readers);
+       CDS_LIST_HEAD(qsreaders);
        unsigned long was_online;
+       DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
+       struct urcu_waiters waiters;
 
-       was_online = URCU_TLS(rcu_reader).ctr;
+       was_online = rcu_read_ongoing();
 
        /* All threads should read qparity before accessing data structure
         * where new ptr points to.  In the "then" case, rcu_thread_offline
@@ -195,15 +223,33 @@ void synchronize_rcu(void)
        else
                cmm_smp_mb();
 
+       /*
+        * Add ourself to gp_waiters queue of threads awaiting to wait
+        * for a grace period. Proceed to perform the grace period only
+        * if we are the first thread added into the queue.
+        */
+       if (urcu_wait_add(&gp_waiters, &wait) != 0) {
+               /* Not first in queue: will be awakened by another thread. */
+               urcu_adaptative_busy_wait(&wait);
+               goto gp_end;
+       }
+       /* We won't need to wake ourself up */
+       urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
+
        mutex_lock(&rcu_gp_lock);
 
+       /*
+        * Move all waiters into our local queue.
+        */
+       urcu_move_waiters(&waiters, &gp_waiters);
+
        if (cds_list_empty(&registry))
                goto out;
 
        /*
         * Wait for readers to observe original parity or be quiescent.
         */
-       wait_for_readers();
+       wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
 
        /*
         * Must finish waiting for quiescent state for original parity
@@ -244,10 +290,16 @@ void synchronize_rcu(void)
        /*
         * Wait for readers to observe new parity or be quiescent.
         */
-       wait_for_readers();
+       wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
+
+       /*
+        * Put quiescent reader list back into registry.
+        */
+       cds_list_splice(&qsreaders, &registry);
 out:
        mutex_unlock(&rcu_gp_lock);
-
+       urcu_wake_all_waiters(&waiters);
+gp_end:
        /*
         * Finish waiting for reader threads before letting the old ptr being
         * freed.
@@ -260,9 +312,12 @@ out:
 #else /* !(CAA_BITS_PER_LONG < 64) */
 void synchronize_rcu(void)
 {
+       CDS_LIST_HEAD(qsreaders);
        unsigned long was_online;
+       DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
+       struct urcu_waiters waiters;
 
-       was_online = URCU_TLS(rcu_reader).ctr;
+       was_online = rcu_read_ongoing();
 
        /*
         * Mark the writer thread offline to make sure we don't wait for
@@ -274,7 +329,26 @@ void synchronize_rcu(void)
        else
                cmm_smp_mb();
 
+       /*
+        * Add ourself to gp_waiters queue of threads awaiting to wait
+        * for a grace period. Proceed to perform the grace period only
+        * if we are the first thread added into the queue.
+        */
+       if (urcu_wait_add(&gp_waiters, &wait) != 0) {
+               /* Not first in queue: will be awakened by another thread. */
+               urcu_adaptative_busy_wait(&wait);
+               goto gp_end;
+       }
+       /* We won't need to wake ourself up */
+       urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
+
        mutex_lock(&rcu_gp_lock);
+
+       /*
+        * Move all waiters into our local queue.
+        */
+       urcu_move_waiters(&waiters, &gp_waiters);
+
        if (cds_list_empty(&registry))
                goto out;
 
@@ -300,10 +374,16 @@ void synchronize_rcu(void)
        /*
         * Wait for readers to observe new count of be quiescent.
         */
-       wait_for_readers();
+       wait_for_readers(&registry, NULL, &qsreaders);
+
+       /*
+        * Put quiescent reader list back into registry.
+        */
+       cds_list_splice(&qsreaders, &registry);
 out:
        mutex_unlock(&rcu_gp_lock);
-
+       urcu_wake_all_waiters(&waiters);
+gp_end:
        if (was_online)
                rcu_thread_online();
        else
@@ -325,6 +405,11 @@ void rcu_read_unlock(void)
        _rcu_read_unlock();
 }
 
+int rcu_read_ongoing(void)
+{
+       return _rcu_read_ongoing();
+}
+
 void rcu_quiescent_state(void)
 {
        _rcu_quiescent_state();
This page took 0.025788 seconds and 4 git commands to generate.