mirror of
https://github.com/python/cpython.git
synced 2025-07-13 22:35:18 +00:00
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.
This commit is contained in:
parent
8997f9cd1a
commit
2914bb32e2
4 changed files with 43 additions and 52 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue