fix(libkernel): spin and yield by the mutex spin and yield loop counts #13544

Merged
cocoa55 merged 2 commits from upstream-pr/13544 into main 2026-10-31 01:57:39 +00:00
cocoa55 commented 2026-10-30 17:59:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cocoa55 on 2026-10-30T17:59:45Z.


What

scePthreadMutexLock, scePthreadMutexTimedlock, pthread_mutex_timedlock and pthread_mutex_reltimedlock_np now use the counts that scePthreadMutexSetspinloops/Setyieldloops (and the pthread_mutex_*_np calls) store. Before this change the counts were stored and never read. As FreeBSD 11.4's mutex_lock_sleep does, a lock that isn't already held by the caller:

  1. tries the mutex up to spinloops times while its owner is clear, with pause between tries;
  2. then up to yieldloops times, yielding before each try;
  3. then blocks as before.

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 now std::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.md debt 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:

  • checks MutexOperations::SpinAcquire for the exact number of tries, pauses and yields, early exit in each phase, and negative counts;
  • runs 4 guest threads × 2000 contended lock/unlock rounds for all four mutex types with spin-only, yield-only, both, and negative counts, checking mutual exclusion and the total;
  • checks a recursive relock with spins set.

All 56 ctest -R ^guest_pthread tests pass, including guest_pthread_mutex_errors, guest_pthread_timed_locks, guest_pthread_rwlock and the cond and cancel tests. tools/check_conventions.py passes. tools/pr_overlap.py finds no line overlap; #12642 changes other lines in the same files. Not run on Windows or with a title.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5088823319 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13544). Originally posted by [cocoa55](https://github.com/cocoa55) on 2026-10-30T17:59:45Z. --- ### What `scePthreadMutexLock`, `scePthreadMutexTimedlock`, `pthread_mutex_timedlock` and `pthread_mutex_reltimedlock_np` now use the counts that `scePthreadMutexSetspinloops`/`Setyieldloops` (and the `pthread_mutex_*_np` calls) store. Before this change the counts were stored and never read. As FreeBSD 11.4's [`mutex_lock_sleep`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_mutex.c#L642-L708) does, a lock that isn't already held by the caller: 1. tries the mutex up to `spinloops` times while its owner is clear, with `pause` between tries; 2. then up to `yieldloops` times, yielding before each try; 3. then blocks as before. 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 now `std::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.md` debt 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`: - checks `MutexOperations::SpinAcquire` for the exact number of tries, pauses and yields, early exit in each phase, and negative counts; - runs 4 guest threads × 2000 contended lock/unlock rounds for all four mutex types with spin-only, yield-only, both, and negative counts, checking mutual exclusion and the total; - checks a recursive relock with spins set. All 56 `ctest -R ^guest_pthread` tests pass, including `guest_pthread_mutex_errors`, `guest_pthread_timed_locks`, `guest_pthread_rwlock` and the cond and cancel tests. `tools/check_conventions.py` passes. `tools/pr_overlap.py` finds no line overlap; #12642 changes other lines in the same files. Not run on Windows or with a title. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
blawesom commented 2026-10-30 20:16:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-30T20:16:47Z.

Review: APPROVED


Verified 32abcb15a0 on Linux x86-64 (GCC 16.2.1, CachyOS kernel 6.2.10, Release, BUILD_TESTING=ON, clean build-gpu directory):

  • guest_pthread_mutex_spin passes, and 30/30 repeated runs pass (~0.04 s each), including the Spin(0, 4, 1) case from the review: a free mutex with a positive yield budget and no spins acquires with 0 yields.
  • All 56 ^guest_pthread tests pass, run 4 times. Same suite on main 5a75b4a119 passes 55/55 on the same machine and build directory, so the PR is +1 test and no regression.
  • All 1030 ^guest_ tests pass on the PR.
  • No new compiler warnings: the 13 warnings in the build log are all in 3rdparty/opus, strenc.c and unrelated test sources, none in Mutex.cpp, Mutex.hpp or GuestPthreadMutexSpin.cpp.

The code reads right too: take routes through spinAcquire only when !tryOnly && !owned, so scePthreadMutexTrylock and a recursive relock by the owner keep the old path, and SpinAcquire tries once before the spin loop, matching _thr_umutex_trylock2 before mutex_lock_sleep in FreeBSD's mutex_lock_common. Making _spinloops/_yieldloops std::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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5718160218 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13544#pullrequestreview-5718160218). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-30T20:16:47Z. **Review:** APPROVED --- Verified `32abcb15a0` on Linux x86-64 (GCC 16.2.1, CachyOS kernel 6.2.10, Release, `BUILD_TESTING=ON`, clean `build-gpu` directory): - `guest_pthread_mutex_spin` passes, and 30/30 repeated runs pass (~0.04 s each), including the `Spin(0, 4, 1)` case from the review: a free mutex with a positive yield budget and no spins acquires with 0 yields. - All 56 `^guest_pthread` tests pass, run 4 times. Same suite on `main` `5a75b4a119` passes 55/55 on the same machine and build directory, so the PR is +1 test and no regression. - All 1030 `^guest_` tests pass on the PR. - No new compiler warnings: the 13 warnings in the build log are all in `3rdparty/opus`, `strenc.c` and unrelated test sources, none in `Mutex.cpp`, `Mutex.hpp` or `GuestPthreadMutexSpin.cpp`. The code reads right too: `take` routes through `spinAcquire` only when `!tryOnly && !owned`, so `scePthreadMutexTrylock` and a recursive relock by the owner keep the old path, and `SpinAcquire` tries once before the spin loop, matching `_thr_umutex_trylock2` before `mutex_lock_sleep` in FreeBSD's `mutex_lock_common`. Making `_spinloops`/`_yieldloops` `std::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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!13544
No description provided.