upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/crt_timed_wait.cpp
diff options
context:
space:
mode:
authorMatic <maticcukmikelnalt@gmail.com>2026-11-24 15:19:20 +0100
committerMatic <maticcukmikelnalt@gmail.com>2026-11-24 15:48:18 +0100
commitbc9d0a5f8f82ed31be2855bccf6ce44fe9c21f25 (patch)
tree156eced486cb03a433caf7b9a1e47ea6325cbccc /runtime/src/crt_timed_wait.cpp
parentf3e26be7c661e97eb4c70464e675a3b32dc149e3 (diff)
runtime: map early OS ERROR_TIMEOUT to _Timedout in _Cnd_timedwait_for_unchecked
Diffstat (limited to 'runtime/src/crt_timed_wait.cpp')
-rw-r--r--runtime/src/crt_timed_wait.cpp29
1 files changed, 25 insertions, 4 deletions
diff --git a/runtime/src/crt_timed_wait.cpp b/runtime/src/crt_timed_wait.cpp
index 6441f2868..cd26e7d3a 100644
--- a/runtime/src/crt_timed_wait.cpp
+++ b/runtime/src/crt_timed_wait.cpp
@@ -25,7 +25,9 @@ extern "C" {
25// absolute, in TIME_UTC seconds and nanoseconds, and the result uses _Thrd_result's values (0, or 2 on timeout). 25// absolute, in TIME_UTC seconds and nanoseconds, and the result uses _Thrd_result's values (0, or 2 on timeout).
26__declspec(dllimport) int __cdecl _Cnd_timedwait(_Cnd_t, _Mtx_t, const _timespec64*) noexcept; 26__declspec(dllimport) int __cdecl _Cnd_timedwait(_Cnd_t, _Mtx_t, const _timespec64*) noexcept;
27 27
28_Thrd_result __stdcall _Cnd_timedwait_for_unchecked(_Cnd_t cond, _Mtx_t mtx, unsigned int ms) noexcept { 28namespace {
29
30_Thrd_result timedwait_for_impl(_Cnd_t cond, _Mtx_t mtx, unsigned int ms, bool unchecked) noexcept {
29 _timespec64 deadline{}; 31 _timespec64 deadline{};
30 if (_timespec64_get(&deadline, TIME_UTC) != TIME_UTC) deadline = {}; 32 if (_timespec64_get(&deadline, TIME_UTC) != TIME_UTC) deadline = {};
31 deadline.tv_sec += ms / 1000; 33 deadline.tv_sec += ms / 1000;
@@ -34,13 +36,32 @@ _Thrd_result __stdcall _Cnd_timedwait_for_unchecked(_Cnd_t cond, _Mtx_t mtx, uns
34 deadline.tv_sec += 1; 36 deadline.tv_sec += 1;
35 deadline.tv_nsec -= 1000000000L; 37 deadline.tv_nsec -= 1000000000L;
36 } 38 }
37 // A spurious or early return is allowed ("unchecked"): the STL's callers check the clock themselves. 39 // Preserve the caller's LastError across the wait while detecting OS timer timeouts cleanly.
38 return static_cast<_Thrd_result>(_Cnd_timedwait(cond, mtx, &deadline)); 40 const DWORD saved_error = GetLastError();
41 SetLastError(ERROR_SUCCESS);
42 const auto res = static_cast<_Thrd_result>(_Cnd_timedwait(cond, mtx, &deadline));
43 const DWORD wait_error = GetLastError();
44 SetLastError(saved_error);
45 // msvcp140's _Cnd_timedwait checks _Xtime_diff_to_millis2(target, &now) == 0 after SleepConditionVariableSRW
46 // returns FALSE/ERROR_TIMEOUT, so an OS timer wake even <1 ms before `deadline` turns into _Thrd_result::_Success.
47 // STL wait_for/wait_until callers treat _Success from _Cnd_timedwait_for_unchecked as a notification/spurious
48 // wake (cv_status::no_timeout) instead of looping until steady_clock reaches the target; report _Timedout here
49 // so the caller checks steady_clock and waits for the remaining time.
50 if (unchecked && res == _Thrd_result::_Success && wait_error == ERROR_TIMEOUT) {
51 return _Thrd_result::_Timedout;
52 }
53 return res;
54}
55
56} // namespace
57
58_Thrd_result __stdcall _Cnd_timedwait_for_unchecked(_Cnd_t cond, _Mtx_t mtx, unsigned int ms) noexcept {
59 return timedwait_for_impl(cond, mtx, ms, true);
39} 60}
40 61
41// Not declared by the 14.4x headers, but defined by the same member: keep a caller of the older name off it too. 62// Not declared by the 14.4x headers, but defined by the same member: keep a caller of the older name off it too.
42_Thrd_result __stdcall _Cnd_timedwait_for(_Cnd_t cond, _Mtx_t mtx, unsigned int ms) noexcept { 63_Thrd_result __stdcall _Cnd_timedwait_for(_Cnd_t cond, _Mtx_t mtx, unsigned int ms) noexcept {
43 return _Cnd_timedwait_for_unchecked(cond, mtx, ms); 64 return timedwait_for_impl(cond, mtx, ms, false);
44} 65}
45 66
46void __cdecl _Smtx_lock_exclusive(_Smtx_t* lock) noexcept { 67void __cdecl _Smtx_lock_exclusive(_Smtx_t* lock) noexcept {