Coverage Report

Created: 2026-09-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/synchronization/internal/kernel_timeout.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/kernel_timeout.h"
16
17
#include <algorithm>
18
#include <chrono>  // NOLINT(build/c++11)
19
#include <cstdint>
20
#include <ctime>
21
#include <limits>
22
23
#include "absl/base/config.h"
24
#include "absl/base/internal/raw_logging.h"
25
#include "absl/time/clock.h"
26
#include "absl/time/time.h"
27
28
#ifndef _WIN32
29
#include <sys/types.h>
30
#endif
31
32
namespace absl {
33
ABSL_NAMESPACE_BEGIN
34
namespace synchronization_internal {
35
36
0
int64_t KernelTimeout::SteadyClockNow() {
37
0
  if (!SupportsSteadyClock()) {
38
0
    return absl::GetCurrentTimeNanos();
39
0
  }
40
0
  return std::chrono::duration_cast<std::chrono::nanoseconds>(
41
0
             std::chrono::steady_clock::now().time_since_epoch())
42
0
      .count();
43
0
}
44
45
0
KernelTimeout::KernelTimeout(absl::Time t) {
46
  // `absl::InfiniteFuture()` is a common "no timeout" value and cheaper to
47
  // compare than convert.
48
0
  if (t == absl::InfiniteFuture()) {
49
0
    rep_ = kNoTimeout;
50
0
    return;
51
0
  }
52
53
0
  int64_t unix_nanos = absl::ToUnixNanos(t);
54
55
  // A timeout that lands before the unix epoch is converted to 0.
56
  // In theory implementations should expire these timeouts immediately.
57
0
  if (unix_nanos < 0) {
58
0
    unix_nanos = 0;
59
0
  }
60
61
  // Values greater than or equal to kMaxNanos are converted to infinite.
62
0
  if (unix_nanos >= kMaxNanos) {
63
0
    rep_ = kNoTimeout;
64
0
    return;
65
0
  }
66
67
0
  rep_ = static_cast<uint64_t>(unix_nanos) << 1;
68
0
}
69
70
0
KernelTimeout::KernelTimeout(absl::Duration d) {
71
  // `absl::InfiniteDuration()` is a common "no timeout" value and cheaper to
72
  // compare than convert.
73
0
  if (d == absl::InfiniteDuration()) {
74
0
    rep_ = kNoTimeout;
75
0
    return;
76
0
  }
77
78
0
  int64_t nanos = absl::ToInt64Nanoseconds(d);
79
80
  // Negative durations are normalized to 0.
81
  // In theory implementations should expire these timeouts immediately.
82
0
  if (nanos < 0) {
83
0
    nanos = 0;
84
0
  }
85
86
0
  int64_t now = SteadyClockNow();
87
0
  if (nanos > kMaxNanos - now) {
88
    // Durations that would be greater than kMaxNanos are converted to infinite.
89
0
    rep_ = kNoTimeout;
90
0
    return;
91
0
  }
92
93
0
  nanos += now;
94
0
  rep_ = (static_cast<uint64_t>(nanos) << 1) | uint64_t{1};
95
0
}
96
97
0
int64_t KernelTimeout::MakeAbsNanos() const {
98
0
  if (!has_timeout()) {
99
0
    return kMaxNanos;
100
0
  }
101
102
0
  int64_t nanos = RawAbsNanos();
103
104
0
  if (is_relative_timeout()) {
105
    // We need to change epochs, because the relative timeout might be
106
    // represented by an absolute timestamp from another clock.
107
0
    nanos = std::max<int64_t>(nanos - SteadyClockNow(), 0);
108
0
    int64_t now = absl::GetCurrentTimeNanos();
109
0
    if (nanos > kMaxNanos - now) {
110
      // Overflow.
111
0
      nanos = kMaxNanos;
112
0
    } else {
113
0
      nanos += now;
114
0
    }
115
0
  } else if (nanos == 0) {
116
    // Some callers have assumed that 0 means no timeout, so instead we return a
117
    // time of 1 nanosecond after the epoch.
118
0
    nanos = 1;
119
0
  }
120
121
0
  return nanos;
122
0
}
123
124
0
int64_t KernelTimeout::InNanosecondsFromNow() const {
125
0
  if (!has_timeout()) {
126
0
    return kMaxNanos;
127
0
  }
128
129
0
  int64_t nanos = RawAbsNanos();
130
0
  if (is_absolute_timeout()) {
131
0
    return std::max<int64_t>(nanos - absl::GetCurrentTimeNanos(), 0);
132
0
  }
133
0
  return std::max<int64_t>(nanos - SteadyClockNow(), 0);
134
0
}
135
136
0
struct timespec KernelTimeout::MakeAbsTimespec() const {
137
0
  return absl::ToTimespec(absl::Nanoseconds(MakeAbsNanos()));
138
0
}
139
140
0
struct timespec KernelTimeout::MakeRelativeTimespec() const {
141
0
  return absl::ToTimespec(absl::Nanoseconds(InNanosecondsFromNow()));
142
0
}
143
144
#ifndef _WIN32
145
0
struct timespec KernelTimeout::MakeClockAbsoluteTimespec(clockid_t c) const {
146
0
  if (!has_timeout()) {
147
0
    return absl::ToTimespec(absl::Nanoseconds(kMaxNanos));
148
0
  }
149
150
0
  int64_t nanos = RawAbsNanos();
151
0
  if (is_absolute_timeout()) {
152
0
    nanos -= absl::GetCurrentTimeNanos();
153
0
  } else {
154
0
    nanos -= SteadyClockNow();
155
0
  }
156
157
0
  struct timespec now;
158
0
  ABSL_RAW_CHECK(clock_gettime(c, &now) == 0, "clock_gettime() failed");
159
0
  absl::Duration from_clock_epoch =
160
0
      absl::DurationFromTimespec(now) + absl::Nanoseconds(nanos);
161
0
  if (from_clock_epoch <= absl::ZeroDuration()) {
162
    // Some callers have assumed that 0 means no timeout, so instead we return a
163
    // time of 1 nanosecond after the epoch. For safety we also do not return
164
    // negative values.
165
0
    return absl::ToTimespec(absl::Nanoseconds(1));
166
0
  }
167
0
  return absl::ToTimespec(from_clock_epoch);
168
0
}
169
#endif
170
171
0
KernelTimeout::DWord KernelTimeout::InMillisecondsFromNow() const {
172
0
  constexpr DWord kInfinite = std::numeric_limits<DWord>::max();
173
174
0
  if (!has_timeout()) {
175
0
    return kInfinite;
176
0
  }
177
178
0
  constexpr uint64_t kNanosInMillis = uint64_t{1'000'000};
179
0
  constexpr uint64_t kMaxValueNanos =
180
0
      std::numeric_limits<int64_t>::max() - kNanosInMillis + 1;
181
182
0
  uint64_t ns_from_now = static_cast<uint64_t>(InNanosecondsFromNow());
183
0
  if (ns_from_now >= kMaxValueNanos) {
184
    // Rounding up would overflow.
185
0
    return kInfinite;
186
0
  }
187
  // Convert to milliseconds, always rounding up.
188
0
  uint64_t ms_from_now = (ns_from_now + kNanosInMillis - 1) / kNanosInMillis;
189
0
  if (ms_from_now > kInfinite) {
190
0
    return kInfinite;
191
0
  }
192
0
  return static_cast<DWord>(ms_from_now);
193
0
}
194
195
std::chrono::time_point<std::chrono::system_clock>
196
0
KernelTimeout::ToChronoTimePoint() const {
197
0
  if (!has_timeout()) {
198
0
    return std::chrono::time_point<std::chrono::system_clock>::max();
199
0
  }
200
201
  // The cast to std::microseconds is because (on some platforms) the
202
  // std::ratio used by std::chrono::steady_clock doesn't convert to
203
  // std::nanoseconds, so it doesn't compile.
204
0
  auto micros = std::chrono::duration_cast<std::chrono::microseconds>(
205
0
      std::chrono::nanoseconds(MakeAbsNanos()));
206
0
  return std::chrono::system_clock::from_time_t(0) + micros;
207
0
}
208
209
0
std::chrono::nanoseconds KernelTimeout::ToChronoDuration() const {
210
0
  if (!has_timeout()) {
211
0
    return std::chrono::nanoseconds::max();
212
0
  }
213
0
  return std::chrono::nanoseconds(InNanosecondsFromNow());
214
0
}
215
216
}  // namespace synchronization_internal
217
ABSL_NAMESPACE_END
218
}  // namespace absl