Fix: defer_rcu: futex wait: handle spurious futex wakeups
[urcu.git] / src / urcu-defer-impl.h
index d25e9b93f4dc17e8f1e6ee986ad04cdca366527f..b5d79262c03bb88a28cd376d78c4189ca3c578cd 100644 (file)
@@ -194,17 +194,25 @@ static void wait_defer(void)
                uatomic_set(&defer_thread_futex, 0);
        } else {
                cmm_smp_rmb();  /* Read queue before read futex */
                uatomic_set(&defer_thread_futex, 0);
        } else {
                cmm_smp_rmb();  /* Read queue before read futex */
-               if (uatomic_read(&defer_thread_futex) != -1)
-                       return;
-               while (futex_noasync(&defer_thread_futex, FUTEX_WAIT, -1,
-                               NULL, NULL, 0)) {
+               while (uatomic_read(&defer_thread_futex) == -1) {
+                       if (!futex_noasync(&defer_thread_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) {
                        switch (errno) {
-                       case EWOULDBLOCK:
+                       case EAGAIN:
                                /* Value already changed. */
                                return;
                        case EINTR:
                                /* Retry if interrupted by signal. */
                                /* Value already changed. */
                                return;
                        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);
                        default:
                                /* Unexpected error. */
                                urcu_die(errno);
This page took 0.022745 seconds and 4 git commands to generate.