diff options
Diffstat (limited to 'runtime/src/crt_timed_wait.cpp')
| -rw-r--r-- | runtime/src/crt_timed_wait.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/runtime/src/crt_timed_wait.cpp b/runtime/src/crt_timed_wait.cpp new file mode 100644 index 000000000..6441f2868 --- /dev/null +++ b/runtime/src/crt_timed_wait.cpp | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | // The timed condition-variable wait of every runtime binary built against the dynamic CRT (/MD), routed through | ||
| 2 | // msvcp140.dll so it uses the same _Mtx_t layout as the mutex it unlocks. | ||
| 3 | // | ||
| 4 | // In the 14.4x STL, condition_variable::wait_for/wait_until call _Cnd_timedwait_for_unchecked. <xthreads.h> declares | ||
| 5 | // it without _CRTIMP2_PURE, so it is not imported: it is linked statically from msvcprt.lib (its sharedmutex.obj), | ||
| 6 | // and it unlocks the mutex by reaching into Windows' _Mtx_t layout for its SRW lock. _Mtx_lock/_Mtx_unlock are | ||
| 7 | // imported from msvcp140.dll. Wine's builtin msvcp140 laid _Mtx_t out differently until wine-10.1 ("msvcp140: Fix | ||
| 8 | // field order in _Mtx_t", 757bd7909fda), so on Wine 9.0 (Ubuntu 24.04's wine) a timed wait released nothing and | ||
| 9 | // kept the mutex for its whole timeout: the thread that would set the predicate could not take the lock, and every | ||
| 10 | // wait_for timed out instead of being woken (issue #3796, measured 2000 ms vs 0.3 ms for a 2 s wait). | ||
| 11 | // | ||
| 12 | // Defining the public symbols of that msvcprt.lib member here keeps it out of the link. The timed wait then goes | ||
| 13 | // through msvcp140's exported _Cnd_timedwait, which locks and waits on its own layout, whatever DLL provides it | ||
| 14 | // (Windows, any Wine). The other definitions are the ones the member would bring in, with the same behaviour: | ||
| 15 | // std::shared_mutex on an SRW lock, and this_thread::sleep_for. Only /MD targets get this file (CMakeLists.txt); | ||
| 16 | // a /MT target links the whole STL statically, so its mutex and wait already agree. No game files. | ||
| 17 | #include <windows.h> | ||
| 18 | |||
| 19 | #include <time.h> | ||
| 20 | #include <xthreads.h> | ||
| 21 | |||
| 22 | extern "C" { | ||
| 23 | |||
| 24 | // Exported by every msvcp140.dll, Windows' and Wine's; no longer declared by the 14.4x headers. The deadline is | ||
| 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; | ||
| 27 | |||
| 28 | _Thrd_result __stdcall _Cnd_timedwait_for_unchecked(_Cnd_t cond, _Mtx_t mtx, unsigned int ms) noexcept { | ||
| 29 | _timespec64 deadline{}; | ||
| 30 | if (_timespec64_get(&deadline, TIME_UTC) != TIME_UTC) deadline = {}; | ||
| 31 | deadline.tv_sec += ms / 1000; | ||
| 32 | deadline.tv_nsec += static_cast<long>(ms % 1000) * 1000000L; | ||
| 33 | if (deadline.tv_nsec >= 1000000000L) { | ||
| 34 | deadline.tv_sec += 1; | ||
| 35 | deadline.tv_nsec -= 1000000000L; | ||
| 36 | } | ||
| 37 | // A spurious or early return is allowed ("unchecked"): the STL's callers check the clock themselves. | ||
| 38 | return static_cast<_Thrd_result>(_Cnd_timedwait(cond, mtx, &deadline)); | ||
| 39 | } | ||
| 40 | |||
| 41 | // 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 { | ||
| 43 | return _Cnd_timedwait_for_unchecked(cond, mtx, ms); | ||
| 44 | } | ||
| 45 | |||
| 46 | void __cdecl _Smtx_lock_exclusive(_Smtx_t* lock) noexcept { | ||
| 47 | AcquireSRWLockExclusive(reinterpret_cast<PSRWLOCK>(lock)); | ||
| 48 | } | ||
| 49 | |||
| 50 | void __cdecl _Smtx_lock_shared(_Smtx_t* lock) noexcept { | ||
| 51 | AcquireSRWLockShared(reinterpret_cast<PSRWLOCK>(lock)); | ||
| 52 | } | ||
| 53 | |||
| 54 | int __cdecl _Smtx_try_lock_exclusive(_Smtx_t* lock) noexcept { | ||
| 55 | return TryAcquireSRWLockExclusive(reinterpret_cast<PSRWLOCK>(lock)); | ||
| 56 | } | ||
| 57 | |||
| 58 | int __cdecl _Smtx_try_lock_shared(_Smtx_t* lock) noexcept { | ||
| 59 | return TryAcquireSRWLockShared(reinterpret_cast<PSRWLOCK>(lock)); | ||
| 60 | } | ||
| 61 | |||
| 62 | void __cdecl _Smtx_unlock_exclusive(_Smtx_t* lock) noexcept { | ||
| 63 | ReleaseSRWLockExclusive(reinterpret_cast<PSRWLOCK>(lock)); | ||
| 64 | } | ||
| 65 | |||
| 66 | void __cdecl _Smtx_unlock_shared(_Smtx_t* lock) noexcept { | ||
| 67 | ReleaseSRWLockShared(reinterpret_cast<PSRWLOCK>(lock)); | ||
| 68 | } | ||
| 69 | |||
| 70 | void __stdcall _Thrd_sleep_for(unsigned long ms) noexcept { | ||
| 71 | Sleep(ms); | ||
| 72 | } | ||
| 73 | |||
| 74 | } // extern "C" | ||