mirror of
https://github.com/python/cpython.git
synced 2026-06-21 10:34:26 -04:00
gh-150902: Optimize PyCriticalSection2 to skip locking the same locks held by the current CS2
This mimics an optimization already present for the single-mutex critical section.
This commit is contained in:
committed by
GitHub
parent
7edcaedc53
commit
c2ca7724af
@@ -72,6 +72,22 @@ _PyCriticalSection2_BeginSlow(PyThreadState *tstate, PyCriticalSection2 *c, PyMu
|
||||
c->_cs_base._cs_prev = 0;
|
||||
return;
|
||||
}
|
||||
// Same optimization as in _PyCriticalSection_BeginSlow: skip locking when
|
||||
// recursively acquiring the same locks.
|
||||
if (tstate->critical_section &&
|
||||
tstate->critical_section & _Py_CRITICAL_SECTION_TWO_MUTEXES) {
|
||||
PyCriticalSection2 *prev2 = (PyCriticalSection2 *)
|
||||
untag_critical_section(tstate->critical_section);
|
||||
assert((uintptr_t)m1 < (uintptr_t)m2);
|
||||
assert((uintptr_t)prev2->_cs_base._cs_mutex <
|
||||
(uintptr_t)prev2->_cs_mutex2);
|
||||
if (prev2->_cs_base._cs_mutex == m1 && prev2->_cs_mutex2 == m2) {
|
||||
c->_cs_base._cs_mutex = NULL;
|
||||
c->_cs_mutex2 = NULL;
|
||||
c->_cs_base._cs_prev = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
c->_cs_base._cs_mutex = NULL;
|
||||
c->_cs_mutex2 = NULL;
|
||||
c->_cs_base._cs_prev = tstate->critical_section;
|
||||
|
||||
Reference in New Issue
Block a user