Commit | Line | Data |
---|---|---|
099e26bd | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
ab5be9fa | 3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
099e26bd | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
099e26bd | 6 | * |
099e26bd DG |
7 | */ |
8 | ||
6c1c0768 | 9 | #define _LGPL_SOURCE |
28ab034a JG |
10 | #include "futex.hpp" |
11 | ||
12 | #include <common/common.hpp> | |
13 | ||
0fdd1e2c | 14 | #include <limits.h> |
099e26bd DG |
15 | #include <unistd.h> |
16 | #include <urcu.h> | |
17 | #include <urcu/futex.h> | |
18 | ||
099e26bd DG |
19 | /* |
20 | * This futex wait/wake scheme only works for N wakers / 1 waiters. Hence the | |
21 | * "nto1" added to all function signature. | |
22 | * | |
b45947b9 JG |
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 | |
099e26bd DG |
33 | */ |
34 | ||
0fdd1e2c DG |
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); | |
cd9adb8b | 45 | if (futex_async(futex, FUTEX_WAKE, INT_MAX, nullptr, nullptr, 0) < 0) { |
549731b7 MD |
46 | PERROR("futex_async"); |
47 | abort(); | |
48 | } | |
0fdd1e2c DG |
49 | } else { |
50 | uatomic_set(futex, 0); | |
51 | } | |
52 | ||
53 | DBG("Futex wait update active %d", active); | |
54 | } | |
55 | ||
099e26bd DG |
56 | /* |
57 | * Prepare futex. | |
58 | */ | |
59 | void futex_nto1_prepare(int32_t *futex) | |
60 | { | |
b45947b9 | 61 | uatomic_set(futex, -1); |
099e26bd DG |
62 | cmm_smp_mb(); |
63 | ||
64 | DBG("Futex n to 1 prepare done"); | |
65 | } | |
66 | ||
67 | /* | |
68 | * Wait futex. | |
69 | */ | |
70 | void futex_nto1_wait(int32_t *futex) | |
71 | { | |
b45947b9 | 72 | cmm_smp_mb(); |
099e26bd | 73 | |
36962e16 | 74 | while (uatomic_read(futex) == -1) { |
cd9adb8b | 75 | if (!futex_async(futex, FUTEX_WAIT, -1, nullptr, nullptr, 0)) { |
36962e16 MD |
76 | /* |
77 | * Prior queued wakeups queued by unrelated code | |
78 | * using the same address can cause futex wait to | |
79 | * return 0 even through the futex value is still | |
80 | * -1 (spurious wakeups). Check the value again | |
81 | * in user-space to validate whether it really | |
82 | * differs from -1. | |
83 | */ | |
84 | continue; | |
85 | } | |
549731b7 | 86 | switch (errno) { |
36962e16 | 87 | case EAGAIN: |
549731b7 | 88 | /* Value already changed. */ |
b45947b9 | 89 | goto end; |
549731b7 MD |
90 | case EINTR: |
91 | /* Retry if interrupted by signal. */ | |
28ab034a | 92 | break; /* Get out of switch. Check again. */ |
549731b7 MD |
93 | default: |
94 | /* Unexpected error. */ | |
b45947b9 | 95 | PERROR("futex_async"); |
549731b7 MD |
96 | abort(); |
97 | } | |
099e26bd | 98 | } |
b45947b9 | 99 | end: |
099e26bd DG |
100 | DBG("Futex n to 1 wait done"); |
101 | } | |
102 | ||
103 | /* | |
104 | * Wake 1 futex. | |
105 | */ | |
106 | void futex_nto1_wake(int32_t *futex) | |
107 | { | |
b45947b9 JG |
108 | if (caa_unlikely(uatomic_read(futex) != -1)) |
109 | goto end; | |
110 | uatomic_set(futex, 0); | |
cd9adb8b | 111 | if (futex_async(futex, FUTEX_WAKE, 1, nullptr, nullptr, 0) < 0) { |
b45947b9 JG |
112 | PERROR("futex_async"); |
113 | abort(); | |
099e26bd | 114 | } |
b45947b9 | 115 | end: |
099e26bd DG |
116 | DBG("Futex n to 1 wake done"); |
117 | } |