fix(libkernel): pause before retrying in MutexOperations::SpinAcquire #27922

Open
gurtgg wants to merge 1 commit from upstream-pr/27922 into main
gurtgg commented 2026-11-27 20:31:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gurtgg on 2026-11-27T20:31:52Z.


What

  • Fix MutexOperations::SpinAcquire (core/libs/prx/libkernel/Pthread/include/Mutex.hpp) so each iteration of the spin loop calls pause() before retrying if (tryAcquire()) return true;, matching the yield() loop directly below it.
  • Previously, the spin loop called tryAcquire() immediately after the initial tryAcquire() check and exited after the final pause() without retrying, causing a back-to-back retry with no pause and missing unlocks occurring during the last spin pause.
  • High-reach PRX kernel library (libkernel): MutexOperations::SpinAcquire is used by contended guest pthread mutex and condvar lock acquisitions.

Tested

  • guest_pthread_mutex_spin (CheckSpinAcquire in core/libs/tests/GuestPthreadMutexSpin.cpp):
    • Updated Spin(5, 3, 4, counts) expectation from pauses == 2 to pauses == 3 (tries == 4, yields == 0), matching pause-before-retry; fails on main (pauses == 2) and passes with this fix.
    • Negative control (spins = 1, yields = 0) where tryAcquire() returns true only after pause() runs once (acquired == true, paused == true, tries == 2, yields == 0), failing on main and passing with this fix.
    • Failure-path event trace (spins = 2, yields = 1) verifying the exact sequence "T,P,T,P,T,Y,T".
    • Non-positive bounds (spins = 0, yields = 0 and spins = -3, yields = -1) verifying a single tryAcquire() (tries == 1) with zero pauses or yields.

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 and upstream licence notices (tools/check_conventions.py passes)
  • 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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Negative-control regression test (plus valid/boundary cases) fails against unmodified main and passes after fix
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Mutex spin acquisition now pauses before each attempt, providing a consistent pause-and-retry sequence while waiting for a lock. This also adjusts the timing of callbacks during acquisition, including when a lock becomes available after a pause.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5532730707 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27922). Originally posted by [gurtgg](https://github.com/gurtgg) on 2026-11-27T20:31:52Z. --- ### What - Fix `MutexOperations::SpinAcquire` (`core/libs/prx/libkernel/Pthread/include/Mutex.hpp`) so each iteration of the spin loop calls `pause()` before retrying `if (tryAcquire()) return true;`, matching the `yield()` loop directly below it. - Previously, the spin loop called `tryAcquire()` immediately after the initial `tryAcquire()` check and exited after the final `pause()` without retrying, causing a back-to-back retry with no pause and missing unlocks occurring during the last spin pause. - High-reach PRX kernel library (`libkernel`): `MutexOperations::SpinAcquire` is used by contended guest pthread mutex and condvar lock acquisitions. ### Tested - `guest_pthread_mutex_spin` (`CheckSpinAcquire` in `core/libs/tests/GuestPthreadMutexSpin.cpp`): - Updated `Spin(5, 3, 4, counts)` expectation from `pauses == 2` to `pauses == 3` (`tries == 4, yields == 0`), matching pause-before-retry; fails on `main` (`pauses == 2`) and passes with this fix. - Negative control (`spins = 1, yields = 0`) where `tryAcquire()` returns `true` only after `pause()` runs once (`acquired == true`, `paused == true`, `tries == 2`, `yields == 0`), failing on `main` and passing with this fix. - Failure-path event trace (`spins = 2, yields = 1`) verifying the exact sequence `"T,P,T,P,T,Y,T"`. - Non-positive bounds (`spins = 0, yields = 0` and `spins = -3, yields = -1`) verifying a single `tryAcquire()` (`tries == 1`) with zero pauses or yields. ### Checklist - [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 and upstream licence notices (`tools/check_conventions.py` passes) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Negative-control regression test (plus valid/boundary cases) fails against unmodified `main` and passes after fix - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Mutex spin acquisition now pauses before each attempt, providing a consistent pause-and-retry sequence while waiting for a lock. This also adjusts the timing of callbacks during acquisition, including when a lock becomes available after a pause. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
PrivacyOnion commented 2026-11-27 20:37:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-27T20:37:39Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 613bee0af5:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_pthread_mutex_spin_tests built cleanly.
  • CTest (guest_pthread_mutex_spin):
    1/1 Test #6453: guest_pthread_mutex_spin .........   Passed    3.06 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/Pthread/include/Mutex.hpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #6453: guest_pthread_mutex_spin .........Subprocess aborted***Exception:   0.34 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified core/libs/prx/libkernel/Pthread/include/Mutex.hpp: SpinAcquire correctly executes pause() before tryAcquire() on each iteration of the spin loop, matching the yield loop semantics and preventing immediate retry with no pause.
    • Verified loop bounds and termination conditions: non-positive spins/yields (spins <= 0, yields <= 0) correctly bypass the loops after the initial tryAcquire() without underflow or unexpected behavior.
    • Verified template parameter safety and lambda invocations (TTry, TPause, TYield) for value/reference forwarding, side-effects, and exception neutrality.
    • Verified core/libs/tests/GuestPthreadMutexSpin.cpp test coverage, including updated pause counts, negative controls, zero/negative bounds, and explicit event trace sequencing ('T,P,T,P,T,Y,T').
    • Verified strict AGENTS.md conventions: no illegal comments added, no technical debt discrepancies, no stubbing violations, and PR description template compliance.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6093135904 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27922#pullrequestreview-6093135904). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-27T20:37:39Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `613bee0af5`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_pthread_mutex_spin_tests` built cleanly. - **CTest** (`guest_pthread_mutex_spin`): ``` 1/1 Test #6453: guest_pthread_mutex_spin ......... Passed 3.06 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/Pthread/include/Mutex.hpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #6453: guest_pthread_mutex_spin .........Subprocess aborted***Exception: 0.34 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified core/libs/prx/libkernel/Pthread/include/Mutex.hpp: SpinAcquire correctly executes pause() before tryAcquire() on each iteration of the spin loop, matching the yield loop semantics and preventing immediate retry with no pause. - Verified loop bounds and termination conditions: non-positive spins/yields (spins <= 0, yields <= 0) correctly bypass the loops after the initial tryAcquire() without underflow or unexpected behavior. - Verified template parameter safety and lambda invocations (TTry, TPause, TYield) for value/reference forwarding, side-effects, and exception neutrality. - Verified core/libs/tests/GuestPthreadMutexSpin.cpp test coverage, including updated pause counts, negative controls, zero/negative bounds, and explicit event trace sequencing ('T,P,T,P,T,Y,T'). - Verified strict AGENTS.md conventions: no illegal comments added, no technical debt discrepancies, no stubbing violations, and PR description template compliance. Looks Good To Me
MarcZX10 commented 2026-11-27 21:08:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-27T21:08:19Z.

Review: APPROVED


Reviewed — looks good

The code change moves each spin pause before its retry, and the updated regression cases cover the changed ordering and bounds at head 613bee0af5615d2c7feea4744c1775e389e80fd6.

What I ran

Read the prepared diff and changed source at the pinned head. No commands or tests were run; the test outcomes in the description are author-reported.

Results

Check Result Note
Changed-line review passed Both changed files read
Runtime tests not run Description reports test results

Findings

Blocking

None.

Non-blocking

None.

Not verified

No test suite, build, or console behavior was run. No files were excluded by manifest.json; both reviewed files were read: core/libs/prx/libkernel/Pthread/include/Mutex.hpp and core/libs/tests/GuestPthreadMutexSpin.cpp. Mutex.cpp and Cond.cpp were read for caller context.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6093367742 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27922#pullrequestreview-6093367742). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-27T21:08:19Z. **Review:** APPROVED --- # Reviewed — looks good The code change moves each spin pause before its retry, and the updated regression cases cover the changed ordering and bounds at head `613bee0af5615d2c7feea4744c1775e389e80fd6`. ## What I ran Read the prepared diff and changed source at the pinned head. No commands or tests were run; the test outcomes in the description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Changed-line review | passed | Both changed files read | | Runtime tests | not run | Description reports test results | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No test suite, build, or console behavior was run. No files were excluded by `manifest.json`; both reviewed files were read: `core/libs/prx/libkernel/Pthread/include/Mutex.hpp` and `core/libs/tests/GuestPthreadMutexSpin.cpp`. `Mutex.cpp` and `Cond.cpp` were read for caller context. ## To get this to a good state Nothing is required.
Plyxen commented 2026-11-28 05:06:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-11-28T05:06:13Z.

Review: APPROVED


Checked head 613bee0af5. All on Windows 11 with WinLibs GCC 15.2 (Release), applied to main a163d4b886 together with #27930, #27929, #27926, #27925, #27924, #27923, #27901, #27828, #27771, #27755, #27748, #27682, #27765. tools/check_conventions.py --head and tools/debt_fold.py --check passed and the full build succeeded. guest_pthread_mutex_spin passes. guest_pthread_mutex_contention timed out in the loaded suite, as it does on main. Run on its own, it takes the same time with and without this change. In the full CPU suite (5,120 tests) the only failures are unrelated:

  • windows_read_trap_unknown fails on main too (fixed in #28028).
  • frame_capture_minimize, jpeg_external_toolchain and guest_pthread_mutex_contention are timeouts under load; they pass when run on their own.
  • guest_audio_in_capture passes 20/20 on main and with this change.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6095938800 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27922#pullrequestreview-6095938800). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-11-28T05:06:13Z. **Review:** APPROVED --- Checked head `613bee0af5`. All on Windows 11 with WinLibs GCC 15.2 (Release), applied to `main` `a163d4b886` together with #27930, #27929, #27926, #27925, #27924, #27923, #27901, #27828, #27771, #27755, #27748, #27682, #27765. `tools/check_conventions.py --head` and `tools/debt_fold.py --check` passed and the full build succeeded. `guest_pthread_mutex_spin` passes. `guest_pthread_mutex_contention` timed out in the loaded suite, as it does on `main`. Run on its own, it takes the same time with and without this change. In the full CPU suite (5,120 tests) the only failures are unrelated: - `windows_read_trap_unknown` fails on `main` too (fixed in #28028). - `frame_capture_minimize`, `jpeg_external_toolchain` and `guest_pthread_mutex_contention` are timeouts under load; they pass when run on their own. - `guest_audio_in_capture` passes 20/20 on `main` and with this change. AI-assisted: yes (Claude Code)
ligmaSec commented 2026-12-03 08:34:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T08:34:52Z.

Review: COMMENTED


The Linux guest_pthread_mutex_spin CTest passed with the patch. Its instrumented retry loop checks that pauses and yields occur before the next retry, including when a phase has no retries, and checks eventual acquisition.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6166197761 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/27922#pullrequestreview-6166197761). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T08:34:52Z. **Review:** COMMENTED --- The Linux guest_pthread_mutex_spin CTest passed with the patch. Its instrumented retry loop checks that pauses and yields occur before the next retry, including when a phase has no retries, and checks eventual acquisition. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/27922:upstream-pr/27922'
git switch 'upstream-pr/27922'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/27922'
git switch 'upstream-pr/27922'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/27922'
git switch 'upstream-pr/27922'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/27922'
git switch 'main'
git merge --squash 'upstream-pr/27922'
git switch 'main'
git merge --ff-only 'upstream-pr/27922'
git switch 'main'
git merge 'upstream-pr/27922'
git push origin 'main'
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!27922
No description provided.