/src/abseil-cpp/absl/synchronization/internal/futex_waiter.cc
Line | Count | Source |
1 | | // Copyright 2023 The Abseil Authors. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "absl/synchronization/internal/futex_waiter.h" |
16 | | |
17 | | #ifdef ABSL_INTERNAL_HAVE_FUTEX_WAITER |
18 | | |
19 | | #include <atomic> |
20 | | #include <cerrno> |
21 | | #include <cstdint> |
22 | | |
23 | | #include "absl/base/config.h" |
24 | | #include "absl/base/internal/raw_logging.h" |
25 | | #include "absl/base/optimization.h" |
26 | | #include "absl/synchronization/internal/futex.h" |
27 | | #include "absl/synchronization/internal/kernel_timeout.h" |
28 | | |
29 | | namespace absl { |
30 | | ABSL_NAMESPACE_BEGIN |
31 | | namespace synchronization_internal { |
32 | | |
33 | | int FutexWaiter::WaitUntil(std::atomic<int32_t>* v, int32_t val, |
34 | 0 | KernelTimeout t) { |
35 | 0 | #ifdef CLOCK_MONOTONIC |
36 | 0 | constexpr bool kHasClockMonotonic = true; |
37 | | #else |
38 | | constexpr bool kHasClockMonotonic = false; |
39 | | #endif |
40 | | |
41 | | // We can't call Futex::WaitUntil() here because the prodkernel implementation |
42 | | // does not know about KernelTimeout::SupportsSteadyClock(). |
43 | 0 | if (!t.has_timeout()) { |
44 | 0 | return Futex::Wait(v, val); |
45 | 0 | } else if (kHasClockMonotonic && KernelTimeout::SupportsSteadyClock() && |
46 | 0 | t.is_relative_timeout()) { |
47 | 0 | auto rel_timespec = t.MakeRelativeTimespec(); |
48 | 0 | return Futex::WaitRelativeTimeout(v, val, &rel_timespec); |
49 | 0 | } else { |
50 | 0 | auto abs_timespec = t.MakeAbsTimespec(); |
51 | 0 | return Futex::WaitAbsoluteTimeout(v, val, &abs_timespec); |
52 | 0 | } |
53 | 0 | } |
54 | | |
55 | 0 | bool FutexWaiter::Wait(KernelTimeout t) { |
56 | | // Loop until we can atomically decrement futex from a positive |
57 | | // value, waiting on a futex while we believe it is zero. |
58 | | // Note that, since the thread ticker is just reset, we don't need to check |
59 | | // whether the thread is idle on the very first pass of the loop. |
60 | 0 | bool first_pass = true; |
61 | 0 | while (true) { |
62 | 0 | int32_t x = futex_.load(std::memory_order_relaxed); |
63 | 0 | while (x != 0) { |
64 | 0 | if (!futex_.compare_exchange_weak(x, x - 1, |
65 | 0 | std::memory_order_acquire, |
66 | 0 | std::memory_order_relaxed)) { |
67 | 0 | continue; // Raced with someone, retry. |
68 | 0 | } |
69 | 0 | return true; // Consumed a wakeup, we are done. |
70 | 0 | } |
71 | | |
72 | 0 | if (!first_pass) MaybeBecomeIdle(); |
73 | 0 | const int err = WaitUntil(&futex_, 0, t); |
74 | 0 | if (err != 0) { |
75 | 0 | if (err == -EINTR || err == -EWOULDBLOCK) { |
76 | | // Do nothing, the loop will retry. |
77 | 0 | } else if (err == -ETIMEDOUT) { |
78 | 0 | return false; |
79 | 0 | } else { |
80 | 0 | ABSL_RAW_LOG(FATAL, "Futex operation failed with error %d\n", err); |
81 | 0 | } |
82 | 0 | } |
83 | 0 | first_pass = false; |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | 0 | void FutexWaiter::Post() { |
88 | 0 | if (futex_.fetch_add(1, std::memory_order_release) == 0) { |
89 | | // We incremented from 0, need to wake a potential waiter. |
90 | 0 | Poke(); |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | 0 | void FutexWaiter::Poke() { |
95 | | // Wake one thread waiting on the futex. |
96 | 0 | const int err = Futex::Wake(&futex_, 1); |
97 | 0 | if (ABSL_PREDICT_FALSE(err < 0)) { |
98 | | ABSL_RAW_LOG(FATAL, "Futex operation failed with error %d\n", err); |
99 | 0 | } |
100 | 0 | } |
101 | | |
102 | | } // namespace synchronization_internal |
103 | | ABSL_NAMESPACE_END |
104 | | } // namespace absl |
105 | | |
106 | | #endif // ABSL_INTERNAL_HAVE_FUTEX_WAITER |