Commit | Line | Data |
---|---|---|
287a512f | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
3 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
287a512f | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: LGPL-2.1-only |
287a512f | 6 | * |
287a512f JG |
7 | */ |
8 | ||
c9e313bc | 9 | #include "error.hpp" |
f40b76ae | 10 | #include "macros.hpp" |
28ab034a JG |
11 | #include "waiter.hpp" |
12 | ||
287a512f | 13 | #include <poll.h> |
28ab034a JG |
14 | #include <urcu/futex.h> |
15 | #include <urcu/uatomic.h> | |
287a512f | 16 | |
32670d71 JG |
17 | namespace { |
18 | /* Number of busy-loop attempts before waiting on futex. */ | |
19 | constexpr auto wait_attempt_count = 1000; | |
287a512f JG |
20 | |
21 | enum waiter_state { | |
22 | /* WAITER_WAITING is compared directly (futex compares it). */ | |
28ab034a | 23 | WAITER_WAITING = 0, |
287a512f | 24 | /* non-zero are used as masks. */ |
28ab034a JG |
25 | WAITER_WOKEN_UP = (1 << 0), |
26 | WAITER_RUNNING = (1 << 1), | |
27 | WAITER_TEARDOWN = (1 << 2), | |
287a512f | 28 | }; |
32670d71 | 29 | } /* namespace */ |
287a512f | 30 | |
32670d71 | 31 | lttng::synchro::waiter::waiter() |
287a512f | 32 | { |
32670d71 JG |
33 | arm(); |
34 | } | |
35 | ||
36 | void lttng::synchro::waiter::arm() noexcept | |
37 | { | |
38 | cds_wfs_node_init(&_wait_queue_node); | |
39 | uatomic_set(&_state, WAITER_WAITING); | |
287a512f JG |
40 | cmm_smp_mb(); |
41 | } | |
42 | ||
43 | /* | |
32670d71 | 44 | * User must arm "waiter" before passing its memory to waker thread. |
287a512f | 45 | */ |
32670d71 | 46 | void lttng::synchro::waiter::wait() |
287a512f | 47 | { |
f40b76ae JG |
48 | DBG("Beginning of waiter \"wait\" period"); |
49 | ||
50 | /* Load and test condition before read state. */ | |
287a512f | 51 | cmm_smp_rmb(); |
32670d71 JG |
52 | for (unsigned int i = 0; i < wait_attempt_count; i++) { |
53 | if (uatomic_read(&_state) != WAITER_WAITING) { | |
287a512f JG |
54 | goto skip_futex_wait; |
55 | } | |
f40b76ae | 56 | |
287a512f JG |
57 | caa_cpu_relax(); |
58 | } | |
f40b76ae | 59 | |
32670d71 | 60 | while (uatomic_read(&_state) == WAITER_WAITING) { |
cd9adb8b | 61 | if (!futex_noasync( |
32670d71 | 62 | &_state, FUTEX_WAIT, WAITER_WAITING, nullptr, nullptr, 0)) { |
6e5438dc MD |
63 | /* |
64 | * Prior queued wakeups queued by unrelated code | |
65 | * using the same address can cause futex wait to | |
66 | * return 0 even through the futex value is still | |
67 | * WAITER_WAITING (spurious wakeups). Check | |
68 | * the value again in user-space to validate | |
69 | * whether it really differs from WAITER_WAITING. | |
70 | */ | |
71 | continue; | |
72 | } | |
f40b76ae | 73 | |
287a512f | 74 | switch (errno) { |
6e5438dc | 75 | case EAGAIN: |
287a512f JG |
76 | /* Value already changed. */ |
77 | goto skip_futex_wait; | |
78 | case EINTR: | |
79 | /* Retry if interrupted by signal. */ | |
28ab034a | 80 | break; /* Get out of switch. Check again. */ |
287a512f JG |
81 | default: |
82 | /* Unexpected error. */ | |
83 | PERROR("futex_noasync"); | |
84 | abort(); | |
85 | } | |
86 | } | |
87 | skip_futex_wait: | |
88 | ||
89 | /* Tell waker thread than we are running. */ | |
32670d71 | 90 | uatomic_or(&_state, WAITER_RUNNING); |
287a512f JG |
91 | |
92 | /* | |
93 | * Wait until waker thread lets us know it's ok to tear down | |
94 | * memory allocated for struct lttng_waiter. | |
95 | */ | |
32670d71 JG |
96 | for (unsigned int i = 0; i < wait_attempt_count; i++) { |
97 | if (uatomic_read(&_state) & WAITER_TEARDOWN) { | |
287a512f JG |
98 | break; |
99 | } | |
f40b76ae | 100 | |
287a512f JG |
101 | caa_cpu_relax(); |
102 | } | |
f40b76ae | 103 | |
32670d71 | 104 | while (!(uatomic_read(&_state) & WAITER_TEARDOWN)) { |
cd9adb8b | 105 | poll(nullptr, 0, 10); |
287a512f | 106 | } |
f40b76ae | 107 | |
32670d71 | 108 | LTTNG_ASSERT(uatomic_read(&_state) & WAITER_TEARDOWN); |
f40b76ae | 109 | DBG("End of waiter \"wait\" period"); |
287a512f JG |
110 | } |
111 | ||
32670d71 JG |
112 | lttng::synchro::waker lttng::synchro::waiter::get_waker() |
113 | { | |
114 | return lttng::synchro::waker(_state); | |
115 | } | |
116 | ||
287a512f JG |
117 | /* |
118 | * Note: lttng_waiter_wake needs waiter to stay allocated throughout its | |
119 | * execution. In this scheme, the waiter owns the node memory, and we only allow | |
120 | * it to free this memory when it sees the WAITER_TEARDOWN flag. | |
121 | */ | |
32670d71 | 122 | void lttng::synchro::waker::wake() |
287a512f JG |
123 | { |
124 | cmm_smp_mb(); | |
32670d71 JG |
125 | |
126 | LTTNG_ASSERT(uatomic_read(&_state) == WAITER_WAITING); | |
127 | ||
128 | uatomic_set(&_state, WAITER_WOKEN_UP); | |
129 | if (!(uatomic_read(&_state) & WAITER_RUNNING)) { | |
130 | if (futex_noasync(&_state, FUTEX_WAKE, 1, nullptr, nullptr, 0) < 0) { | |
287a512f JG |
131 | PERROR("futex_noasync"); |
132 | abort(); | |
133 | } | |
134 | } | |
f40b76ae | 135 | |
287a512f | 136 | /* Allow teardown of struct urcu_wait memory. */ |
32670d71 | 137 | uatomic_or(&_state, WAITER_TEARDOWN); |
287a512f | 138 | } |
f40b76ae | 139 | |
32670d71 | 140 | lttng::synchro::wait_queue::wait_queue() |
f40b76ae | 141 | { |
32670d71 | 142 | cds_wfs_init(&_stack); |
f40b76ae JG |
143 | } |
144 | ||
32670d71 | 145 | void lttng::synchro::wait_queue::add(waiter &waiter) noexcept |
f40b76ae | 146 | { |
32670d71 | 147 | (void) cds_wfs_push(&_stack, &waiter._wait_queue_node); |
f40b76ae JG |
148 | } |
149 | ||
32670d71 | 150 | void lttng::synchro::wait_queue::wake_all() |
f40b76ae | 151 | { |
f40b76ae | 152 | /* Move all waiters from the queue to our local stack. */ |
32670d71 | 153 | auto *waiters = __cds_wfs_pop_all(&_stack); |
f40b76ae JG |
154 | |
155 | /* Wake all waiters in our stack head. */ | |
32670d71 JG |
156 | cds_wfs_node *iter, *iter_n; |
157 | cds_wfs_for_each_blocking_safe(waiters, iter, iter_n) { | |
158 | auto& waiter = *lttng::utils::container_of( | |
159 | iter, <tng::synchro::waiter::_wait_queue_node); | |
f40b76ae JG |
160 | |
161 | /* Don't wake already running threads. */ | |
32670d71 | 162 | if (waiter._state & WAITER_RUNNING) { |
f40b76ae JG |
163 | continue; |
164 | } | |
165 | ||
32670d71 | 166 | waiter.get_waker().wake(); |
f40b76ae JG |
167 | } |
168 | } |