diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2019-03-08 23:44:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-08 23:44:33 -0700 |
commit | 8479a3426eb7d1840473f7788e639954363ed37e (patch) | |
tree | 9d588c5c30e29d0ab6d6aa0e25c53caba8c5b593 /Python/pystate.c | |
parent | 5be45a6105d656c551adeee7770afdc3b806fbb5 (diff) | |
download | cpython-git-8479a3426eb7d1840473f7788e639954363ed37e.tar.gz |
bpo-33608: Make sure locks in the runtime are properly re-created. (gh-12245)
Diffstat (limited to 'Python/pystate.c')
-rw-r--r-- | Python/pystate.c | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/Python/pystate.c b/Python/pystate.c index ec8dba8ee5..3978baa7af 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -60,7 +60,8 @@ _PyRuntimeState_Init_impl(_PyRuntimeState *runtime) return _Py_INIT_ERR("Can't initialize threads for cross-interpreter data registry"); } - // runtime->main_thread is set in PyEval_InitThreads(). + // Set it to the ID of the main thread of the main interpreter. + runtime->main_thread = PyThread_get_thread_ident(); return _Py_INIT_OK(); } @@ -94,6 +95,32 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime) PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); } +/* This function is called from PyOS_AfterFork_Child to ensure that + * newly created child processes do not share locks with the parent. + */ + +void +_PyRuntimeState_ReInitThreads(void) +{ + // This was initially set in _PyRuntimeState_Init(). + _PyRuntime.main_thread = PyThread_get_thread_ident(); + + _PyRuntime.interpreters.mutex = PyThread_allocate_lock(); + if (_PyRuntime.interpreters.mutex == NULL) { + Py_FatalError("Can't initialize lock for runtime interpreters"); + } + + _PyRuntime.interpreters.main->id_mutex = PyThread_allocate_lock(); + if (_PyRuntime.interpreters.main->id_mutex == NULL) { + Py_FatalError("Can't initialize ID lock for main interpreter"); + } + + _PyRuntime.xidregistry.mutex = PyThread_allocate_lock(); + if (_PyRuntime.xidregistry.mutex == NULL) { + Py_FatalError("Can't initialize lock for cross-interpreter data registry"); + } +} + #define HEAD_LOCK() PyThread_acquire_lock(_PyRuntime.interpreters.mutex, \ WAIT_LOCK) #define HEAD_UNLOCK() PyThread_release_lock(_PyRuntime.interpreters.mutex) |