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
blob: cd26e7d3a83b6be0f6fbe8e9513c6510c8fb82cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// The timed condition-variable wait of every runtime binary built against the dynamic CRT (/MD), routed through
// msvcp140.dll so it uses the same _Mtx_t layout as the mutex it unlocks.
//
// In the 14.4x STL, condition_variable::wait_for/wait_until call _Cnd_timedwait_for_unchecked. <xthreads.h> declares
// it without _CRTIMP2_PURE, so it is not imported: it is linked statically from msvcprt.lib (its sharedmutex.obj),
// and it unlocks the mutex by reaching into Windows' _Mtx_t layout for its SRW lock. _Mtx_lock/_Mtx_unlock are
// imported from msvcp140.dll. Wine's builtin msvcp140 laid _Mtx_t out differently until wine-10.1 ("msvcp140: Fix
// field order in _Mtx_t", 757bd7909fda), so on Wine 9.0 (Ubuntu 24.04's wine) a timed wait released nothing and
// kept the mutex for its whole timeout: the thread that would set the predicate could not take the lock, and every
// wait_for timed out instead of being woken (issue #3796, measured 2000 ms vs 0.3 ms for a 2 s wait).
//
// Defining the public symbols of that msvcprt.lib member here keeps it out of the link. The timed wait then goes
// through msvcp140's exported _Cnd_timedwait, which locks and waits on its own layout, whatever DLL provides it
// (Windows, any Wine). The other definitions are the ones the member would bring in, with the same behaviour:
// std::shared_mutex on an SRW lock, and this_thread::sleep_for. Only /MD targets get this file (CMakeLists.txt);
// a /MT target links the whole STL statically, so its mutex and wait already agree. No game files.
#include <windows.h>

#include <time.h>
#include <xthreads.h>

extern "C" {

// Exported by every msvcp140.dll, Windows' and Wine's; no longer declared by the 14.4x headers. The deadline is
// absolute, in TIME_UTC seconds and nanoseconds, and the result uses _Thrd_result's values (0, or 2 on timeout).
__declspec(dllimport) int __cdecl _Cnd_timedwait(_Cnd_t, _Mtx_t, const _timespec64*) noexcept;

namespace {

_Thrd_result timedwait_for_impl(_Cnd_t cond, _Mtx_t mtx, unsigned int ms, bool unchecked) noexcept {
    _timespec64 deadline{};
    if (_timespec64_get(&deadline, TIME_UTC) != TIME_UTC) deadline = {};
    deadline.tv_sec += ms / 1000;
    deadline.tv_nsec += static_cast<long>(ms % 1000) * 1000000L;
    if (deadline.tv_nsec >= 1000000000L) {
        deadline.tv_sec += 1;
        deadline.tv_nsec -= 1000000000L;
    }
    // Preserve the caller's LastError across the wait while detecting OS timer timeouts cleanly.
    const DWORD saved_error = GetLastError();
    SetLastError(ERROR_SUCCESS);
    const auto res = static_cast<_Thrd_result>(_Cnd_timedwait(cond, mtx, &deadline));
    const DWORD wait_error = GetLastError();
    SetLastError(saved_error);
    // msvcp140's _Cnd_timedwait checks _Xtime_diff_to_millis2(target, &now) == 0 after SleepConditionVariableSRW
    // returns FALSE/ERROR_TIMEOUT, so an OS timer wake even <1 ms before `deadline` turns into _Thrd_result::_Success.
    // STL wait_for/wait_until callers treat _Success from _Cnd_timedwait_for_unchecked as a notification/spurious
    // wake (cv_status::no_timeout) instead of looping until steady_clock reaches the target; report _Timedout here
    // so the caller checks steady_clock and waits for the remaining time.
    if (unchecked && res == _Thrd_result::_Success && wait_error == ERROR_TIMEOUT) {
        return _Thrd_result::_Timedout;
    }
    return res;
}

}  // namespace

_Thrd_result __stdcall _Cnd_timedwait_for_unchecked(_Cnd_t cond, _Mtx_t mtx, unsigned int ms) noexcept {
    return timedwait_for_impl(cond, mtx, ms, true);
}

// Not declared by the 14.4x headers, but defined by the same member: keep a caller of the older name off it too.
_Thrd_result __stdcall _Cnd_timedwait_for(_Cnd_t cond, _Mtx_t mtx, unsigned int ms) noexcept {
    return timedwait_for_impl(cond, mtx, ms, false);
}

void __cdecl _Smtx_lock_exclusive(_Smtx_t* lock) noexcept {
    AcquireSRWLockExclusive(reinterpret_cast<PSRWLOCK>(lock));
}

void __cdecl _Smtx_lock_shared(_Smtx_t* lock) noexcept {
    AcquireSRWLockShared(reinterpret_cast<PSRWLOCK>(lock));
}

int __cdecl _Smtx_try_lock_exclusive(_Smtx_t* lock) noexcept {
    return TryAcquireSRWLockExclusive(reinterpret_cast<PSRWLOCK>(lock));
}

int __cdecl _Smtx_try_lock_shared(_Smtx_t* lock) noexcept {
    return TryAcquireSRWLockShared(reinterpret_cast<PSRWLOCK>(lock));
}

void __cdecl _Smtx_unlock_exclusive(_Smtx_t* lock) noexcept {
    ReleaseSRWLockExclusive(reinterpret_cast<PSRWLOCK>(lock));
}

void __cdecl _Smtx_unlock_shared(_Smtx_t* lock) noexcept {
    ReleaseSRWLockShared(reinterpret_cast<PSRWLOCK>(lock));
}

void __stdcall _Thrd_sleep_for(unsigned long ms) noexcept {
    Sleep(ms);
}

}  // extern "C"