◐ Shell
clean mode source ↗

gh-135871: Fix needless spinning in _PyMutex_LockTimed (timeout==0) by jtibbertsma · Pull Request #135872 · python/cpython

…==0)

* Move the timeout == 0 guard outside the else so a non-blocking call
  returns immediately after a failed CAS instead of entering the spin loop.
* Reload v on every spin iteration, allowing timed/blocking callers to
  notice an unlock promptly.

No-GIL builds now honor the semantics of non-blocking attempts and avoid
wasted CPU; GIL builds are unaffected (MAX_SPIN_COUNT == 0).

ZeroIntensity

vstinner

colesbury

@jtibbertsma

vstinner

colesbury

@colesbury

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request

Jun 25, 2025
…ongh-135872)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
(cherry picked from commit cbfaf41)

Co-authored-by: Joseph Tibbertsma <josephtibbertsma@gmail.com>

colesbury pushed a commit to colesbury/cpython that referenced this pull request

Jun 25, 2025
pythongh-135872)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
(cherry picked from commit cbfaf41)

Co-authored-by: Joseph Tibbertsma <josephtibbertsma@gmail.com>

colesbury pushed a commit that referenced this pull request

Jun 25, 2025
…zero timeout (gh-135872) (gh-135946)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
(cherry picked from commit cbfaf41)

Co-authored-by: Joseph Tibbertsma <josephtibbertsma@gmail.com>

colesbury added a commit that referenced this pull request

Jun 25, 2025
…zero timeout (gh-135872) (gh-135947)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
(cherry picked from commit cbfaf41)

Co-authored-by: Joseph Tibbertsma <josephtibbertsma@gmail.com>

AndPuQing pushed a commit to AndPuQing/cpython that referenced this pull request

Jul 11, 2025
…ongh-135872)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.

Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request

Jul 12, 2025
…ongh-135872)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.

taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request

Aug 4, 2025
…ongh-135872)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.

Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request

Aug 19, 2025
…ongh-135872)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.