summaryrefslogtreecommitdiff
path: root/absl/synchronization/internal/futex_waiter.cc
diff options
context:
space:
mode:
authorGravatar Derek Mauro <dmauro@google.com>2023-04-12 13:26:48 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2023-04-12 13:27:28 -0700
commitc23acb9b5636e7b908fba03d6b3584d8f80dba6d (patch)
treecc2332512cecd0d3efb2f3819516fb6d7af44bc6 /absl/synchronization/internal/futex_waiter.cc
parent32d314d0f5bb0ca3ff71ece49c71a728c128d43e (diff)
Synchronization: Consolidate the logic for whether steady clocks are supported
for relative timeouts PiperOrigin-RevId: 523789416 Change-Id: Ide4cfdcae9ea7bffca3355c80ea9c8833a9536e6
Diffstat (limited to 'absl/synchronization/internal/futex_waiter.cc')
-rw-r--r--absl/synchronization/internal/futex_waiter.cc24
1 files changed, 23 insertions, 1 deletions
diff --git a/absl/synchronization/internal/futex_waiter.cc b/absl/synchronization/internal/futex_waiter.cc
index 0a76bc9a..7c07fbe2 100644
--- a/absl/synchronization/internal/futex_waiter.cc
+++ b/absl/synchronization/internal/futex_waiter.cc
@@ -35,6 +35,28 @@ namespace synchronization_internal {
constexpr char FutexWaiter::kName[];
#endif
+int FutexWaiter::WaitUntil(std::atomic<int32_t>* v, int32_t val,
+ KernelTimeout t) {
+#ifdef CLOCK_MONOTONIC
+ constexpr bool kHasClockMonotonic = true;
+#else
+ constexpr bool kHasClockMonotonic = false;
+#endif
+
+ // We can't call Futex::WaitUntil() here because the prodkernel implementation
+ // does not know about KernelTimeout::SupportsSteadyClock().
+ if (!t.has_timeout()) {
+ return Futex::Wait(v, val);
+ } else if (kHasClockMonotonic && KernelTimeout::SupportsSteadyClock() &&
+ t.is_relative_timeout()) {
+ auto rel_timespec = t.MakeRelativeTimespec();
+ return Futex::WaitRelativeTimeout(v, val, &rel_timespec);
+ } else {
+ auto abs_timespec = t.MakeAbsTimespec();
+ return Futex::WaitAbsoluteTimeout(v, val, &abs_timespec);
+ }
+}
+
bool FutexWaiter::Wait(KernelTimeout t) {
// Loop until we can atomically decrement futex from a positive
// value, waiting on a futex while we believe it is zero.
@@ -54,7 +76,7 @@ bool FutexWaiter::Wait(KernelTimeout t) {
}
if (!first_pass) MaybeBecomeIdle();
- const int err = Futex::WaitUntil(&futex_, 0, t);
+ const int err = WaitUntil(&futex_, 0, t);
if (err != 0) {
if (err == -EINTR || err == -EWOULDBLOCK) {
// Do nothing, the loop will retry.