diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-12-03-12-00-00.gh-issue-162421.kQ7mZp.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-03-12-00-00.gh-issue-162421.kQ7mZp.rst new file mode 100644 index 00000000000000..857c124ab066d1 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-03-12-00-00.gh-issue-162421.kQ7mZp.rst @@ -0,0 +1,4 @@ +On the :term:`free-threaded build`, a thread waiting for a contended +:c:type:`PyMutex` now stops spinning and parks once the spin takes longer than +expected, for example because the machine has more runnable threads than CPUs. +This greatly reduces lock hand-off latency on oversubscribed machines. diff --git a/Python/lock.c b/Python/lock.c index 59dfe772a0365f..46915975df7538 100644 --- a/Python/lock.c +++ b/Python/lock.c @@ -31,6 +31,24 @@ static const int MAX_SPIN_COUNT = 40; static const int MAX_SPIN_COUNT = 0; #endif +#ifdef Py_GIL_DISABLED +// The spin assumes that _Py_yield() returns almost immediately. That holds on +// an idle machine, but on an oversubscribed one each yield can give the CPU +// to another runnable thread for a whole scheduler time slice. These limits +// only ever shorten the spin; they never change how the lock is acquired. +// +// - A spin that has lasted longer than MAX_SPIN_NS parks. +// - A single yield that lasted longer than SLOW_YIELD_NS means the CPU was +// given away. The thread parks at once and does not spin again for the +// next SPIN_DISABLE_NS. +static const PyTime_t MAX_SPIN_NS = 100*1000; +static const PyTime_t SLOW_YIELD_NS = 500*1000; +static const PyTime_t SPIN_DISABLE_NS = 10*1000*1000; + +// Monotonic time before which this thread parks without spinning. +static _Py_thread_local PyTime_t spin_disabled_until = 0; +#endif + struct mutex_entry { // The time after which the unlocking thread should hand off lock ownership // directly to the waiting thread. Written by the waiting thread. @@ -79,6 +97,15 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) }; Py_ssize_t spin_count = 0; +#ifdef Py_GIL_DISABLED + // Time the spin started, and time of the last clock read during the + // spin (0 if unknown, e.g. right after parking). + PyTime_t spin_start = now; + PyTime_t last_read = now; + if (now < spin_disabled_until) { + spin_count = MAX_SPIN_COUNT; + } +#endif for (;;) { if ((v & _Py_LOCKED) == 0) { // The lock is unlocked. Try to grab it. @@ -92,6 +119,24 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) // Spin for a bit. _Py_yield(); spin_count++; +#ifdef Py_GIL_DISABLED + // If the clock failed when the wait started (spin_start is 0), + // keep the full spin, as before these limits existed. + if (spin_start != 0) { + PyTime_t yielded; + // silently ignore error: a clock of 0 shortens nothing + (void)PyTime_MonotonicRaw(&yielded); + if (last_read != 0 && yielded - last_read > SLOW_YIELD_NS) { + spin_disabled_until = _PyTime_Add(yielded, + SPIN_DISABLE_NS); + spin_count = MAX_SPIN_COUNT; + } + else if (yielded - spin_start > MAX_SPIN_NS) { + spin_count = MAX_SPIN_COUNT; + } + last_read = yielded; + } +#endif continue; } @@ -122,6 +167,10 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) int ret = _PyParkingLot_Park(&m->_bits, &newv, sizeof(newv), timeout, &entry, (flags & _PY_LOCK_DETACH) != 0); +#ifdef Py_GIL_DISABLED + // Time spent parked is not a slow yield. + last_read = 0; +#endif if (ret == Py_PARK_OK) { if (entry.handed_off) { // We own the lock now. thread.Lock allows other threads