mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
[3.13] gh-117657: Fix TSAN race involving import lock (GH-118523) (#120169)
This adds a `_PyRecursiveMutex` type based on `PyMutex` and uses that
for the import lock. This fixes some data races in the free-threaded
build and generally simplifies the import lock code.
(cherry picked from commit e21057b999
)
Co-authored-by: Sam Gross <colesbury@gmail.com>
This commit is contained in:
parent
015ddfeca5
commit
517733ce3c
7 changed files with 90 additions and 105 deletions
|
@ -366,6 +366,48 @@ _PyOnceFlag_CallOnceSlow(_PyOnceFlag *flag, _Py_once_fn_t *fn, void *arg)
|
|||
}
|
||||
}
|
||||
|
||||
static int
|
||||
recursive_mutex_is_owned_by(_PyRecursiveMutex *m, PyThread_ident_t tid)
|
||||
{
|
||||
return _Py_atomic_load_ullong_relaxed(&m->thread) == tid;
|
||||
}
|
||||
|
||||
int
|
||||
_PyRecursiveMutex_IsLockedByCurrentThread(_PyRecursiveMutex *m)
|
||||
{
|
||||
return recursive_mutex_is_owned_by(m, PyThread_get_thread_ident_ex());
|
||||
}
|
||||
|
||||
void
|
||||
_PyRecursiveMutex_Lock(_PyRecursiveMutex *m)
|
||||
{
|
||||
PyThread_ident_t thread = PyThread_get_thread_ident_ex();
|
||||
if (recursive_mutex_is_owned_by(m, thread)) {
|
||||
m->level++;
|
||||
return;
|
||||
}
|
||||
PyMutex_Lock(&m->mutex);
|
||||
_Py_atomic_store_ullong_relaxed(&m->thread, thread);
|
||||
assert(m->level == 0);
|
||||
}
|
||||
|
||||
void
|
||||
_PyRecursiveMutex_Unlock(_PyRecursiveMutex *m)
|
||||
{
|
||||
PyThread_ident_t thread = PyThread_get_thread_ident_ex();
|
||||
if (!recursive_mutex_is_owned_by(m, thread)) {
|
||||
Py_FatalError("unlocking a recursive mutex that is not owned by the"
|
||||
" current thread");
|
||||
}
|
||||
if (m->level > 0) {
|
||||
m->level--;
|
||||
return;
|
||||
}
|
||||
assert(m->level == 0);
|
||||
_Py_atomic_store_ullong_relaxed(&m->thread, 0);
|
||||
PyMutex_Unlock(&m->mutex);
|
||||
}
|
||||
|
||||
#define _Py_WRITE_LOCKED 1
|
||||
#define _PyRWMutex_READER_SHIFT 2
|
||||
#define _Py_RWMUTEX_MAX_READERS (UINTPTR_MAX >> _PyRWMutex_READER_SHIFT)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue