Skip to content

Commit 1a72ea7

Browse files
derekmaurocopybara-github
authored andcommitted
Synchronization: Support true relative timeouts using the POSIX
proposed standard pthread_cond_clockwait() and sem_clockwait(). These are currently implemented in glibc >= 2.30. These methods take a clock and use an absolute time with reference to that clock, so KernelTimeout now can produce these values. PiperOrigin-RevId: 522824226 Change-Id: Ife98713f6f95d800b1f8e52d5364a3dbebc4f8a6
1 parent 42a3c03 commit 1a72ea7

File tree

6 files changed

+150
-11
lines changed

6 files changed

+150
-11
lines changed

absl/synchronization/internal/kernel_timeout.cc

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
#include "absl/synchronization/internal/kernel_timeout.h"
1616

17+
#ifndef _WIN32
18+
#include <sys/types.h>
19+
#endif
20+
1721
#include <algorithm>
1822
#include <chrono> // NOLINT(build/c++11)
1923
#include <cstdint>
@@ -101,7 +105,7 @@ int64_t KernelTimeout::MakeAbsNanos() const {
101105
return kMaxNanos;
102106
}
103107

104-
int64_t nanos = RawNanos();
108+
int64_t nanos = RawAbsNanos();
105109

106110
if (is_relative_timeout()) {
107111
// We need to change epochs, because the relative timeout might be
@@ -128,7 +132,7 @@ int64_t KernelTimeout::InNanosecondsFromNow() const {
128132
return kMaxNanos;
129133
}
130134

131-
int64_t nanos = RawNanos();
135+
int64_t nanos = RawAbsNanos();
132136
if (is_absolute_timeout()) {
133137
return std::max<int64_t>(nanos - absl::GetCurrentTimeNanos(), 0);
134138
}
@@ -143,6 +147,33 @@ struct timespec KernelTimeout::MakeRelativeTimespec() const {
143147
return absl::ToTimespec(absl::Nanoseconds(InNanosecondsFromNow()));
144148
}
145149

150+
#ifndef _WIN32
151+
struct timespec KernelTimeout::MakeClockAbsoluteTimespec(clockid_t c) const {
152+
if (!has_timeout()) {
153+
return absl::ToTimespec(absl::Nanoseconds(kMaxNanos));
154+
}
155+
156+
int64_t nanos = RawAbsNanos();
157+
if (is_absolute_timeout()) {
158+
nanos -= absl::GetCurrentTimeNanos();
159+
} else {
160+
nanos -= SteadyClockNow();
161+
}
162+
163+
struct timespec now;
164+
ABSL_RAW_CHECK(clock_gettime(c, &now) == 0, "clock_gettime() failed");
165+
absl::Duration from_clock_epoch =
166+
absl::DurationFromTimespec(now) + absl::Nanoseconds(nanos);
167+
if (from_clock_epoch <= absl::ZeroDuration()) {
168+
// Some callers have assumed that 0 means no timeout, so instead we return a
169+
// time of 1 nanosecond after the epoch. For safety we also do not return
170+
// negative values.
171+
return absl::ToTimespec(absl::Nanoseconds(1));
172+
}
173+
return absl::ToTimespec(from_clock_epoch);
174+
}
175+
#endif
176+
146177
KernelTimeout::DWord KernelTimeout::InMillisecondsFromNow() const {
147178
constexpr DWord kInfinite = std::numeric_limits<DWord>::max();
148179

absl/synchronization/internal/kernel_timeout.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#ifndef ABSL_SYNCHRONIZATION_INTERNAL_KERNEL_TIMEOUT_H_
1616
#define ABSL_SYNCHRONIZATION_INTERNAL_KERNEL_TIMEOUT_H_
1717

18+
#ifndef _WIN32
19+
#include <sys/types.h>
20+
#endif
21+
1822
#include <algorithm>
1923
#include <chrono> // NOLINT(build/c++11)
2024
#include <cstdint>
@@ -78,6 +82,18 @@ class KernelTimeout {
7882
// this method in the case of a spurious wakeup.
7983
struct timespec MakeRelativeTimespec() const;
8084

85+
#ifndef _WIN32
86+
// Convert to `struct timespec` for interfaces that expect an absolute timeout
87+
// on a specific clock `c`. This is similar to `MakeAbsTimespec()`, but
88+
// callers usually want to use this method with `CLOCK_MONOTONIC` when
89+
// relative timeouts are requested, and when the appropriate interface expects
90+
// an absolute timeout relative to a specific clock (for example,
91+
// pthread_cond_clockwait() or sem_clockwait()). If !has_timeout(), attempts
92+
// to convert to a reasonable absolute timeout, but callers should to test
93+
// has_timeout() prefer to use a more appropriate interface.
94+
struct timespec MakeClockAbsoluteTimespec(clockid_t c) const;
95+
#endif
96+
8197
// Convert to unix epoch nanos for interfaces that expect an absolute timeout
8298
// in nanoseconds. If !has_timeout() or is_relative_timeout(), attempts to
8399
// convert to a reasonable absolute timeout, but callers should to test
@@ -125,12 +141,18 @@ class KernelTimeout {
125141
// after the unix epoch.
126142
// - If the low bit is 1, then the high 63 bits is the number of nanoseconds
127143
// after the epoch used by SteadyClockNow().
144+
//
145+
// In all cases the time is stored as an absolute time, the only difference is
146+
// the clock epoch. The use of absolute times is important since in the case
147+
// of a relative timeout with a spurious wakeup, the program would have to
148+
// restart the wait, and thus needs a way of recomputing the remaining time.
128149
uint64_t rep_;
129150

130151
// Returns the number of nanoseconds stored in the internal representation.
131-
// Together with is_absolute_timeout() and is_relative_timeout(), the return
132-
// value is used to compute when the timeout should occur.
133-
int64_t RawNanos() const { return static_cast<int64_t>(rep_ >> 1); }
152+
// When combined with the clock epoch indicated by the low bit (which is
153+
// accessed through is_absolute_timeout() and is_relative_timeout()), the
154+
// return value is used to compute when the timeout should occur.
155+
int64_t RawAbsNanos() const { return static_cast<int64_t>(rep_ >> 1); }
134156

135157
// Converts to nanoseconds from now. Since the return value is a relative
136158
// duration, it should be recomputed by calling this method in the case of a

absl/synchronization/internal/kernel_timeout_test.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ TEST(KernelTimeout, FiniteTimes) {
6464
EXPECT_TRUE(t.is_absolute_timeout());
6565
EXPECT_FALSE(t.is_relative_timeout());
6666
EXPECT_EQ(absl::TimeFromTimespec(t.MakeAbsTimespec()), when);
67+
#ifndef _WIN32
68+
EXPECT_LE(
69+
absl::AbsDuration(absl::Now() + duration -
70+
absl::TimeFromTimespec(
71+
t.MakeClockAbsoluteTimespec(CLOCK_REALTIME))),
72+
absl::Milliseconds(10));
73+
#endif
6774
EXPECT_LE(
6875
absl::AbsDuration(absl::DurationFromTimespec(t.MakeRelativeTimespec()) -
6976
std::max(duration, absl::ZeroDuration())),
@@ -89,6 +96,10 @@ TEST(KernelTimeout, InfiniteFuture) {
8996
// absl::InfiniteFuture(), but we should return a very large value.
9097
EXPECT_GT(absl::TimeFromTimespec(t.MakeAbsTimespec()),
9198
absl::Now() + absl::Hours(100000));
99+
#ifndef _WIN32
100+
EXPECT_GT(absl::TimeFromTimespec(t.MakeClockAbsoluteTimespec(CLOCK_REALTIME)),
101+
absl::Now() + absl::Hours(100000));
102+
#endif
92103
EXPECT_GT(absl::DurationFromTimespec(t.MakeRelativeTimespec()),
93104
absl::Hours(100000));
94105
EXPECT_GT(absl::FromUnixNanos(t.MakeAbsNanos()),
@@ -110,6 +121,10 @@ TEST(KernelTimeout, DefaultConstructor) {
110121
// absl::InfiniteFuture(), but we should return a very large value.
111122
EXPECT_GT(absl::TimeFromTimespec(t.MakeAbsTimespec()),
112123
absl::Now() + absl::Hours(100000));
124+
#ifndef _WIN32
125+
EXPECT_GT(absl::TimeFromTimespec(t.MakeClockAbsoluteTimespec(CLOCK_REALTIME)),
126+
absl::Now() + absl::Hours(100000));
127+
#endif
113128
EXPECT_GT(absl::DurationFromTimespec(t.MakeRelativeTimespec()),
114129
absl::Hours(100000));
115130
EXPECT_GT(absl::FromUnixNanos(t.MakeAbsNanos()),
@@ -131,6 +146,10 @@ TEST(KernelTimeout, TimeMaxNanos) {
131146
// absl::InfiniteFuture(), but we should return a very large value.
132147
EXPECT_GT(absl::TimeFromTimespec(t.MakeAbsTimespec()),
133148
absl::Now() + absl::Hours(100000));
149+
#ifndef _WIN32
150+
EXPECT_GT(absl::TimeFromTimespec(t.MakeClockAbsoluteTimespec(CLOCK_REALTIME)),
151+
absl::Now() + absl::Hours(100000));
152+
#endif
134153
EXPECT_GT(absl::DurationFromTimespec(t.MakeRelativeTimespec()),
135154
absl::Hours(100000));
136155
EXPECT_GT(absl::FromUnixNanos(t.MakeAbsNanos()),
@@ -152,6 +171,10 @@ TEST(KernelTimeout, Never) {
152171
// absl::InfiniteFuture(), but we should return a very large value.
153172
EXPECT_GT(absl::TimeFromTimespec(t.MakeAbsTimespec()),
154173
absl::Now() + absl::Hours(100000));
174+
#ifndef _WIN32
175+
EXPECT_GT(absl::TimeFromTimespec(t.MakeClockAbsoluteTimespec(CLOCK_REALTIME)),
176+
absl::Now() + absl::Hours(100000));
177+
#endif
155178
EXPECT_GT(absl::DurationFromTimespec(t.MakeRelativeTimespec()),
156179
absl::Hours(100000));
157180
EXPECT_GT(absl::FromUnixNanos(t.MakeAbsNanos()),
@@ -170,6 +193,10 @@ TEST(KernelTimeout, InfinitePast) {
170193
EXPECT_FALSE(t.is_relative_timeout());
171194
EXPECT_LE(absl::TimeFromTimespec(t.MakeAbsTimespec()),
172195
absl::FromUnixNanos(1));
196+
#ifndef _WIN32
197+
EXPECT_LE(absl::TimeFromTimespec(t.MakeClockAbsoluteTimespec(CLOCK_REALTIME)),
198+
absl::FromUnixSeconds(1));
199+
#endif
173200
EXPECT_EQ(absl::DurationFromTimespec(t.MakeRelativeTimespec()),
174201
absl::ZeroDuration());
175202
EXPECT_LE(absl::FromUnixNanos(t.MakeAbsNanos()), absl::FromUnixNanos(1));
@@ -200,6 +227,13 @@ TEST(KernelTimeout, FiniteDurations) {
200227
EXPECT_LE(absl::AbsDuration(absl::Now() + duration -
201228
absl::TimeFromTimespec(t.MakeAbsTimespec())),
202229
absl::Milliseconds(5));
230+
#ifndef _WIN32
231+
EXPECT_LE(
232+
absl::AbsDuration(absl::Now() + duration -
233+
absl::TimeFromTimespec(
234+
t.MakeClockAbsoluteTimespec(CLOCK_REALTIME))),
235+
absl::Milliseconds(5));
236+
#endif
203237
EXPECT_LE(
204238
absl::AbsDuration(absl::DurationFromTimespec(t.MakeRelativeTimespec()) -
205239
duration),
@@ -241,6 +275,12 @@ TEST(KernelTimeout, NegativeDurations) {
241275
EXPECT_LE(absl::AbsDuration(absl::Now() -
242276
absl::TimeFromTimespec(t.MakeAbsTimespec())),
243277
absl::Milliseconds(5));
278+
#ifndef _WIN32
279+
EXPECT_LE(absl::AbsDuration(absl::Now() - absl::TimeFromTimespec(
280+
t.MakeClockAbsoluteTimespec(
281+
CLOCK_REALTIME))),
282+
absl::Milliseconds(5));
283+
#endif
244284
EXPECT_EQ(absl::DurationFromTimespec(t.MakeRelativeTimespec()),
245285
absl::ZeroDuration());
246286
EXPECT_LE(
@@ -263,6 +303,10 @@ TEST(KernelTimeout, InfiniteDuration) {
263303
// absl::InfiniteFuture(), but we should return a very large value.
264304
EXPECT_GT(absl::TimeFromTimespec(t.MakeAbsTimespec()),
265305
absl::Now() + absl::Hours(100000));
306+
#ifndef _WIN32
307+
EXPECT_GT(absl::TimeFromTimespec(t.MakeClockAbsoluteTimespec(CLOCK_REALTIME)),
308+
absl::Now() + absl::Hours(100000));
309+
#endif
266310
EXPECT_GT(absl::DurationFromTimespec(t.MakeRelativeTimespec()),
267311
absl::Hours(100000));
268312
EXPECT_GT(absl::FromUnixNanos(t.MakeAbsNanos()),
@@ -284,6 +328,10 @@ TEST(KernelTimeout, DurationMaxNanos) {
284328
// absl::InfiniteFuture(), but we should return a very large value.
285329
EXPECT_GT(absl::TimeFromTimespec(t.MakeAbsTimespec()),
286330
absl::Now() + absl::Hours(100000));
331+
#ifndef _WIN32
332+
EXPECT_GT(absl::TimeFromTimespec(t.MakeClockAbsoluteTimespec(CLOCK_REALTIME)),
333+
absl::Now() + absl::Hours(100000));
334+
#endif
287335
EXPECT_GT(absl::DurationFromTimespec(t.MakeRelativeTimespec()),
288336
absl::Hours(100000));
289337
EXPECT_GT(absl::FromUnixNanos(t.MakeAbsNanos()),
@@ -305,6 +353,10 @@ TEST(KernelTimeout, OverflowNanos) {
305353
// Timeouts should still be far in the future.
306354
EXPECT_GT(absl::TimeFromTimespec(t.MakeAbsTimespec()),
307355
absl::Now() + absl::Hours(100000));
356+
#ifndef _WIN32
357+
EXPECT_GT(absl::TimeFromTimespec(t.MakeClockAbsoluteTimespec(CLOCK_REALTIME)),
358+
absl::Now() + absl::Hours(100000));
359+
#endif
308360
EXPECT_GT(absl::DurationFromTimespec(t.MakeRelativeTimespec()),
309361
absl::Hours(100000));
310362
EXPECT_GT(absl::FromUnixNanos(t.MakeAbsNanos()),

absl/synchronization/internal/pthread_waiter.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ PthreadWaiter::PthreadWaiter() : waiter_count_(0), wakeup_count_(0) {
7878
#define ABSL_INTERNAL_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP 1
7979
#endif
8080

81+
#if defined(__GLIBC__) && \
82+
(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30))
83+
#define ABSL_INTERNAL_HAVE_PTHREAD_COND_CLOCKWAIT 1
84+
#endif
85+
8186
// Calls pthread_cond_timedwait() or possibly something else like
8287
// pthread_cond_timedwait_relative_np() depending on the platform and
8388
// KernelTimeout requested. The return value is the same as the return
@@ -94,6 +99,11 @@ int PthreadWaiter::TimedWait(KernelTimeout t) {
9499
#ifdef ABSL_INTERNAL_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
95100
const auto rel_timeout = t.MakeRelativeTimespec();
96101
return pthread_cond_timedwait_relative_np(&cv_, &mu_, &rel_timeout);
102+
#elif defined(ABSL_INTERNAL_HAVE_PTHREAD_COND_CLOCKWAIT) && \
103+
defined(CLOCK_MONOTONIC)
104+
const auto abs_clock_timeout = t.MakeClockAbsoluteTimespec(CLOCK_MONOTONIC);
105+
return pthread_cond_clockwait(&cv_, &mu_, CLOCK_MONOTONIC,
106+
&abs_clock_timeout);
97107
#endif
98108
}
99109

absl/synchronization/internal/sem_waiter.cc

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,34 @@ SemWaiter::SemWaiter() : wakeups_(0) {
4343
}
4444
}
4545

46-
bool SemWaiter::Wait(KernelTimeout t) {
47-
struct timespec abs_timeout;
48-
if (t.has_timeout()) {
49-
abs_timeout = t.MakeAbsTimespec();
46+
#if defined(__GLIBC__) && \
47+
(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30))
48+
#define ABSL_INTERNAL_HAVE_SEM_CLOCKWAIT 1
49+
#endif
50+
51+
// Calls sem_timedwait() or possibly something else like
52+
// sem_clockwait() depending on the platform and
53+
// KernelTimeout requested. The return value is the same as a call to the return
54+
// value to a call to sem_timedwait().
55+
int SemWaiter::TimedWait(KernelTimeout t) {
56+
#ifndef __GOOGLE_GRTE_VERSION__
57+
constexpr bool kRelativeTimeoutSupported = true;
58+
#else
59+
constexpr bool kRelativeTimeoutSupported = false;
60+
#endif
61+
62+
if (kRelativeTimeoutSupported && t.is_relative_timeout()) {
63+
#if defined(ABSL_INTERNAL_HAVE_SEM_CLOCKWAIT) && defined(CLOCK_MONOTONIC)
64+
const auto abs_clock_timeout = t.MakeClockAbsoluteTimespec(CLOCK_MONOTONIC);
65+
return sem_clockwait(&sem_, CLOCK_MONOTONIC, &abs_clock_timeout);
66+
#endif
5067
}
5168

69+
const auto abs_timeout = t.MakeAbsTimespec();
70+
return sem_timedwait(&sem_, &abs_timeout);
71+
}
72+
73+
bool SemWaiter::Wait(KernelTimeout t) {
5274
// Loop until we timeout or consume a wakeup.
5375
// Note that, since the thread ticker is just reset, we don't need to check
5476
// whether the thread is idle on the very first pass of the loop.
@@ -73,10 +95,10 @@ bool SemWaiter::Wait(KernelTimeout t) {
7395
if (errno == EINTR) continue;
7496
ABSL_RAW_LOG(FATAL, "sem_wait failed: %d", errno);
7597
} else {
76-
if (sem_timedwait(&sem_, &abs_timeout) == 0) break;
98+
if (TimedWait(t) == 0) break;
7799
if (errno == EINTR) continue;
78100
if (errno == ETIMEDOUT) return false;
79-
ABSL_RAW_LOG(FATAL, "sem_timedwait failed: %d", errno);
101+
ABSL_RAW_LOG(FATAL, "SemWaiter::TimedWait() failed: %d", errno);
80102
}
81103
}
82104
first_pass = false;

absl/synchronization/internal/sem_waiter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class SemWaiter : public WaiterCrtp<SemWaiter> {
4646
static constexpr char kName[] = "SemWaiter";
4747

4848
private:
49+
int TimedWait(KernelTimeout t);
50+
4951
sem_t sem_;
5052

5153
// This seems superfluous, but for Poke() we need to cause spurious

0 commit comments

Comments
 (0)