Skip to content

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

Open
gurtgg wants to merge 1 commit into
boykopovar:mainfrom
gurtgg:fix-libkernel-mutex-spin-acquire-pause-before-retry
Open

gurtgg wants to merge 1 commit into
boykopovar:mainfrom
gurtgg:fix-libkernel-mutex-spin-acquire-pause-before-retry

Conversation

@gurtgg

@gurtgg gurtgg commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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.

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Mutex Spin Acquisition

Layer / File(s) Summary
Spin acquisition order and regression coverage
core/libs/prx/libkernel/Pthread/include/Mutex.hpp, core/libs/tests/GuestPthreadMutexSpin.cpp
Each spin iteration calls pause() before tryAcquire(). Tests cover pause counts, zero spin and yield settings, acquisition after a pause callback, and the T,P,T,P,T,Y,T callback sequence.

Suggested labels: libkernel

Suggested reviewers: cocoa55, privacyonion

Change: Bug fix

Merge Risk: Minimal · up to 613be

The change makes contended mutex spinning pause before each retry, as intended. The tests cover the new ordering. No merge-blocking risk was found.

Security architecture risk: Minimal · up to 613be

The fix pauses before retrying a contended lock. An uncontended lock still gets an immediate attempt, and the rules for acquiring the lock or falling back are unchanged.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7df3332f-4268-410b-b3d1-a3be370150fa
Commits

Reviewing files that changed from the base of the PR and between fdf873a and 613bee0.

Files selected for processing: 2

  • core/libs/prx/libkernel/Pthread/include/Mutex.hpp
  • core/libs/tests/GuestPthreadMutexSpin.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@JJenkx

JJenkx commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Windows at this head (613bee0af561): guest_pthread_mutex_spin passes, 3/3 runs. This is the CTest of the executable that compiles the changed GuestPthreadMutexSpin.cpp.

Setup:

  • Windows 11 22621, Ryzen 9 5950X, MinGW-w64 GCC 15.2 UCRT r7, Release.
  • The exact PR head was built (not merged into main).
  • 3 runs through the shared FIFO.

The added test lines have no platform guard.

AI-assisted: yes.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Plyxen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants