feat(libkernel): implement the pthread_rwlock try and timed lock functions #344
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/344"
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 DotDebian on 2026-10-02T10:17:41Z.
What
The POSIX layer of libkernel exported only
pthread_rwlock_init,destroy,rdlock,wrlockandunlock. This PR adds the four missing lock functions incore/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp, on top of thescePthreadRwlock*functions that already exist:pthread_rwlock_tryrdlockandpthread_rwlock_trywrlockreturn0orEBUSY.pthread_rwlock_timedrdlockandpthread_rwlock_timedwrlocktake an absoluteCLOCK_REALTIMEdeadline and return0,ETIMEDOUT, orEINVALwhentv_nsecis outside[0, 1000000000).Behaviour follows POSIX.1-2017 and FreeBSD libthr (
thr_rwlock.c): the timed variants try the lock first and only validateabstimewhen the lock is not free, so an invalidabstimeon a free lock still returns0.Three choices to review:
abstimethrows. FreeBSD treats it as an unbounded wait. I followedpthread_mutex_timedlockin this repository, which throws. I can make it wait forever instead if you prefer.EDEADLKfor the owning writer. A timed lock called by the thread that already holds the write lock returnsEDEADLK, becausescePthreadRwlockTimedrdlockandscePthreadRwlockTimedwrlockalready do. POSIX allows it.PosixThread::RelativeMicroseconds, so a deadline more thanKernelUsecondsmax (about 71 minutes) away is clamped, as it is forsem_timedwait.Two changes in the sce layer:
Pthread/include/Rwlock.hppdeclaresscePthreadRwlockTimedrdlockandscePthreadRwlockTimedwrlock, which were defined but not declared.scePthreadRwlockTryrdlockandscePthreadRwlockTrywrlockreturnEBUSYwhen the caller already holds the write lock. They used to calltry_lock_shared/try_lockon astd::shared_timed_mutexthe thread already owns, which is undefined behaviour. This is a guard, not an observed bug: on my toolchain the old code already returnedEBUSY.Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release build with
-DBUILD_TESTING=ON. No title or homebrew was run. Linux was not built or run locally; the change has no platform-specific code and the new test is registered on both platforms, so CI is the first Linux run of it.New ctest
guest_pthread_rwlock(core/libs/tests/GuestPthreadRwlock.cpp):0; the timed ones are giventv_nsec = 1000000000and still return0because the lock is free;EBUSYfrom bothtryfunctions andEDEADLKfrom bothtimedfunctions;tryrdlockreturns0,trywrlockreturnsEBUSY,timedwrlockwith a deadline 20 ms away returnsETIMEDOUTafter at least 15 ms, a past deadline returnsETIMEDOUT,tv_nsec = 1000000000andtv_nsec = -1returnEINVAL;tryfunctions returnEBUSY, and bothtimedfunctions go through the same timeout andEINVALchecks;timedwrlockandtimedrdlockwith a deadline 5 s away return0once the holder unlocks, 20 ms later;abstimethrowsstd::runtime_error.The test passed 20 consecutive runs. It also passes without the
EBUSYguard on this toolchain, so it does not prove the guard. Full suite: 108/108 tests pass.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 repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-04T05:26:19Z.
Progress report (
2cf901b→f9625f2)📈 System libraries: 77.38% (+0.03%, +4 implemented functions)
✅ 4 implemented
pthread_rwlock_timedrdlock_nid_postfixpthread_rwlock_timedwrlock_nid_postfixpthread_rwlock_tryrdlock_nid_postfixpthread_rwlock_trywrlock_nid_postfixImported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:42:01Z.
Thanks, this reads right to me against FreeBSD libthr (
thr_rwlock.c: try first, then validatetv_nsec), and theOwnsWriteguards remove real undefined behaviour. It now conflicts with main incore/libs/prx/libkernel/CMakeLists.txt; could you rebase it? Then it looks ready from my side.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:37:54Z.
Thanks for the rebase; the code is unchanged apart from the CMake context, and CI is green now.
guest_pthread_rwlockpasses here on current main. Ready to merge from my side. #367 adds its test right after yours inlibkernel/CMakeLists.txt, so whichever lands second needs a one-line rebase there.