Fix: futex wait: handle spurious futex wakeups
[lttng-tools.git] / src / common / futex.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10 #include <limits.h>
11 #include <unistd.h>
12 #include <urcu.h>
13 #include <urcu/futex.h>
14
15 #include <common/common.hpp>
16
17 #include "futex.hpp"
18
19 /*
20 * This futex wait/wake scheme only works for N wakers / 1 waiters. Hence the
21 * "nto1" added to all function signature.
22 *
23 * Please see wait_gp()/update_counter_and_wait() calls in urcu.c in the urcu
24 * git tree for a detail example of this scheme being used. futex_async() is
25 * the urcu wrapper over the futex() sycall.
26 *
27 * There is also a formal verification available in the git tree.
28 *
29 * branch: formal-model
30 * commit id: 2a8044f3493046fcc8c67016902dc7beec6f026a
31 *
32 * Ref: git://git.lttng.org/userspace-rcu.git
33 */
34
35 /*
36 * Update futex according to active or not. This scheme is used to wake every
37 * libust waiting on the shared memory map futex hence the INT_MAX used in the
38 * futex() call. If active, we set the value and wake everyone else we indicate
39 * that we are gone (cleanup() case).
40 */
41 void futex_wait_update(int32_t *futex, int active)
42 {
43 if (active) {
44 uatomic_set(futex, 1);
45 if (futex_async(futex, FUTEX_WAKE,
46 INT_MAX, NULL, NULL, 0) < 0) {
47 PERROR("futex_async");
48 abort();
49 }
50 } else {
51 uatomic_set(futex, 0);
52 }
53
54 DBG("Futex wait update active %d", active);
55 }
56
57 /*
58 * Prepare futex.
59 */
60 void futex_nto1_prepare(int32_t *futex)
61 {
62 uatomic_set(futex, -1);
63 cmm_smp_mb();
64
65 DBG("Futex n to 1 prepare done");
66 }
67
68 /*
69 * Wait futex.
70 */
71 void futex_nto1_wait(int32_t *futex)
72 {
73 cmm_smp_mb();
74
75 while (uatomic_read(futex) == -1) {
76 if (!futex_async(futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
77 /*
78 * Prior queued wakeups queued by unrelated code
79 * using the same address can cause futex wait to
80 * return 0 even through the futex value is still
81 * -1 (spurious wakeups). Check the value again
82 * in user-space to validate whether it really
83 * differs from -1.
84 */
85 continue;
86 }
87 switch (errno) {
88 case EAGAIN:
89 /* Value already changed. */
90 goto end;
91 case EINTR:
92 /* Retry if interrupted by signal. */
93 break; /* Get out of switch. Check again. */
94 default:
95 /* Unexpected error. */
96 PERROR("futex_async");
97 abort();
98 }
99 }
100 end:
101 DBG("Futex n to 1 wait done");
102 }
103
104 /*
105 * Wake 1 futex.
106 */
107 void futex_nto1_wake(int32_t *futex)
108 {
109 if (caa_unlikely(uatomic_read(futex) != -1))
110 goto end;
111 uatomic_set(futex, 0);
112 if (futex_async(futex, FUTEX_WAKE, 1, NULL, NULL, 0) < 0) {
113 PERROR("futex_async");
114 abort();
115 }
116 end:
117 DBG("Futex n to 1 wake done");
118 }
This page took 0.032196 seconds and 5 git commands to generate.