fix(libkernel): treat a null deadline as an untimed lock in the timed pthread locks #7035
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7035"
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 milescoviello on 2026-10-17T06:46:33Z.
What
pthread_rwlock_timedrdlock,pthread_rwlock_timedwrlockandpthread_mutex_timedlockthrewstd::runtime_error(null abstime) for a null deadline. In FreeBSD 11's libthr, a nullabstimereaches the untimed kernel lock:rwlock_rdlock_commonandrwlock_wrlock_commoncheck the deadline only when it isn't null and pass it to__thr_rwlock_rdlock/__thr_rwlock_wrlock, where null means no timeout.mutex_lock_sleepcalls__thr_umutex_lockwhenabstime == NULL, andmutex_self_lockreturnsEDEADLKfor an error-checking mutex the caller already holds, aspthread_mutex_lockdoes.The three calls now do what
pthread_rwlock_rdlock,pthread_rwlock_wrlockandpthread_mutex_lockdo whenabstimeis null.I don't know of a title that hits this. I found it while comparing the pthread wrappers with libthr.
Tested
Ubuntu 24.04, GCC 13.3, Release, on
main6a32550fd1.guest_pthread_timed_locks(new): with a null deadline, both rwlock calls take a free lock, and each waits for a writer that holds the lock for 50 ms, then takes it.pthread_mutex_timedlocktakes a free mutex, returnsEDEADLKwhen the caller already holds it, and waits for another holder the same way. Withmain'sRwlock.cpp, and separately withmain'sMutex.cpp, the test aborts at the first null deadline in that file (the throw). With this change it passes 3 of 3.guest_pthread_rwlock: its check that a null deadline throws now expects the lock to be taken and released. Withmain'sRwlock.cppthat check fails.ctest -R '^guest_|libc': 403 of 403 pass, with this and one other small libkernel fix applied together.c7849f8495, after a review: the waiter now requires a flag that the holder sets just before it unlocks, in place of the old check that it waited at least half the hold time. That check aborted in 15 of 20 runs on a heavily loaded Windows host, because the waiter saw the holder's lock 40–90 ms late (run by @screamm). Here, under 24 CPU burners, the new test passes 20 of 20; the old check also passed 20 of 20, so I could not reproduce the abort on Linux. Both lock tests pass 3 of 3 with ctest.676ee71c3: both tests pass 3 of 3, and with this PR's source reverted both thrownull abstime(series run by @screamm).tools/check_conventions.pypasses.tools/pr_overlap.pyfinds no conflicting lines, and the new test is registered away from where #6711 adds one (git merge-treemerges this branch with #6711, #4563, #6893 and #6244 cleanly). It couldn't compare against seven PRs that conflict withmainand touchCMakeLists.txt(#6044 also movesGuestPthreadRwlock.cpp).@screamm rechecked c7849f849 on Windows:
guest_pthread_timed_lockspasses 20 of 20 direct runs on a loaded host (where it had aborted 15 of 20) and 20 of 20 next to an 8-job build; both tests abort withmain'sMutex.cppandRwlock.cpp.Not checked: macOS, a console.
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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T06:56:48Z.
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)core/libs/prx/libkernel/CMakeLists.txtL671Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/tests/GuestPthreadRwlock.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-17T10:05:27Z.
Windows run in https://github.com/boykopovar/AnyPS5/pull/7023#issuecomment-6232617712: both tests pass 3/3, and with the source reverted both throw "null abstime". guest_pthread_timed_locks aborted in 15 of 20 runs on a heavily loaded host and passed 20 of 20 on a quieter one. Every abort was the elapsed >= HoldTime / 2 check, after the main thread saw held 40-90 ms late; the lock was always taken after the holder unlocked. A flag the holder sets just before unlocking, checked when acquire() returns, would make the test deterministic.
Written with Claude Code assistance (validation by a subagent, reviewed in this session).
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T10:09:52Z.
Thanks @screamm, that diagnosis was right. The lock was taken only after the holder unlocked, so the half-hold-time check was measuring scheduler delay.
c7849f8495does what you suggested: the holder setsreleasingjust before it unlocks, and the waiter requires that flag onceacquire()returns. Here, under 24 CPU burners, the new test passes 20 of 20, and both lock tests pass 3 of 3 with ctest. (The old check also passed 20 of 20 under that load on Linux, so I couldn't reproduce the abort here.) I added your Windows run to the description.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-17T11:33:33Z.
Rechecked on head
c7849f849(scratch-merged onto main673f7406c, merge is clean). Windows 11 Pro 26220, Ryzen 7 5700X, WinLibs GCC 15.2. Targeted build only (libs,guest_pthread_timed_locks_tests,guest_pthread_rwlock_tests), not a full build.guest_pthread_timed_locks: 20 of 20 direct runs pass on a loaded host (other builds were running; this is the condition under which it aborted 15 of 20 before), and 20 of 20 again while I ran my own 8-job build next to them. Each run takes 0.5 to 3 s including process start. ctest--repeat until-fail:3: 3 of 3.guest_pthread_rwlock: 3 of 3 with ctest--repeat until-fail:3and 3 of 3 direct.Mutex.cppandRwlock.cppreverted to main (tests kept), both tests abort withwhat(): pthread_rwlock_timedrdlock: null abstime; with the files restored both pass.tools/check_conventions.py --base origin/mainpasses andgit diff --checkis clean.The
releasingflag is set before the unlock and the lock can only be taken after it, so the check no longer depends on when the main thread is scheduled. That resolves the flake I reported.Not checked: full build, the non-GPU suite, Linux, title runs.
Written with Claude Code assistance (validation by a subagent, reviewed in this session).