fix(libkernel): spin and yield by the mutex spin and yield loop counts #13544
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/13544"
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 cocoa55 on 2026-10-30T17:59:45Z.
What
scePthreadMutexLock,scePthreadMutexTimedlock,pthread_mutex_timedlockandpthread_mutex_reltimedlock_npnow use the counts thatscePthreadMutexSetspinloops/Setyieldloops(and thepthread_mutex_*_npcalls) store. Before this change the counts were stored and never read. As FreeBSD 11.4'smutex_lock_sleepdoes, a lock that isn't already held by the caller:spinloopstimes while its owner is clear, withpausebetween tries;yieldloopstimes, yielding before each try;Trylock doesn't spin, and neither does a recursive relock by the owner. On a uniprocessor host the spins are skipped and the yields kept, as in FreeBSD (
_thr_is_smp). The counts are nowstd::atomic<int>, because a setter can race with a lock on another thread. The default stays 2000 spins for adaptive mutexes, as measured on the console.The
libkernel.mddebt entry is cut to what's still open: cond-wait relock doesn't spin, priority-inherit and priority-protect mutexes spin, and negative counts spin 0 times. FreeBSD differs on all three, and none of them was measured on the console.This fixes behaviour only and makes no speed claim.
Tested
Linux (CachyOS, kernel 7.3, GCC, Release). New test
guest_pthread_mutex_spin:MutexOperations::SpinAcquirefor the exact number of tries, pauses and yields, early exit in each phase, and negative counts;All 56
ctest -R ^guest_pthreadtests pass, includingguest_pthread_mutex_errors,guest_pthread_timed_locks,guest_pthread_rwlockand the cond and cancel tests.tools/check_conventions.pypasses.tools/pr_overlap.pyfinds no line overlap; #12642 changes other lines in the same files. Not run on Windows or with 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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-30T20:16:47Z.
Review: APPROVED
Verified
32abcb15a0on Linux x86-64 (GCC 16.2.1, CachyOS kernel 6.2.10, Release,BUILD_TESTING=ON, cleanbuild-gpudirectory):guest_pthread_mutex_spinpasses, and 30/30 repeated runs pass (~0.04 s each), including theSpin(0, 4, 1)case from the review: a free mutex with a positive yield budget and no spins acquires with 0 yields.^guest_pthreadtests pass, run 4 times. Same suite onmain5a75b4a119passes 55/55 on the same machine and build directory, so the PR is +1 test and no regression.^guest_tests pass on the PR.3rdparty/opus,strenc.cand unrelated test sources, none inMutex.cpp,Mutex.hpporGuestPthreadMutexSpin.cpp.The code reads right too:
takeroutes throughspinAcquireonly when!tryOnly && !owned, soscePthreadMutexTrylockand a recursive relock by the owner keep the old path, andSpinAcquiretries once before the spin loop, matching_thr_umutex_trylock2beforemutex_lock_sleepin FreeBSD'smutex_lock_common. Making_spinloops/_yieldloopsstd::atomic<int>covers the set-after-init use from another thread that the spin loop now reads on the lock path.No requests from me.