Skip to content

Commit 55ab336

Browse files
[3.15] gh-157914: Revert "gh-135871: Reload lock internal state while spinning in PyMutex_LockTimed (gh-146064)" (gh-158082) (#158290)
Co-authored-by: Daniele Parmeggiani <daniele.parmeggiani@arm.com>
1 parent 545124f commit 55ab336

2 files changed

Lines changed: 2 additions & 15 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Revert :gh:`146064`. The linked PyMutex change was
2+
causing a significant performance regression in some cases.

‎Python/lock.c‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ static const PyTime_t TIME_TO_BE_FAIR_NS = 1000*1000;
2727
// enabled.
2828
#if Py_GIL_DISABLED
2929
static const int MAX_SPIN_COUNT = 40;
30-
static const int RELOAD_SPIN_MASK = 3;
3130
#else
3231
static const int MAX_SPIN_COUNT = 0;
33-
static const int RELOAD_SPIN_MASK = 1;
3432
#endif
3533

3634
struct mutex_entry {
@@ -81,16 +79,6 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
8179
};
8280

8381
Py_ssize_t spin_count = 0;
84-
#ifdef Py_GIL_DISABLED
85-
// Using thread-id as a way of reducing contention further in the reload below.
86-
// It adds a pseudo-random starting offset to the recurrence, so that threads
87-
// are less likely to try and run compare-exchange at the same time.
88-
// The lower bits of platform thread ids are likely to not be random,
89-
// hence the right shift.
90-
const Py_ssize_t tid = (Py_ssize_t)(_Py_ThreadId() >> 12);
91-
#else
92-
const Py_ssize_t tid = 0;
93-
#endif
9482
for (;;) {
9583
if ((v & _Py_LOCKED) == 0) {
9684
// The lock is unlocked. Try to grab it.
@@ -104,9 +92,6 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
10492
// Spin for a bit.
10593
_Py_yield();
10694
spin_count++;
107-
if (((spin_count + tid) & RELOAD_SPIN_MASK) == 0) {
108-
v = _Py_atomic_load_uint8_relaxed(&m->_bits);
109-
}
11095
continue;
11196
}
11297

0 commit comments

Comments
 (0)