Fix: urcu: futex wait: handle spurious futex wakeups
[urcu.git] / src / urcu.c
index 902b8f839a75b4df7e0b077ed780a2f0b0c4a433..59f2e8f18fdddc5a781e25d2478ca86e18a9fee5 100644 (file)
@@ -30,7 +30,6 @@
 #include <stdio.h>
 #include <pthread.h>
 #include <signal.h>
-#include <assert.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
@@ -39,6 +38,7 @@
 #include <poll.h>
 
 #include <urcu/config.h>
+#include <urcu/assert.h>
 #include <urcu/arch.h>
 #include <urcu/wfcqueue.h>
 #include <urcu/map/urcu.h>
@@ -259,17 +259,25 @@ static void wait_gp(void)
        smp_mb_master();
        /* Temporarily unlock the registry lock. */
        mutex_unlock(&rcu_registry_lock);
-       if (uatomic_read(&rcu_gp.futex) != -1)
-               goto end;
-       while (futex_async(&rcu_gp.futex, FUTEX_WAIT, -1,
-                       NULL, NULL, 0)) {
+       while (uatomic_read(&rcu_gp.futex) == -1) {
+               if (!futex_async(&rcu_gp.futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
+                       /*
+                        * Prior queued wakeups queued by unrelated code
+                        * using the same address can cause futex wait to
+                        * return 0 even through the futex value is still
+                        * -1 (spurious wakeups). Check the value again
+                        * in user-space to validate whether it really
+                        * differs from -1.
+                        */
+                       continue;
+               }
                switch (errno) {
-               case EWOULDBLOCK:
+               case EAGAIN:
                        /* Value already changed. */
                        goto end;
                case EINTR:
                        /* Retry if interrupted by signal. */
-                       break;  /* Get out of switch. */
+                       break;  /* Get out of switch. Check again. */
                default:
                        /* Unexpected error. */
                        urcu_die(errno);
@@ -532,11 +540,11 @@ int rcu_read_ongoing(void)
 void rcu_register_thread(void)
 {
        URCU_TLS(rcu_reader).tid = pthread_self();
-       assert(URCU_TLS(rcu_reader).need_mb == 0);
-       assert(!(URCU_TLS(rcu_reader).ctr & URCU_GP_CTR_NEST_MASK));
+       urcu_posix_assert(URCU_TLS(rcu_reader).need_mb == 0);
+       urcu_posix_assert(!(URCU_TLS(rcu_reader).ctr & URCU_GP_CTR_NEST_MASK));
 
        mutex_lock(&rcu_registry_lock);
-       assert(!URCU_TLS(rcu_reader).registered);
+       urcu_posix_assert(!URCU_TLS(rcu_reader).registered);
        URCU_TLS(rcu_reader).registered = 1;
        rcu_init();     /* In case gcc does not support constructor attribute */
        cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
@@ -546,7 +554,7 @@ void rcu_register_thread(void)
 void rcu_unregister_thread(void)
 {
        mutex_lock(&rcu_registry_lock);
-       assert(URCU_TLS(rcu_reader).registered);
+       urcu_posix_assert(URCU_TLS(rcu_reader).registered);
        URCU_TLS(rcu_reader).registered = 0;
        cds_list_del(&URCU_TLS(rcu_reader).node);
        mutex_unlock(&rcu_registry_lock);
@@ -648,7 +656,7 @@ void rcu_exit(void)
         * application exits.
         * Assertion disabled because call_rcu threads are now rcu
         * readers, and left running at exit.
-        * assert(cds_list_empty(&registry));
+        * urcu_posix_assert(cds_list_empty(&registry));
         */
 }
 
This page took 0.026968 seconds and 4 git commands to generate.