Skip to content

runtime: a timed condition-variable wait releases its mutex on Wine 9 - #6847

Merged
shayderrr merged 1 commit into
ItsNotRexed:mainfrom
GeremC:wine9-timed-wait
Nov 23, 2026
Merged

shayderrr merged 1 commit into
ItsNotRexed:mainfrom
GeremC:wine9-timed-wait

Conversation

@GeremC

@GeremC GeremC commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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 wine of Ubuntu 24.04 LTS, and the wine (ubuntu) CI job) every std::condition_variable::wait_for / wait_until in 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 from msvcprt.lib (sharedmutex.obj) and unlocks through Windows' _Mtx_t layout, while _Mtx_lock/_Mtx_unlock are imported from msvcp140. Wine's builtin msvcp140 used a different _Mtx_t layout until wine-10.1 (757bd7909fda, "msvcp140: Fix field order in _Mtx_t").

Fix

#3796 ruled out linking the CRT statically (gta6rt must share the host CRT with the mapped game). This PR keeps /MD and only takes that one library member out of the link:

  • runtime/src/crt_timed_wait.cpp defines the public symbols of sharedmutex.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 absolute TIME_UTC deadline.
    • That export locks and waits on its own layout, whichever DLL provides it: Windows' msvcp140, or Wine's on any version.
    • _Smtx_* (std::shared_mutex) and _Thrd_sleep_for keep the member's behaviour: SRW lock and Sleep.
  • runtime/CMakeLists.txt: at the end, every Windows executable/DLL built against the dynamic CRT gets the object. Left alone:
    • targets with a static MSVC_RUNTIME_LIBRARY (/MT), whose mutex and wait already agree;
    • the /NODEFAULTLIB fixtures, which have no CRT.
  • runtime/test/crt_timed_wait_test.cpp (ctest crt_timed_wait) checks five things:
    • a wait_for(5 s) is woken by a thread that takes its mutex, sets the flag and notifies;
    • the same for wait_until;
    • an unnotified wait times out after about its timeout, with the mutex held again;
    • shared_mutex shared/exclusive ownership;
    • sleep_for.
  • docs/testing.md: a Known-problems row.

