Clean-up: modernize pretty_xml.cpp
[lttng-tools.git] / src / common / futex.cpp
index 21ab09ab7e455cc961305f6711ed2b6f53e36132..b0e01fc73d4d8d4a6cbec6e669958fe80fa94b5e 100644 (file)
@@ -7,15 +7,15 @@
  */
 
 #define _LGPL_SOURCE
+#include "futex.hpp"
+
+#include <common/common.hpp>
+
 #include <limits.h>
 #include <unistd.h>
 #include <urcu.h>
 #include <urcu/futex.h>
 
-#include <common/common.hpp>
-
-#include "futex.hpp"
-
 /*
  * This futex wait/wake scheme only works for N wakers / 1 waiters. Hence the
  * "nto1" added to all function signature.
@@ -42,8 +42,7 @@ void futex_wait_update(int32_t *futex, int active)
 {
        if (active) {
                uatomic_set(futex, 1);
-               if (futex_async(futex, FUTEX_WAKE,
-                               INT_MAX, NULL, NULL, 0) < 0) {
+               if (futex_async(futex, FUTEX_WAKE, INT_MAX, nullptr, nullptr, 0) < 0) {
                        PERROR("futex_async");
                        abort();
                }
@@ -72,16 +71,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, nullptr, nullptr, 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");
@@ -100,7 +108,7 @@ void futex_nto1_wake(int32_t *futex)
        if (caa_unlikely(uatomic_read(futex) != -1))
                goto end;
        uatomic_set(futex, 0);
-       if (futex_async(futex, FUTEX_WAKE, 1, NULL, NULL, 0) < 0) {
+       if (futex_async(futex, FUTEX_WAKE, 1, nullptr, nullptr, 0) < 0) {
                PERROR("futex_async");
                abort();
        }
This page took 0.024843 seconds and 4 git commands to generate.