Repository navigation
runtime: a timed condition-variable wait releases its mutex on Wine 9 - #6847
Conversation
On Wine 9.0 (Ubuntu 24.04's wine) every std::condition_variable wait_for / wait_until in a runtime binary kept its mutex for the whole timeout: the 14.4x STL links _Cnd_timedwait_for_unchecked statically from msvcprt.lib (sharedmutex.obj), and it unlocks through Windows' _Mtx_t layout, while _Mtx_lock/_Mtx_unlock come from Wine's builtin msvcp140, whose layout differed until wine-10.1 (757bd7909fda). Diagnosed by ray3242 in ItsNotRexed#3796. Every /MD executable and DLL now gets src/crt_timed_wait.cpp, which defines the public symbols of that member, so it stays out of the link: the timed wait goes through msvcp140's exported _Cnd_timedwait, which uses its own layout on Windows and on every Wine, and shared_mutex / sleep_for keep the same SRW-lock and Sleep behaviour. /MT targets and the /NODEFAULTLIB fixtures are left alone. crt_timed_wait_test checks a wait is woken by a thread taking its mutex, an unnotified wait times out, and shared_mutex and sleep_for still work. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014W1f1QexoRrV2Dg68CN4cD
|
Independent native Windows check of 6c4f6e0b with MSVC 19.44 and the dynamic CRT ( One test portability issue remains in the shared-lock check. The thread calls AI-assisted. |
|
Tested
Not covered: Wine (the bug this fixes), the Debug configuration ( |
|
Native Windows check of
AI-assisted (Claude Code). No game files. |
shayderrr
left a comment
There was a problem hiding this comment.
Summary
New runtime/src/crt_timed_wait.cpp defines _Cnd_timedwait_for_unchecked/_Cnd_timedwait_for to call msvcp140's exported _Cnd_timedwait with an absolute TIME_UTC deadline, plus _Smtx_* and _Thrd_sleep_for; runtime/CMakeLists.txt links that object into dynamic-CRT Windows targets, and crt_timed_wait_test plus a docs/testing.md row cover it.
Why merge
Fixes Wine 9 holding a condition-variable mutex for the entire timeout, which stalled every timed queue/fence/present wait, without giving up the shared host CRT. Broadly verified on Wine 9 and 11.
Verdict: Approve
|
Summary Why merge Verdict: Approve |
Item: new RT item claimed in #6414 (claim), follow-up of @ray3242's diagnosis in #3796 (released there without a fix).
What
On Wine 9.0 (the
wineof Ubuntu 24.04 LTS, and thewine (ubuntu)CI job) everystd::condition_variable::wait_for/wait_untilin a runtime binary keeps its mutex for the whole timeout. The thread that would set the predicate can't take the lock, so every wait times out instead of being woken. In a game, that means stalls and timeouts in every queue, fence or present path that waits with a timeout.Cause (as @ray3242 found): the 14.4x STL calls
_Cnd_timedwait_for_unchecked, which<xthreads.h>declares without_CRTIMP2_PURE. It is therefore linked statically frommsvcprt.lib(sharedmutex.obj) and unlocks through Windows'_Mtx_tlayout, while_Mtx_lock/_Mtx_unlockare imported from msvcp140. Wine's builtin msvcp140 used a different_Mtx_tlayout until wine-10.1 (757bd7909fda, "msvcp140: Fix field order in _Mtx_t").Fix
#3796 ruled out linking the CRT statically (
gta6rtmust share the host CRT with the mapped game). This PR keeps/MDand only takes that one library member out of the link:runtime/src/crt_timed_wait.cppdefines the public symbols ofsharedmutex.obj. The linker then never pulls it in, and no duplicate symbols are possible._Cnd_timedwait_for_unchecked(and the older_Cnd_timedwait_for) go through msvcp140's exported_Cnd_timedwait, with an absoluteTIME_UTCdeadline._Smtx_*(std::shared_mutex) and_Thrd_sleep_forkeep the member's behaviour: SRW lock andSleep.runtime/CMakeLists.txt: at the end, every Windows executable/DLL built against the dynamic CRT gets the object. Left alone:MSVC_RUNTIME_LIBRARY(/MT), whose mutex and wait already agree;/NODEFAULTLIBfixtures, which have no CRT.runtime/test/crt_timed_wait_test.cpp(ctestcrt_timed_wait) checks five things:wait_for(5 s)is woken by a thread that takes its mutex, sets the flag and notifies;wait_until;shared_mutexshared/exclusive ownership;sleep_for.docs/testing.md: a Known-problems row.The symbols and signatures come from the 14.44 headers and from
llvm-nmonmsvcprt.lib. The return codes and clock (TIME_UTC,_Timedout= 2) match both Microsoft's and Wine's_Cnd_timedwait(dlls/msvcp90/misc.c).Tests
Ubuntu 24.04, NVIDIA RTX 4090 (proprietary driver, Vulkan 1.4.318), real Wayland display. Runtime cross-built with clang-cl/lld-link 19.1.1 against xwin 0.10.0 (SDK 10.0.26100, MSVC 14.44.17.14).
crt_timed_wait_testalone, Wine 9.0 (9.0~repack-4build3):wait_for(5 s) returned after 5000 ms; the setter waited 4949 ms for the mutex, 4 failures;returned after 50 ms; the setter waited 0 ms, all pass.ctestunder Wine 9.0:wait_registration_admission,wait_registration_reentrant_admission,context_concurrent_detach,vk_presenter_sync,vk_fence_dispatcher_sync,frame_pacer. Rerun alone, these six pluscrt_timed_wait: 7/7 pass.xbox_presentfailed in that run withpresent failed (VkResult -11), but it fails the same way built from untouchedmainon this machine (display-dependent), so it is not caused by this change._Cnd_timedwaitinstead of the static copy; a Windows ctest run from a reviewer would be welcome.No game files.
AI-assisted (Claude Code, Claude Opus 5.5).
🤖 Generated with Claude Code
https://claude.ai/code/session_014W1f1QexoRrV2Dg68CN4cD