Fix: futex wait: handle spurious futex wakeups
[lttng-tools.git] / src / common / futex.cpp
index 21ab09ab7e455cc961305f6711ed2b6f53e36132..e178b7e1b4f1e8a99de25e42b10733266353ada0 100644 (file)
@@ -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.023071 seconds and 4 git commands to generate.