Skip to content

Commit 87a7a38

Browse files
White-Reaper0claude
andcommitted
gh-162421: Stop the PyMutex spin when yields are slow
On the free-threaded build, a contended _PyMutex_LockTimed() calls _Py_yield() up to 40 times before parking, without reloading the lock. On an oversubscribed machine each sched_yield() can give the CPU away for a whole time slice, so a lock hand-off can take hundreds of milliseconds. Time the spin: park once it has lasted longer than 100 us, or at once when a single yield took longer than 500 us, and then skip the spin in this thread for the next 10 ms. On an idle machine the limits are never reached, so the spin is unchanged. The new code only shortens the spin; the lock word, parking and hand-off logic are untouched. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 828c153 commit 87a7a38

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
On the :term:`free-threaded build`, a thread waiting for a contended
2+
:c:type:`PyMutex` now stops spinning and parks once the spin takes longer than
3+
expected, for example because the machine has more runnable threads than CPUs.
4+
This greatly reduces lock hand-off latency on oversubscribed machines.

‎Python/lock.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ static const int MAX_SPIN_COUNT = 40;
3131
static const int MAX_SPIN_COUNT = 0;
3232
#endif
3333

34+
#ifdef Py_GIL_DISABLED
35+
// The spin assumes that _Py_yield() returns almost immediately. That holds on
36+
// an idle machine, but on an oversubscribed one each yield can give the CPU
37+
// to another runnable thread for a whole scheduler time slice. These limits
38+
// only ever shorten the spin; they never change how the lock is acquired.
39+
//
40+
// - A spin that has lasted longer than MAX_SPIN_NS parks.
41+
// - A single yield that lasted longer than SLOW_YIELD_NS means the CPU was
42+
// given away. The thread parks at once and does not spin again for the
43+
// next SPIN_DISABLE_NS.
44+
static const PyTime_t MAX_SPIN_NS = 100*1000;
45+
static const PyTime_t SLOW_YIELD_NS = 500*1000;
46+
static const PyTime_t SPIN_DISABLE_NS = 10*1000*1000;
47+
48+
// Monotonic time before which this thread parks without spinning.
49+
static _Py_thread_local PyTime_t spin_disabled_until = 0;
50+
#endif
51+
3452
struct mutex_entry {
3553
// The time after which the unlocking thread should hand off lock ownership
3654
// directly to the waiting thread. Written by the waiting thread.
@@ -79,6 +97,15 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
7997
};
8098

8199
Py_ssize_t spin_count = 0;
100+
#ifdef Py_GIL_DISABLED
101+
// Time the spin started, and time of the last clock read during the
102+
// spin (0 if unknown, e.g. right after parking).
103+
PyTime_t spin_start = now;
104+
PyTime_t last_read = now;
105+
if (now < spin_disabled_until) {
106+
spin_count = MAX_SPIN_COUNT;
107+
}
108+
#endif
82109
for (;;) {
83110
if ((v & _Py_LOCKED) == 0) {
84111
// The lock is unlocked. Try to grab it.
@@ -92,6 +119,19 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
92119
// Spin for a bit.
93120
_Py_yield();
94121
spin_count++;
122+
#ifdef Py_GIL_DISABLED
123+
PyTime_t yielded;
124+
// silently ignore error: a clock of 0 leaves the spin unchanged
125+
(void)PyTime_MonotonicRaw(&yielded);
126+
if (last_read != 0 && yielded - last_read > SLOW_YIELD_NS) {
127+
spin_disabled_until = _PyTime_Add(yielded, SPIN_DISABLE_NS);
128+
spin_count = MAX_SPIN_COUNT;
129+
}
130+
else if (yielded - spin_start > MAX_SPIN_NS) {
131+
spin_count = MAX_SPIN_COUNT;
132+
}
133+
last_read = yielded;
134+
#endif
95135
continue;
96136
}
97137

@@ -122,6 +162,10 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
122162

123163
int ret = _PyParkingLot_Park(&m->_bits, &newv, sizeof(newv), timeout,
124164
&entry, (flags & _PY_LOCK_DETACH) != 0);
165+
#ifdef Py_GIL_DISABLED
166+
// Time spent parked is not a slow yield.
167+
last_read = 0;
168+
#endif
125169
if (ret == Py_PARK_OK) {
126170
if (entry.handed_off) {
127171
// We own the lock now. thread.Lock allows other threads

0 commit comments

Comments
 (0)