From 7296a7d133c078f1b9d6294f64dc653c9b03eb56 Mon Sep 17 00:00:00 2001 From: Joseph Tibbertsma Date: Mon, 23 Jun 2025 18:08:43 -0700 Subject: [PATCH 1/5] gh-135871: Fix needless spinning in _PyMutex_LockTimed (timeout==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). --- .../2025-06-23-18-08-32.gh-issue-135871.50C528.rst | 1 + Python/lock.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst b/Misc/NEWS.d/next/Core and Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst new file mode 100644 index 00000000000000..669280f27382dc --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst @@ -0,0 +1 @@ +gh-135871: _PyMutex_LockTimed() no longer spins or fails for timeout==0 in no-GIL builds; spin loop now reloads lock state. diff --git a/Python/lock.c b/Python/lock.c index b125ad0c9e356d..a914f069e833c8 100644 --- a/Python/lock.c +++ b/Python/lock.c @@ -58,7 +58,7 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) return PY_LOCK_ACQUIRED; } } - else if (timeout == 0) { + if (timeout == 0) { return PY_LOCK_FAILURE; } @@ -89,6 +89,7 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) // Spin for a bit. _Py_yield(); spin_count++; + v = _Py_atomic_load_uint8_relaxed(&m->_bits); continue; } From 4b5123c6cf8c00dcc9b528f26673d66d4020266c Mon Sep 17 00:00:00 2001 From: Joseph Tibbertsma Date: Mon, 23 Jun 2025 18:54:05 -0700 Subject: [PATCH 2/5] Update news entry --- .../2025-06-23-18-08-32.gh-issue-135871.50C528.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst b/Misc/NEWS.d/next/Core and Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst index 669280f27382dc..a754092b6e920f 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst @@ -1 +1 @@ -gh-135871: _PyMutex_LockTimed() no longer spins or fails for timeout==0 in no-GIL builds; spin loop now reloads lock state. +Improve internal lock performance on the free threaded build. From 8a4e9adefc25a82138f785784736d9aef5362862 Mon Sep 17 00:00:00 2001 From: Joseph Tibbertsma Date: Mon, 23 Jun 2025 18:57:01 -0700 Subject: [PATCH 3/5] Fix filename --- .../2025-06-23-18-08-32.gh-issue-135871.50C528.rst | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/{Core and Builtins => Core_and_Builtins}/2025-06-23-18-08-32.gh-issue-135871.50C528.rst (100%) diff --git a/Misc/NEWS.d/next/Core and Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst similarity index 100% rename from Misc/NEWS.d/next/Core and Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst rename to Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst From 3fb8f375655457199374e1ff4fd4865f1ff7aa86 Mon Sep 17 00:00:00 2001 From: Joseph Tibbertsma Date: Tue, 24 Jun 2025 17:29:32 -0700 Subject: [PATCH 4/5] Keep timeout==0 fast-fail, drop spin-loop state reload --- .../2025-06-23-18-08-32.gh-issue-135871.50C528.rst | 3 ++- Python/lock.c | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst index a754092b6e920f..c4239d3be0ef57 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst @@ -1 +1,2 @@ -Improve internal lock performance on the free threaded build. +Free-threaded (no-GIL) builds: a non-blocking mutex lock attempt now returns +immediately when the lock is busy instead of briefly spinning. diff --git a/Python/lock.c b/Python/lock.c index a914f069e833c8..ea6ac00bfeccb4 100644 --- a/Python/lock.c +++ b/Python/lock.c @@ -89,7 +89,6 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) // Spin for a bit. _Py_yield(); spin_count++; - v = _Py_atomic_load_uint8_relaxed(&m->_bits); continue; } From 8cbb99249b81cb28d33777a8dd011a75e10f7fc5 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Wed, 25 Jun 2025 12:14:11 -0400 Subject: [PATCH 5/5] Update Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst --- .../2025-06-23-18-08-32.gh-issue-135871.50C528.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst index c4239d3be0ef57..ce29ddecefe17f 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst @@ -1,2 +1,2 @@ -Free-threaded (no-GIL) builds: a non-blocking mutex lock attempt now returns -immediately when the lock is busy instead of briefly spinning. +Non-blocking mutex lock attempts now return immediately when the lock is busy +instead of briefly spinning in the :term:`free threading` build.