xref: /aosp_15_r20/external/abseil-cpp/absl/synchronization/internal/futex_waiter.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
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 <cstdint>
21 #include <cerrno>
22 
23 #include "absl/base/config.h"
24 #include "absl/base/internal/raw_logging.h"
25 #include "absl/base/internal/thread_identity.h"
26 #include "absl/base/optimization.h"
27 #include "absl/synchronization/internal/kernel_timeout.h"
28 #include "absl/synchronization/internal/futex.h"
29 
30 namespace absl {
31 ABSL_NAMESPACE_BEGIN
32 namespace synchronization_internal {
33 
34 #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
35 constexpr char FutexWaiter::kName[];
36 #endif
37 
WaitUntil(std::atomic<int32_t> * v,int32_t val,KernelTimeout t)38 int FutexWaiter::WaitUntil(std::atomic<int32_t>* v, int32_t val,
39                            KernelTimeout t) {
40 #ifdef CLOCK_MONOTONIC
41   constexpr bool kHasClockMonotonic = true;
42 #else
43   constexpr bool kHasClockMonotonic = false;
44 #endif
45 
46   // We can't call Futex::WaitUntil() here because the prodkernel implementation
47   // does not know about KernelTimeout::SupportsSteadyClock().
48   if (!t.has_timeout()) {
49     return Futex::Wait(v, val);
50   } else if (kHasClockMonotonic && KernelTimeout::SupportsSteadyClock() &&
51              t.is_relative_timeout()) {
52     auto rel_timespec = t.MakeRelativeTimespec();
53     return Futex::WaitRelativeTimeout(v, val, &rel_timespec);
54   } else {
55     auto abs_timespec = t.MakeAbsTimespec();
56     return Futex::WaitAbsoluteTimeout(v, val, &abs_timespec);
57   }
58 }
59 
Wait(KernelTimeout t)60 bool FutexWaiter::Wait(KernelTimeout t) {
61   // Loop until we can atomically decrement futex from a positive
62   // value, waiting on a futex while we believe it is zero.
63   // Note that, since the thread ticker is just reset, we don't need to check
64   // whether the thread is idle on the very first pass of the loop.
65   bool first_pass = true;
66   while (true) {
67     int32_t x = futex_.load(std::memory_order_relaxed);
68     while (x != 0) {
69       if (!futex_.compare_exchange_weak(x, x - 1,
70                                         std::memory_order_acquire,
71                                         std::memory_order_relaxed)) {
72         continue;  // Raced with someone, retry.
73       }
74       return true;  // Consumed a wakeup, we are done.
75     }
76 
77     if (!first_pass) MaybeBecomeIdle();
78     const int err = WaitUntil(&futex_, 0, t);
79     if (err != 0) {
80       if (err == -EINTR || err == -EWOULDBLOCK) {
81         // Do nothing, the loop will retry.
82       } else if (err == -ETIMEDOUT) {
83         return false;
84       } else {
85         ABSL_RAW_LOG(FATAL, "Futex operation failed with error %d\n", err);
86       }
87     }
88     first_pass = false;
89   }
90 }
91 
Post()92 void FutexWaiter::Post() {
93   if (futex_.fetch_add(1, std::memory_order_release) == 0) {
94     // We incremented from 0, need to wake a potential waiter.
95     Poke();
96   }
97 }
98 
Poke()99 void FutexWaiter::Poke() {
100   // Wake one thread waiting on the futex.
101   const int err = Futex::Wake(&futex_, 1);
102   if (ABSL_PREDICT_FALSE(err < 0)) {
103     ABSL_RAW_LOG(FATAL, "Futex operation failed with error %d\n", err);
104   }
105 }
106 
107 }  // namespace synchronization_internal
108 ABSL_NAMESPACE_END
109 }  // namespace absl
110 
111 #endif  // ABSL_INTERNAL_HAVE_FUTEX_WAITER
112