From 504b51f195e8cff785391e055de149890784467c Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Thu, 24 Nov 2022 14:13:41 -0500 Subject: [PATCH] Fix: compare loaded futex value after futex wait syscall return Handle spurious wakeups. Signed-off-by: Mathieu Desnoyers --- src/rcu.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/rcu.c b/src/rcu.c index 1fd1af2..9652725 100644 --- a/src/rcu.c +++ b/src/rcu.c @@ -89,9 +89,14 @@ void wait_gp(struct side_rcu_gp_state *gp_state) } else { __atomic_thread_fence(__ATOMIC_SEQ_CST); } - if (__atomic_load_n(&gp_state->futex, __ATOMIC_RELAXED) != -1) - return; - while (futex(&gp_state->futex, FUTEX_WAIT, -1, NULL, NULL, 0)) { + while (__atomic_load_n(&gp_state->futex, __ATOMIC_RELAXED) == -1) { + if (!futex(&gp_state->futex, FUTEX_WAIT, -1, NULL, NULL, 0)) { + /* + * May be awakened by either spurious wake up or + * because the state is now as expected. + */ + continue; + } switch (errno) { case EWOULDBLOCK: /* Value already changed. */ -- 2.34.1