The symbols and signatures come from the 14.44 headers and from llvm-nm on msvcprt.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_test alone, Wine 9.0 (9.0~repack-4build3):
    • before: wait_for(5 s) returned after 5000 ms; the setter waited 4949 ms for the mutex, 4 failures;
    • after: returned after 50 ms; the setter waited 0 ms, all pass.
  • Same binaries under Wine 11.0 (Proton Experimental): both pass (50 ms), so newer Wine is unaffected and nothing regresses there.
  • Full ctest under Wine 9.0:
    • before: 197/214 passed, 17 failed;
    • after: 202/215 passed (the new test included), 12 failed.
    • Fixed: wait_registration_admission, wait_registration_reentrant_admission, context_concurrent_detach, vk_presenter_sync, vk_fence_dispatcher_sync, frame_pacer. Rerun alone, these six plus crt_timed_wait: 7/7 pass.
    • The remaining failures are unrelated (WARP/host differential tests, the abort reference fixed by tiny_game_abort_test: the native reference accepts Wine's abort() exit code 3 #6539, proxy crashes being looked at separately).
    • xbox_present failed in that run with present failed (VkResult -11), but it fails the same way built from untouched main on this machine (display-dependent), so it is not caused by this change.
  • Not run on Windows here (no Windows machine). On Windows the timed wait now goes through msvcp140's exported _Cnd_timedwait instead 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

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
@thehollowedone

Copy link
Copy Markdown

Independent native Windows check of 6c4f6e0b with MSVC 19.44 and the dynamic CRT (/MD). All 12 stock crt_timed_wait checks passed. The five-second wait woke after 50 ms, wait_until also woke, and the unnotified 200 ms wait timed out with the mutex reacquired. A standalone frame-pacer build linked to the same CRT wait code also passed.

One test portability issue remains in the shared-lock check. The thread calls try_lock_shared() while it already owns the mutex. The C++ precondition requires the caller to have no ownership, so a second reader thread would verify concurrent shared ownership without relying on recursive locking. C++ shared mutex requirements

AI-assisted.

@hazben

hazben commented Nov 23, 2026

Copy link
Copy Markdown

Tested 6c4f6e0b (as pushed, on merge base 8497c702) on NVIDIA GeForce RTX 4070 SUPER (Ada), driver 621.14, Windows 11 (build 26200), with native MSVC link.exe: pass. The one ctest failure also happens on main. AI-assisted run, no game files.

  • Build: MSVC 14.44 (VS 2022, cl 19.44.35230), Ninja, RelWithDebInfo, Vulkan SDK 1.4.363.0. Full build in a fresh build folder: 647 steps and 220 .exe/.dll links. The log has no LNK2005, LNK4006, LNK1169 or sharedmutex.
  • crt_timed_wait_test.exe run directly: all 12 checks PASS, RESULT: PASS. It printed wait_for(5 s) returned after 51 ms; the setter waited 0 ms for the mutex and an unnotified wait_for(200 ms) returned after 208 ms.
  • The seven tests named in the PR (crt_timed_wait, wait_registration_admission, wait_registration_reentrant_admission, context_concurrent_detach, vk_presenter_sync, vk_fence_dispatcher_sync, frame_pacer): 7/7 passed normally and 7/7 with LEONIDA_VK_VALIDATION=1. The GPU tests printed device: NVIDIA GeForce RTX 4070 SUPER ((validation on) in the second run), fence dispatcher: mark_device_lost() wakes blocked wait and teardown completes ok and presenter sync: synthetic WSI device-idle during swapchain destruction serialized ok.
  • dumpbin /imports: crt_timed_wait_test.exe, frame_pacer_test.exe, vk_presenter_sync_test.exe and gta6rt.exe all import _Cnd_timedwait from MSVCP140.dll, next to _Mtx_lock/_Mtx_unlock.
  • Link check: I compiled crt_timed_wait.cpp and crt_timed_wait_test.cpp on their own (cl /MD /O2 /std:c++20) and linked the test with link /VERBOSE twice, with and without the PR's crt_timed_wait.obj.
    • With the object, no sharedmutex.obj is loaded and _Cnd_timedwait is imported.
    • Without it (what main links), the log shows Loaded msvcprt.lib(sharedmutex.obj) and there is no _Cnd_timedwait import.
    • Both programs pass on Windows (returned after 56 ms and 57 ms). So on Windows this shows no regression; it can't show the Wine 9 bug.
  • Full ctest, because the object goes into every /MD binary. 252 tests are registered and gameinput is disabled.
    • Normally: 239 passed, 11 skipped, 1 failed.
    • With validation: 240 passed, 10 skipped, 1 failed (vk_sync_validation runs only with validation on).
    • The other skips, in both runs: shader_library_corpus, the four Bink tests, fsr_dispatch, bvh_builder, d3d12_rgb9e5_rtv, d3d12_boot_movie and d3d12_first_light.
    • xbox_present passed in both runs on the PC's own display.
    • The failure is d3d12_indirect_dispatch_count, in both runs: count 0 update 1: host 6/10, layer 0/0, expected 6/10. It fails the same way when re-run alone, and also on main b4adb7508 built in a separate worktree, so this PR does not cause it (d3d12_indirect_dispatch_count_test: its compute fixture reads the root UAV at s4:s5 (D3D-448 layout) #5792 is an open fix for that test).

Not covered: Wine (the bug this fixes), the Debug configuration (/MDd, msvcprtd.lib), a clang-cl build on Windows, Windows 10, and the game itself. The PR head was not merged onto current main for this run.

@BeegSchmooze

Copy link
Copy Markdown
Contributor

Native Windows check of 6c4f6e0b (the bug is Wine 9 only, and this changes runtime code, so this is the "unchanged on Windows" side): pass. NVIDIA GeForce RTX 4080 / 610.88, Windows 11 26200, MSVC 19.51 (VS 18), RelWithDebInfo.

  • Full ctest on the PR's tree: 217/217 passed, including the new crt_timed_wait (0.43 s). One test is disabled (gameinput, no SDL3).

AI-assisted (Claude Code). No game files.

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@shayderrr

Copy link
Copy Markdown
Collaborator

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

@shayderrr
shayderrr merged commit ee56bf9 into ItsNotRexed:main Nov 23, 2026
0 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants