summaryrefslogtreecommitdiffstats
path: root/Include/internal/pycore_lock.h
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2024-06-06 17:40:58 (GMT)
committerGitHub <noreply@github.com>2024-06-06 17:40:58 (GMT)
commite21057b99967eb5323320e6d1121955e0cd2985e (patch)
tree10bff96fbf34c833c65258d6565ed47cbbd2c62c /Include/internal/pycore_lock.h
parent417bec733c11e63df559ecf898802dbef590142e (diff)
downloadcpython-e21057b99967eb5323320e6d1121955e0cd2985e.zip
cpython-e21057b99967eb5323320e6d1121955e0cd2985e.tar.gz
cpython-e21057b99967eb5323320e6d1121955e0cd2985e.tar.bz2
gh-117657: Fix TSAN race involving import lock (#118523)
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.
Diffstat (limited to 'Include/internal/pycore_lock.h')
-rw-r--r--Include/internal/pycore_lock.h12
1 files changed, 12 insertions, 0 deletions
diff --git a/Include/internal/pycore_lock.h b/Include/internal/pycore_lock.h
index a5b28e4..d5853b2 100644
--- a/Include/internal/pycore_lock.h
+++ b/Include/internal/pycore_lock.h
@@ -219,6 +219,18 @@ _PyOnceFlag_CallOnce(_PyOnceFlag *flag, _Py_once_fn_t *fn, void *arg)
return _PyOnceFlag_CallOnceSlow(flag, fn, arg);
}
+// A recursive mutex. The mutex should zero-initialized.
+typedef struct {
+ PyMutex mutex;
+ unsigned long long thread; // i.e., PyThread_get_thread_ident_ex()
+ size_t level;
+} _PyRecursiveMutex;
+
+PyAPI_FUNC(int) _PyRecursiveMutex_IsLockedByCurrentThread(_PyRecursiveMutex *m);
+PyAPI_FUNC(void) _PyRecursiveMutex_Lock(_PyRecursiveMutex *m);
+PyAPI_FUNC(void) _PyRecursiveMutex_Unlock(_PyRecursiveMutex *m);
+
+
// A readers-writer (RW) lock. The lock supports multiple concurrent readers or
// a single writer. The lock is write-preferring: if a writer is waiting while
// the lock is read-locked then, new readers will be blocked. This avoids