Coverage Report

Created: 2024-09-23 06:29

/src/abseil-cpp/absl/synchronization/internal/futex_waiter.cc
Line
Count
Source (jump to first uncovered line)
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
38
int FutexWaiter::WaitUntil(std::atomic<int32_t>* v, int32_t val,
39
0
                           KernelTimeout t) {
40
0
#ifdef CLOCK_MONOTONIC
41
0
  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
0
  if (!t.has_timeout()) {
49
0
    return Futex::Wait(v, val);
50
0
  } else if (kHasClockMonotonic && KernelTimeout::SupportsSteadyClock() &&
51
0
             t.is_relative_timeout()) {
52
0
    auto rel_timespec = t.MakeRelativeTimespec();
53
0
    return Futex::WaitRelativeTimeout(v, val, &rel_timespec);
54
0
  } else {
55
0
    auto abs_timespec = t.MakeAbsTimespec();
56
0
    return Futex::WaitAbsoluteTimeout(v, val, &abs_timespec);
57
0
  }
58
0
}
59
60
0
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
0
  bool first_pass = true;
66
0
  while (true) {
67
0
    int32_t x = futex_.load(std::memory_order_relaxed);
68
0
    while (x != 0) {
69
0
      if (!futex_.compare_exchange_weak(x, x - 1,
70
0
                                        std::memory_order_acquire,
71
0
                                        std::memory_order_relaxed)) {
72
0
        continue;  // Raced with someone, retry.
73
0
      }
74
0
      return true;  // Consumed a wakeup, we are done.
75
0
    }
76
77
0
    if (!first_pass) MaybeBecomeIdle();
78
0
    const int err = WaitUntil(&futex_, 0, t);
79
0
    if (err != 0) {
80
0
      if (err == -EINTR || err == -EWOULDBLOCK) {
81
        // Do nothing, the loop will retry.
82
0
      } else if (err == -ETIMEDOUT) {
83
0
        return false;
84
0
      } else {
85
0
        ABSL_RAW_LOG(FATAL, "Futex operation failed with error %d\n", err);
86
0
      }
87
0
    }
88
0
    first_pass = false;
89
0
  }
90
0
}
91
92
0
void FutexWaiter::Post() {
93
0
  if (futex_.fetch_add(1, std::memory_order_release) == 0) {
94
    // We incremented from 0, need to wake a potential waiter.
95
0
    Poke();
96
0
  }
97
0
}
98
99
0
void FutexWaiter::Poke() {
100
  // Wake one thread waiting on the futex.
101
0
  const int err = Futex::Wake(&futex_, 1);
102
0
  if (ABSL_PREDICT_FALSE(err < 0)) {
103
0
    ABSL_RAW_LOG(FATAL, "Futex operation failed with error %d\n", err);
104
0
  }
105
0
}
106
107
}  // namespace synchronization_internal
108
ABSL_NAMESPACE_END
109
}  // namespace absl
110
111
#endif  // ABSL_INTERNAL_HAVE_FUTEX_WAITER