feat(libkernel): add mutex ownership, relative lock and spin/yield accessors #8400
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8400"
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 slider on 2026-10-20T12:23:07Z.
What
Adds 10 missing libkernel mutex extensions, following FreeBSD 11.4 libthr (
thr_mutex.c):pthread_mutex_isowned_np: 1 when the calling thread owns the mutex, otherwise 0. A static initializer or destroyed mutex reports 0 without being initialized, as_pthread_mutex_isowned_npdoes.pthread_mutex_reltimedlock_np: the POSIX name of the existing relativescePthreadMutexTimedlock, with the same static-initializer handling as the other POSIX lock calls.pthread_mutex_getspinloops_np,pthread_mutex_setspinloops_np,pthread_mutex_getyieldloops_npandpthread_mutex_setyieldloops_np, plusscePthreadMutexGetspinloops,Setspinloops,GetyieldloopsandSetyieldloops. They followcheck_and_init_mutex: a static initializer is initialized and a destroyed mutex returnsEINVAL. The exception is setting the yield count, which returns 0 as FreeBSD's__pthread_mutex_setyieldloops_npdoes.mutex_init_bodydefaults: 2000 spins for an adaptive mutex and 0 otherwise.PthreadMutexPrivate::SetTypesets them at the three places a mutex is created.docs/dev/debt/libkernel.md): the counts are stored and reported but host mutexes do not spin or yield by them, and the console's defaults are unverified.Left out on purpose:
pthread_mutex_getprioceilingandsetprioceiling(and their SCE names): mutexes here do not carry the priority protocol, and FreeBSD's setter locks the mutex throughUMTX_OP_SET_CEILING.scePthreadMutexIsowned: shadPS4 wraps it with the error-convertingORBIS()wrapper, which would turn "owned" into0x80020001. Its console behaviour is undocumented.No overlap with boykopovar/AnyPS5#8373: the branches merge cleanly (
git merge-tree).Tested
Linux (Ubuntu 24.04, GCC 13.3, Release):
guest_pthread_mutex_errorspass: ownership from the owner and another thread,reltimedlock_npdeadlock and timeout, destroyed and static-initializer mutexes, and spin/yield defaults per type, round trips and errors.guest_pthread_mutexattr,guest_cond_timedwait,guest_cond_recursive_mutex,guest_pthread_destroyed_objectsandguest_kernel_errorspass.tools/check_conventions.py --base origin/mainpasses.Windows: the changed sources and test pass a syntax-only check with MinGW-w64 GCC 13 (not the supported 15.2); not built or run.
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)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T12:23:47Z.
Progress report (
f894ba6→31fc06b)📈 System libraries: 50.64% (+0.06%, +10 implemented functions)
✅ 10 implemented
pthread_mutex_getspinloops_np_nid_postfixpthread_mutex_getyieldloops_np_nid_postfixpthread_mutex_isowned_np_nid_postfixpthread_mutex_reltimedlock_np_nid_postfixpthread_mutex_setspinloops_np_nid_postfixpthread_mutex_setyieldloops_np_nid_postfixscePthreadMutexGetspinloopsscePthreadMutexGetyieldloopsscePthreadMutexSetspinloopsscePthreadMutexSetyieldloopsImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T12:32:45Z.
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/Pthread/src/Mutex.cppL155-156Some 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.
docs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Pthread/include/Pthread.hppdocs/dev/debt/libkernel.mdcore/libs/tests/GuestPthreadMutexErrors.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Pthread/include/Pthread.hppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Pthread/include/Pthread.hppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Pthread/include/Pthread.hppdocs/dev/debt/libkernel.mdImported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T20:41:03Z.
core/libs/prx/libkernel/Pthread/Posix/Mutex.cppL82:pthread_mutex_reltimedlock_nptakes(PthreadMutex*, KernelUseconds), where the count is 32 bits (SceTypes.hppL38). The description cites no source for this signature, and the debt file does not record it. FreeBSD 11.4 has no such function: it is not inthr_mutex.c,pthread_np.horpthread.map. The only implementation I found that gives a signature is shadPS4. It reads a 64-bit count and registers that one function for this NID (nbQ0bXMRlhk) and forscePthreadMutexTimedlock. If the console also reads 64 bits, takepthread_mutex_reltimedlock_np(&m, 0x100000000)(about 71.6 minutes) on a mutex another thread holds. Here only the low 32 bits are read, which are 0, so the call returnsETIMEDOUTat once instead of waiting.CONTRIBUTING.mdL73 asks for signatures to be recorded in technical debt. Thepthread_cond_reltimedwait_npentry already does this for the same case. A matching line next to it would cover this one:Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T23:00:00Z.
@slider The code looks right to me: the spin defaults reach every creation path (the no-attr
pthread_mutex_initcase ends up non-adaptive, so 0 spins is correct), the ownership check handles static and destroyed mutexes without initialising them, and everything compiles with the Windows toolchain too.One thing before it can go in, the same point nikowolf123 made:
pthread_mutex_reltimedlock_nptakes(mutex, KernelUseconds), but no source gives that signature (FreeBSD 11.4 has no such function, and shadPS4 reads a 64-bit count). Please record it indocs/dev/debt/libkernel.mdnext to thepthread_cond_reltimedwait_npentry; nikowolf123's suggested line works. If you have a source showing the console reads 64 bits, switch the parameter tostd::uint64_tinstead.After that, the bar is a full CI run with
guest_pthread_mutex_errorspassing on Linux and Windows. Note that #8087 also editsPthread/src/Mutex.cpp, so whichever lands second needs a rebase.Imported from boykopovar/AnyPS5. Originally posted by slider on 2026-10-21T13:17:35Z.
Thanks @nikowolf123 and @oneandonlydean. Added the debt entry next to
pthread_cond_reltimedwait_np(separate commit, 5993f57… after rebase:docs(debt): record the pthread_mutex_reltimedlock_np timeout width). I found no source that the console reads 64 bits: FreeBSD 11.4 has no such function, the ps5-payload-dev SDK headers don't declare it, and only shadPS4 (u64 usec, mutex.cpp L284-L287 at 7c960ea) gives a signature. So the parameter stays the 32-bitSceKernelUsecondsof thescePthreadMutexTimedlocktwin, as forpthread_cond_reltimedwait_np.Also rebased onto current main;
guest_pthread_mutex_errorsand the related mutex/cond tests pass on Linux, and the changed files pass a MinGW syntax check. Could a collaborator start the full CI run (Linux and Windows)? If #8087 lands first I'll rebase again.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T13:34:39Z.
Checked the FreeBSD 11.4
thr_mutex.cdefaults and the static/destroyed-mutex handling, that the newguest_pthread_mutex_errorscases pin values only this change produces, and that thereltimedlock_nptimeout width is now recorded indocs/dev/debt/libkernel.mdas requested; merged.