fix(libkernel): make cond relock and pthread_once waits alertable on Windows #27985
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/27985"
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 kiansaeidnia on 2026-11-28T00:48:56Z.
What
On Windows,
sceKernelRaiseExceptionreaches a target in host code as an APC only while it is in a libkernel wait. Two guest-visible waits did not go through one, so a raise at a thread blocked in them was never delivered:scePthreadCondWait,scePthreadCondTimedwait,pthread_cond_waitandpthread_cond_timedwait, and for libc's libc++std::condition_variable, which forwards to them. After a wake,WakingLock::lockcalled the native mutex lock once spinning failed.pthread_oncewaiters. They blocked on astd::condition_variable.This is the shape of the Unity/IL2CPP stop-the-world freeze:
#1562 fixed this for
scePthreadMutexLockand rwlock waits after a measured God of War: Sons of Sparta (PPSA28997) freeze, and #10666 (Graveyard Keeper 2, PPSA35943) recorded the general gap.The change:
TimedWait::ParkAcquire, keyed and woken the wayscePthreadMutexLockis.WakingLock::unlockandscePthreadMutexUnlockalready unpark that key.pthread_oncewaits on aTimedWait::Condition.PCATCHsleeps, then a retry in libthr).TimedWait::Conditionis acondition_variable_any, sopthread_oncebehaves as before.The debt entry keeps its general wording about waits on an already stopped thread, and lists the waits that remain as examples:
pthread_barrier_wait,scePthreadOnce, libc_Mtx_lock,std::call_onceand__cxa_guard_acquire. Two of these are left out on purpose:scePthreadOnceis rewritten by #25283, and Dinkumware_Mtx_lock/_Cnd_waitby #18347.pr_overlapcould not compareOnce.cppwith #18190, because that PR conflicts withmain. Its change to the file is a rename ofpthread_once_nid_postfixto_nid_no_patchplus a forwarder, so whichever merges second needs a textual rebase. #26174 rewriteswaitUntilinCond.cppbut leavesWakingLock::lockalone.Tested
Windows 11, MinGW-w64 GCC 16.2, Release.
guest_raise_leaves_host_codegains five cases, each run for 10 rounds:scePthreadCondWait, with a normal mutex and with a recursive mutex held 3 times (the count is restored);scePthreadCondTimedwait;pthread_oncewhose initializer thread is stopped in its raise handler.In the relock cases the main thread holds the mutex while the woken waiter blocks, which is the same delivery point as a stopped holder.
In each case the handler must acknowledge within 2 s. Afterwards the wait must return 0 with the mutex owned, and the initializer must have run exactly once. Each relock round raises twice: the second raise is sent only after the first handler has returned, so it can only land in the relock. With
main'sCond.cppthe test aborts on the acknowledgement timeout (0xc0000409).guest_raise_leaves_host_codeandguest_mutex_raise_exceptionpass with--repeat until-fail:3. The 48 related raise, mutex, cond, once and libc++ condition tests that are built pass with--repeat until-fail:3.Cond.cppandOnce.cppwere syntax-checked with g++ 11 on Linux (WSL Ubuntu).debt_fold.py --checkpasses. Not tested in a title.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)pthread_onceinitializer)🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Documentation
Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-11-28T07:01:30Z.
Review: APPROVED
Checked head
555fff5dec. All on Windows 11 with WinLibs GCC 15.2 (Release), applied tomain4c7578e0a4together with #28006, #28002, #28001, #27991, #27987, #28007, #27994.tools/check_conventions.py --headandtools/debt_fold.py --checkpassed and the full build succeeded. In the full CPU suite (5,135 tests) the only failure iswindows_read_trap_unknown, which fails onmaintoo (fixed in #28028).guest_raise_leaves_host_codepasses. Windows is the platform the alertableParkAcquireandpthread_oncewaits are for.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T07:44:50Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this change; the points below are optional.
What I ran
Reviewed head
555fff5dec658050755d95afaac75bcbc7d54d03. No commands or tests were run; source was inspected through the pinned reader.Results
Findings
Blocking
None.
Non-blocking
GuestRaiseLeavesHostCode.cpp, the relock setup loops onrelock.waiting, which is set before the waiter enters the condition wait (:145); the fixed sleeps then assume the signal and mutex reacquisition have progressed to the intended blocking point (:172-177). Consider adding explicit synchronization or an observable wait-state check so this test cannot pass or fail based on scheduling.Not verified
Once.cpp,Cond.cpp,GuestRaiseLeavesHostCode.cpp, and the technical debt change note. No changed files were excluded in the manifest.files.jsonwas not available as a review-folder document.To get this to a good state
Nothing is required. The optional test synchronization improvement is noted above.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T14:05:29Z.
Review: COMMENTED
The changed
pthread_onceimplementation, condition wait implementation, and raise fixture passed MinGW syntax and type checking. I traced the blocking relock toTimedWait::ParkAcquireand the once wait toTimedWait::Condition::Wait. The local MinGW backend could not emit all three objects, so the five Windows runtime cases remain unverified here.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.