diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2018-01-29 10:57:45 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-29 10:57:45 (GMT) |
commit | 2914bb32e2adf8dff77c0ca58b33201bc94e398c (patch) | |
tree | 1a2e9e064f9dbc362c2b3c5bbb52affadaa448fa /Python | |
parent | 8997f9cd1a59f04fbb8c7b590295a9f38c548744 (diff) | |
download | cpython-2914bb32e2adf8dff77c0ca58b33201bc94e398c.zip cpython-2914bb32e2adf8dff77c0ca58b33201bc94e398c.tar.gz cpython-2914bb32e2adf8dff77c0ca58b33201bc94e398c.tar.bz2 |
bpo-20891: Py_Initialize() now creates the GIL (#4700)
The GIL is no longer created "on demand" to fix a race condition when
PyGILState_Ensure() is called in a non-Python thread.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/ceval.c | 25 | ||||
-rw-r--r-- | Python/pylifecycle.c | 4 |
2 files changed, 17 insertions, 12 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index 128ec2c..52a42b0 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -254,8 +254,8 @@ PyEval_SaveThread(void) PyThreadState *tstate = PyThreadState_Swap(NULL); if (tstate == NULL) Py_FatalError("PyEval_SaveThread: NULL tstate"); - if (gil_created()) - drop_gil(tstate); + assert(gil_created()); + drop_gil(tstate); return tstate; } @@ -264,17 +264,18 @@ PyEval_RestoreThread(PyThreadState *tstate) { if (tstate == NULL) Py_FatalError("PyEval_RestoreThread: NULL tstate"); - if (gil_created()) { - int err = errno; - take_gil(tstate); - /* _Py_Finalizing is protected by the GIL */ - if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) { - drop_gil(tstate); - PyThread_exit_thread(); - Py_UNREACHABLE(); - } - errno = err; + assert(gil_created()); + + int err = errno; + take_gil(tstate); + /* _Py_Finalizing is protected by the GIL */ + if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) { + drop_gil(tstate); + PyThread_exit_thread(); + Py_UNREACHABLE(); } + errno = err; + PyThreadState_Swap(tstate); } diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index d46784a..82ab915 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -681,9 +681,13 @@ _Py_InitializeCore(const _PyCoreConfig *core_config) Instead we destroy the previously created GIL here, which ensures that we can call Py_Initialize / Py_FinalizeEx multiple times. */ _PyEval_FiniThreads(); + /* Auto-thread-state API */ _PyGILState_Init(interp, tstate); + /* Create the GIL */ + PyEval_InitThreads(); + _Py_ReadyTypes(); if (!_PyFrame_Init()) |