diff options
Diffstat (limited to 'Python/pystate.c')
-rw-r--r-- | Python/pystate.c | 32 |
1 files changed, 13 insertions, 19 deletions
diff --git a/Python/pystate.c b/Python/pystate.c index f907fc1fc9..a926e9753c 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1280,27 +1280,28 @@ PyGILState_Check(void) PyGILState_STATE PyGILState_Ensure(void) { - struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate; - int current; - PyThreadState *tcur; - int need_init_threads = 0; + _PyRuntimeState *runtime = &_PyRuntime; + struct _gilstate_runtime_state *gilstate = &runtime->gilstate; /* Note that we do not auto-init Python here - apart from potential races with 2 threads auto-initializing, pep-311 spells out other issues. Embedders are expected to have - called Py_Initialize() and usually PyEval_InitThreads(). - */ - /* Py_Initialize() hasn't been called! */ + called Py_Initialize(). */ + + /* Ensure that _PyEval_InitThreads() and _PyGILState_Init() have been + called by Py_Initialize() */ + assert(_PyEval_ThreadsInitialized(runtime)); assert(gilstate->autoInterpreterState); - tcur = (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey); + PyThreadState *tcur = (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey); + int current; if (tcur == NULL) { - need_init_threads = 1; - - /* Create a new thread state for this thread */ + /* Create a new Python thread state for this thread */ tcur = PyThreadState_New(gilstate->autoInterpreterState); - if (tcur == NULL) + if (tcur == NULL) { Py_FatalError("Couldn't create thread-state for new thread"); + } + /* This is our thread state! We'll need to delete it in the matching call to PyGILState_Release(). */ tcur->gilstate_counter = 0; @@ -1321,13 +1322,6 @@ PyGILState_Ensure(void) */ ++tcur->gilstate_counter; - if (need_init_threads) { - /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is - called from a new thread for the first time, we need the create the - GIL. */ - PyEval_InitThreads(); - } - return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; } |