fix(shader): lock recompiler shared mutexes once when created #3593
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3593"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-11T13:56:00Z.
What
On Windows,
std::shared_mutexfrom libstdc++ is a winpthreadspthread_rwlock_tset toPTHREAD_RWLOCK_INITIALIZER, which winpthreads initializes on its first lock. When two threads take that first lock at once, the one that loses the initialization getsEINVALwithout holding the lock. libstdc++'slock()andlock_shared()only checkEDEADLKand assert success, which is compiled out in Release, so that thread enters the critical section anyway (TechnicalDebt, Build).Guest rwlocks already avoid this by locking once when created (
PthreadRwlockPrivateinPthread.hpp). This applies the same to the four host shared mutexes inRecompiler.cpp: the source cache ingetSource, the binding plan cache inpreparedBindingPlanand the per-planPreparedBindingPlan::mutex, and the module cache inspecializeModule. They are now aPrimedSharedMutex, astd::shared_mutexthat locks and unlocks in its constructor. The static ones are constructed under the function-local static guard and the per-plan one before the plan is published, so no thread can lock them before that. Nothing else changes; on Linux and macOS the first lock never fails, so the extra lock and unlock in each constructor is the only difference.The TechnicalDebt entry also listed the mutex in
GuestWriteWatch.cpp. It is only in the Linux watch (#if defined(__linux__) && ...), so it is not affected and the entry no longer lists it.No new test: the failure needs threads racing the first lock of a fresh mutex on winpthreads. A test would pass on CI's Linux runners whatever the code does, and on Windows it fails only in some runs, so it would be flaky rather than a regression guard. The repro is in the first comment.
Tested
-O2 -static, run under Wine 9.0. 8 threads take the first lock of a fresh mutex and each stays inside for 2 ms; 250 trials:pthread_rwlock_wrlockonPTHREAD_RWLOCK_INITIALIZER:EINVALin 38 trialsstd::shared_mutex: two writers inside at once in 26 trialsPrimedSharedMutex: 0 trialsRecompiler.cppcompiles with GCC 13.3 (Linux) and MinGW-w64 GCC 13.2 posix (-std=c++20 -Wall -fsyntax-only), no warnings from the file.tools/check_conventions.py --base upstream/mainpasses.ctestwith WinLibs GCC 15.2.0, Linux runtime tests, titles or homebrew. The repro used GCC 13.2's winpthreads, not WinLibs 15.2's.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-11T14:09:21Z.
Repro for the numbers in "Tested" (MinGW-w64 GCC 13.2 posix,
-std=c++20 -O2 -static, run asrace.exe 250):Imported from boykopovar/AnyPS5. Originally posted by jweidenh on 2026-10-11T16:11:47Z.
Native Windows confirmation on the supported WinLibs toolchain: this reproduces outside Wine.
Windows 11 build 26300, Ryzen 7 5800X, WinLibs GCC 15.2.0 r7 UCRT/POSIX/SEH. I compiled the exact posted source and ran it with
250(eight workers, unchanged 2 ms writer hold). Each number below is affected trials out of 250:-std=c++20 -O2 -static-std=c++20 -O3 -DNDEBUG, dynamic runtimeI also ran each arm in its own process using the same race function bodies, with a 30-second deadline and explicit result/exit capture. Static results were 249 / 250 / 0; dynamic results were 249 / 246 / 0. All eight measurement processes completed, none timed out; the longest took 7.812 seconds. The dynamic isolated probes confirmed the loaded libstdc++ and winpthreads DLLs came from the pinned WinLibs directory.
At the exact authored head
48dcaace7868a55a831b196c46464163b816aa76, a Release build with SPIRV-Tools enabled passed these six tests on the RTX 5090, NVIDIA 617.42, serially with a 30-second per-test timeout:agc_shader_frontend_pairand_offagc_driver_prepared_exportsagc_driver_prepared_shaders,_indirect, and_uncachedAll six completed in 5.77 seconds total, with no skips or
FATAL:output and all expected completion markers. Vulkan was required and the log confirmed the RTX 5090 was selected. Shader disk caching was disabled.These results confirm the first-lock writer exclusion problem and the priming mechanism on this native toolchain. The probe does not cover reader-first/mixed contention or establish a specific title crash as fixed; the runtime tests are regression coverage, not a deterministic shader-cache race reproducer. No driver or system configuration was changed. AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by ahir061 on 2026-10-11T20:07:39Z.
@jweidenh thanks for running it natively on WinLibs 15.2, that's the toolchain I couldn't test.
On reader-first and mixed contention:
pthread_rwlock_rdlockgoes through the same lazy initialization aspthread_rwlock_wrlockin winpthreads, so a reader can also getEINVALon the first lock and proceed unlocked. Priming in the constructor initializes the rwlock before any reader or writer can reach it. I checked this with a mixed variant of the repro (same 2 ms hold, 8 threads, fresh mutex per trial, under Wine with MinGW-w64 GCC 13.2 posix, 150 trials):std::shared_mutexPrimedSharedMutexAgreed that none of this establishes a specific title crash as fixed; the PR doesn't claim one.
mixed.cpp
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T03:50:16Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)docs/dev/TechnicalDebt.mdL10