Skip to content

Commit 313964b

Browse files
authored
<semaphore>: use steady_clock (#5575)
1 parent d07e2cc commit 313964b

File tree

3 files changed

+6
-16
lines changed

3 files changed

+6
-16
lines changed

stl/inc/semaphore

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ _STL_DISABLE_CLANG_WARNINGS
2929

3030
_STD_BEGIN
3131

32-
template <class _Rep, class _Period>
33-
_NODISCARD unsigned long long _Semaphore_deadline(const chrono::duration<_Rep, _Period>& _Rel_time) {
34-
return __std_atomic_wait_get_deadline(
35-
chrono::duration_cast<chrono::duration<unsigned long long, milli>>(_Rel_time).count());
36-
}
37-
3832
template <class _Clock, class _Duration>
3933
_NODISCARD unsigned long _Semaphore_remaining_timeout(const chrono::time_point<_Clock, _Duration>& _Abs_time) {
4034
const auto _Now = _Clock::now();
@@ -145,11 +139,11 @@ public:
145139

146140
template <class _Rep, class _Period>
147141
_NODISCARD_TRY_CHANGE_STATE bool try_acquire_for(const chrono::duration<_Rep, _Period>& _Rel_time) {
148-
auto _Deadline = _Semaphore_deadline(_Rel_time);
142+
auto _Deadline = _STD chrono::steady_clock::now() + _Rel_time;
149143
ptrdiff_t _Current = _Counter.load(memory_order_relaxed);
150144
for (;;) {
151145
while (_Current == 0) {
152-
const auto _Remaining_timeout = __std_atomic_wait_get_remaining_timeout(_Deadline);
146+
const auto _Remaining_timeout = _Semaphore_remaining_timeout(_Deadline);
153147
if (_Remaining_timeout == 0) {
154148
return false;
155149
}
@@ -257,7 +251,7 @@ public:
257251

258252
template <class _Rep, class _Period>
259253
_NODISCARD_TRY_CHANGE_STATE bool try_acquire_for(const chrono::duration<_Rep, _Period>& _Rel_time) {
260-
auto _Deadline = _Semaphore_deadline(_Rel_time);
254+
auto _Deadline = _STD chrono::steady_clock::now() + _Rel_time;
261255
for (;;) {
262256
// "happens after release" ordering is provided by this exchange, so loads and waits can be relaxed
263257
// TRANSITION, GH-1133: should be memory_order_acquire
@@ -267,7 +261,7 @@ public:
267261
}
268262
_STL_VERIFY(_Prev == 0, "Invariant: semaphore counter is non-negative and doesn't exceed max(), "
269263
"possibly caused by memory corruption");
270-
const auto _Remaining_timeout = __std_atomic_wait_get_remaining_timeout(_Deadline);
264+
const auto _Remaining_timeout = _Semaphore_remaining_timeout(_Deadline);
271265
if (_Remaining_timeout == 0) {
272266
return false;
273267
}

stl/inc/xatomic_wait.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ int __stdcall __std_atomic_wait_indirect(const void* _Storage, void* _Comparand,
4040
void __stdcall __std_atomic_notify_one_indirect(const void* _Storage) noexcept;
4141
void __stdcall __std_atomic_notify_all_indirect(const void* _Storage) noexcept;
4242

43-
// These functions convert a duration into a time point in order to tolerate spurious wakes in atomic wait, and then
44-
// convert back from the time point to individual wait attempts (which are limited by DWORD milliseconds to a length of
45-
// ~49 days)
46-
unsigned long long __stdcall __std_atomic_wait_get_deadline(unsigned long long _Timeout) noexcept;
47-
unsigned long __stdcall __std_atomic_wait_get_remaining_timeout(unsigned long long _Deadline) noexcept;
48-
4943
} // extern "C"
5044

5145
#pragma pop_macro("new")

stl/src/atomic_wait.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ int __stdcall __std_atomic_wait_indirect(const void* _Storage, void* _Comparand,
177177
}
178178
}
179179

180+
// TRANSITION, ABI: preserved for binary compatibility
180181
unsigned long long __stdcall __std_atomic_wait_get_deadline(const unsigned long long _Timeout) noexcept {
181182
if (_Timeout == _Atomic_wait_no_deadline) {
182183
return _Atomic_wait_no_deadline;
@@ -185,6 +186,7 @@ unsigned long long __stdcall __std_atomic_wait_get_deadline(const unsigned long
185186
}
186187
}
187188

189+
// TRANSITION, ABI: preserved for binary compatibility
188190
unsigned long __stdcall __std_atomic_wait_get_remaining_timeout(unsigned long long _Deadline) noexcept {
189191
static_assert(__std_atomic_wait_no_timeout == INFINITE,
190192
"__std_atomic_wait_no_timeout is passed directly to underlying API, so should match it");

0 commit comments

Comments
 (0)