Fix: futex wait: handle spurious futex wakeups
[lttng-tools.git] / src / common / futex.cpp
index 3eadf096bc8086760e36b24f3a342341d048c9fa..e178b7e1b4f1e8a99de25e42b10733266353ada0 100644 (file)
@@ -12,9 +12,9 @@
 #include <urcu.h>
 #include <urcu/futex.h>
 
-#include <common/common.h>
+#include <common/common.hpp>
 
-#include "futex.h"
+#include "futex.hpp"
 
 /*
  * This futex wait/wake scheme only works for N wakers / 1 waiters. Hence the
@@ -72,16 +72,25 @@ void futex_nto1_wait(int32_t *futex)
 {
        cmm_smp_mb();
 
-       if (uatomic_read(futex) != -1)
-               goto end;
-       while (futex_async(futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
+       while (uatomic_read(futex) == -1) {
+               if (!futex_async(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. */
                        PERROR("futex_async");
This page took 0.023102 seconds and 4 git commands to generate.