fix(libkernel): spin and yield when a condition variable wait relocks its mutex #13725

Merged
cocoa55 merged 2 commits from upstream-pr/13725 into main 2026-10-31 21:20:44 +00:00
cocoa55 commented 2026-10-31 02:49:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cocoa55 on 2026-10-31T02:49:05Z.


What

A woken scePthreadCondWait/scePthreadCondTimedwait (and the pthread_cond_* calls that use them) now takes its mutex back through the same path as scePthreadMutexLock: it tries once, spins and yields by the mutex's spin and yield counts, then blocks. Before this change, WakingLock::lock() in Cond.cpp called the native lock directly. FreeBSD 11.4 re-locks through _mutex_cv_lock, which calls mutex_lock_common, so a woken waiter spins and yields like any other lock. This is a follow-up to #13544.

MutexOperations::SpinAcquire(PthreadMutex, std::timed_mutex&) and its std::recursive_timed_mutex& overload expose Mutex.cpp's existing spin path. The re-lock of a recursive mutex's outer level spins; the inner levels the owner takes back don't. The condition variable clause is removed from the libkernel.md debt entry.

This fixes behaviour only and makes no speed claim.

Tested

Linux (CachyOS, kernel 7.3, GCC, Release, i5-9400F). guest_pthread_mutex_spin adds a token ring: 4 guest threads take 300 turns each through one condition variable, broadcasting after every turn, so every wake re-locks the mutex under contention. It runs for all four mutex types with 2000 spins and 4 yields, and with neither; with scePthreadCondWait and scePthreadCondTimedwait; and at recursive depth 2 for the recursive type. It checks that no two threads are ever inside together and that the turn and total counts are exact.

These pass: all 56 ^guest_pthread tests, guest_cond_timedwait, guest_cond_recursive_mutex, guest_cond_unowned_mutex, both guest_libc_cnd_* tests and all 29 guest_job_manager_* tests. tools/check_conventions.py passes. tools/pr_overlap.py finds no shared lines: #9326 and #12642 change other functions in Mutex.cpp. 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:5093793616 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13725). Originally posted by [cocoa55](https://github.com/cocoa55) on 2026-10-31T02:49:05Z. --- ### What A woken `scePthreadCondWait`/`scePthreadCondTimedwait` (and the `pthread_cond_*` calls that use them) now takes its mutex back through the same path as `scePthreadMutexLock`: it tries once, spins and yields by the mutex's spin and yield counts, then blocks. Before this change, `WakingLock::lock()` in `Cond.cpp` called the native lock directly. FreeBSD 11.4 re-locks through [`_mutex_cv_lock`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_mutex.c#L782-L791), which calls `mutex_lock_common`, so a woken waiter spins and yields like any other lock. This is a follow-up to #13544. `MutexOperations::SpinAcquire(PthreadMutex, std::timed_mutex&)` and its `std::recursive_timed_mutex&` overload expose `Mutex.cpp`'s existing spin path. The re-lock of a recursive mutex's outer level spins; the inner levels the owner takes back don't. The condition variable clause is removed from the `libkernel.md` debt entry. This fixes behaviour only and makes no speed claim. ### Tested Linux (CachyOS, kernel 7.3, GCC, Release, i5-9400F). `guest_pthread_mutex_spin` adds a token ring: 4 guest threads take 300 turns each through one condition variable, broadcasting after every turn, so every wake re-locks the mutex under contention. It runs for all four mutex types with 2000 spins and 4 yields, and with neither; with `scePthreadCondWait` and `scePthreadCondTimedwait`; and at recursive depth 2 for the recursive type. It checks that no two threads are ever inside together and that the turn and total counts are exact. These pass: all 56 `^guest_pthread` tests, `guest_cond_timedwait`, `guest_cond_recursive_mutex`, `guest_cond_unowned_mutex`, both `guest_libc_cnd_*` tests and all 29 `guest_job_manager_*` tests. `tools/check_conventions.py` passes. `tools/pr_overlap.py` finds no shared lines: #9326 and #12642 change other functions in `Mutex.cpp`. 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)
MarcZX10 commented 2026-10-31 03:03:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T03:03:31Z.

Review: COMMENTED


Re-checked — new head, test evidence needed

I reviewed head 16375634e31e44e35ff62a7ab513811d5249b292 and found no source-level blocker, but the new condition-wait test has not been verified by the hosted run.

The Windows job passed its build, but its log says No guest core/libs test sources changed; the changed-test runner therefore did not execute guest_pthread_mutex_spin. Linux and specialized jobs were skipped. The PR description reports Linux test results but does not identify the tested commit SHA, so I cannot tie those results to this head.

Please provide the exact tested SHA and output for guest_pthread_mutex_spin and the focused condition/mutex tests, or arrange for CI to select and run the changed test. I will recheck the same evidence on the updated head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5720748857 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13725#pullrequestreview-5720748857). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T03:03:31Z. **Review:** COMMENTED --- # Re-checked — new head, test evidence needed I reviewed head `16375634e31e44e35ff62a7ab513811d5249b292` and found no source-level blocker, but the new condition-wait test has not been verified by the hosted run. The Windows job passed its build, but its log says `No guest core/libs test sources changed`; the changed-test runner therefore did not execute `guest_pthread_mutex_spin`. Linux and specialized jobs were skipped. The PR description reports Linux test results but does not identify the tested commit SHA, so I cannot tie those results to this head. Please provide the exact tested SHA and output for `guest_pthread_mutex_spin` and the focused condition/mutex tests, or arrange for CI to select and run the changed test. I will recheck the same evidence on the updated head.
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!13725
No description provided.