summaryrefslogtreecommitdiffstats
path: root/Python/pystate.c
diff options
context:
space:
mode:
authormpage <mpage@meta.com>2024-04-08 14:58:38 (GMT)
committerGitHub <noreply@github.com>2024-04-08 14:58:38 (GMT)
commitdf7317904849a41d51db39d92c5d431a18e22637 (patch)
treee7dddcb5006cb6f50b9f47477217043157a42e01 /Python/pystate.c
parente16062dd3428a5846344e0a8c6ee2f352d34ce1b (diff)
downloadcpython-df7317904849a41d51db39d92c5d431a18e22637.zip
cpython-df7317904849a41d51db39d92c5d431a18e22637.tar.gz
cpython-df7317904849a41d51db39d92c5d431a18e22637.tar.bz2
gh-111926: Make weakrefs thread-safe in free-threaded builds (#117168)
Most mutable data is protected by a striped lock that is keyed on the referenced object's address. The weakref's hash is protected using the weakref's per-object lock. Note that this only affects free-threaded builds. Apart from some minor refactoring, the added code is all either gated by `ifdef`s or is a no-op (e.g. `Py_BEGIN_CRITICAL_SECTION`).
Diffstat (limited to 'Python/pystate.c')
-rw-r--r--Python/pystate.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index 892e740..cee481c 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -506,6 +506,15 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
for (size_t i = 0; i < Py_ARRAY_LENGTH(locks); i++) {
_PyMutex_at_fork_reinit(locks[i]);
}
+#ifdef Py_GIL_DISABLED
+ for (PyInterpreterState *interp = runtime->interpreters.head;
+ interp != NULL; interp = interp->next)
+ {
+ for (int i = 0; i < NUM_WEAKREF_LIST_LOCKS; i++) {
+ _PyMutex_at_fork_reinit(&interp->weakref_locks[i]);
+ }
+ }
+#endif
_PyTypes_AfterFork();