X-Git-Url: https://git.liburcu.org/?a=blobdiff_plain;f=src%2Fcommon%2Ffutex.cpp;h=1b9f7fc9288cae694e21cad369ffc87dc7ea174e;hb=28ab034a2c3582d07d3423d2d746731f87d3969f;hp=3eadf096bc8086760e36b24f3a342341d048c9fa;hpb=21cf9b6b1843774306a76f4dccddddd706b64f79;p=lttng-tools.git diff --git a/src/common/futex.cpp b/src/common/futex.cpp index 3eadf096b..1b9f7fc92 100644 --- a/src/common/futex.cpp +++ b/src/common/futex.cpp @@ -7,15 +7,15 @@ */ #define _LGPL_SOURCE +#include "futex.hpp" + +#include + #include #include #include #include -#include - -#include "futex.h" - /* * 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, NULL, NULL, 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, 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");