8000 gh-135871: Fix needless spinning in _PyMutex_LockTimed (timeout==0) · python/cpython@45811fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 45811fd

Browse files
committed
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).
1 parent 34393cb commit 45811fd

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bpo-135871: _PyMutex_LockTimed() no longer spins or fails for timeout==0 in no-GIL builds; the spin loop now reloads the lock state.

Python/lock.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
5858
return PY_LOCK_ACQUIRED;
5959
}
6060
}
61-
else if (timeout == 0) {
61+
if (timeout == 0) {
6262
return PY_LOCK_FAILURE;
6363
}
6464

@@ -89,6 +89,7 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
8989
// Spin for a bit.
9090
_Py_yield();
9191
spin_count++;
92+
v = _Py_atomic_load_uint8_relaxed(&m->_bits);
9293
continue;
9394
}
9495

0 commit comments

Comments
 (0)
0