[2.7] bpo-20891: Fix PyGILState_Ensure() (#4650) by vstinner · Pull Request #4657 · python/cpython
/* 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
/* Create a new thread state for this thread */ tcur = PyThreadState_New(autoInterpreterState); if (tcur == NULL)
if (current == 0) { PyEval_RestoreThread(tcur); }
/* Update our counter in the thread-state - no need for locks: - tcur will remain valid as we hold the GIL. - the counter is safe as we are the only thread "allowed" to modify this value */ ++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; }