8000 gh-59956: Clarify GILState-related Code by ericsnowcurrently · Pull Request #101161 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-59956: Clarify GILState-related Code #101161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 19, 2023
Merged
Prev Previous commit
Next Next commit
Do not use _PyThreadState_GET() in pystate.c.
  • Loading branch information
ericsnowcurrently committed Jan 19, 2023
commit 2f8aaecba9ff4a6cd975d730b9bfdc5dfe7b7b89
34 changes: 18 additions & 16 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "pycore_pyerrors.h"
#include "pycore_pylifecycle.h"
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_pystate.h"
#include "pycore_runtime_init.h" // _PyRuntimeState_INIT
#include "pycore_sysmodule.h"

Expand Down Expand Up @@ -391,7 +391,8 @@ PyInterpreterState *
PyInterpreterState_New(void)
{
PyInterpreterState *interp;
PyThreadState *tstate = _PyThreadState_GET();
_PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = current_fast_get(runtime);

/* tstate is NULL when Py_InitializeFromConfig() calls
PyInterpreterState_New() to create the main interpreter. */
Expand All @@ -408,7 +409,6 @@ PyInterpreterState_New(void)
}

/* Don't get runtime from tstate since tstate can be NULL. */
_PyRuntimeState *runtime = &_PyRuntime;
struct pyinterpreters *interpreters = &runtime->interpreters;

/* We completely serialize creation of multiple interpreters, since
Expand Down Expand Up @@ -556,7 +556,7 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
// Use the current Python thread state to call audit hooks and to collect
// garbage. It can be different than the current Python thread state
// of 'interp'.
PyThreadState *current_tstate = _PyThreadState_GET();
PyThreadState *current_tstate = current_fast_get(interp->runtime);

interpreter_clear(interp, current_tstate);
}
Expand Down Expand Up @@ -672,7 +672,7 @@ _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
PyInterpreterState *
PyInterpreterState_Get(void)
{
PyThreadState *tstate = _PyThreadState_GET();
PyThreadState *tstate = current_fast_get(&_PyRuntime);
_Py_EnsureTstateNotNULL(tstate);
PyInterpreterState *interp = tstate->interp;
if (interp == NULL) {
Expand Down Expand Up @@ -1040,7 +1040,7 @@ PyState_AddModule(PyObject* module, PyModuleDef* def)
return -1;
}

PyThreadState *tstate = _PyThreadState_GET();
PyThreadState *tstate = current_fast_get(&_PyRuntime);
PyInterpreterState *interp = tstate->interp;
Py_ssize_t index = def->m_base.m_index;
if (interp->modules_by_index &&
Expand All @@ -1056,7 +1056,7 @@ PyState_AddModule(PyObject* module, PyModuleDef* def)
int
PyState_RemoveModule(PyModuleDef* def)
{
PyThreadState *tstate = _PyThreadState_GET();
PyThreadState *tstate = current_fast_get(&_PyRuntime);
PyInterpreterState *interp = tstate->interp;

if (def->m_slots) {
Expand Down Expand Up @@ -1282,14 +1282,14 @@ _PyThreadState_DeleteExcept(_PyRuntimeState *runtime, PyThreadState *tstate)
PyThreadState *
_PyThreadState_UncheckedGet(void)
{
return _PyThreadState_GET();
return current_fast_get(&_PyRuntime);
}


PyThreadState *
PyThreadState_Get(void)
{
PyThreadState *tstate = _PyThreadState_GET();
PyThreadState *tstate = current_fast_get(&_PyRuntime);
_Py_EnsureTstateNotNULL(tstate);
return tstate;
}
Expand Down Expand Up @@ -1355,7 +1355,7 @@ _PyThreadState_GetDict(PyThreadState *tstate)
PyObject *
PyThreadState_GetDict(void)
{
PyThreadState *tstate = _PyThreadState_GET();
PyThreadState *tstate = current_fast_get(&_PyRuntime);
if (tstate == NULL) {
return NULL;
}
Expand Down Expand Up @@ -1478,7 +1478,8 @@ PyThreadState_Next(PyThreadState *tstate) {
PyObject *
_PyThread_CurrentFrames(void)
{
PyThreadState *tstate = _PyThreadState_GET();
_PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = current_fast_get(runtime);
if (_PySys_Audit(tstate, "sys._current_frames", NULL) < 0) {
return NULL;
}
DEF3 Expand All @@ -1494,7 +1495,6 @@ _PyThread_CurrentFrames(void)
* Because these lists can mutate even when the GIL is held, we
* need to grab head_mutex for the duration.
*/
_PyRuntimeState *runtime = tstate->interp->runtime;
HEAD_LOCK(runtime);
PyInterpreterState *i;
for (i = runtime->interpreters.head; i != NULL; i = i->next) {
Expand Down Expand Up @@ -1534,7 +1534,8 @@ _PyThread_CurrentFrames(void)
PyObject *
_PyThread_CurrentExceptions(void)
{
PyThreadState *tstate = _PyThreadState_GET();
_PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = current_fast_get(runtime);

_Py_EnsureTstateNotNULL(tstate);

Expand All @@ -1553,7 +1554,6 @@ _PyThread_CurrentExceptions(void)
* Because these lists can mutate even when the GIL is held, we
* need to grab head_mutex for the duration.
*/
_PyRuntimeState *runtime = tstate->interp->runtime;
HEAD_LOCK(runtime);
PyInterpreterState *i;
for (i = runtime->interpreters.head; i != NULL; i = i->next) {
Expand Down Expand Up @@ -1983,7 +1983,8 @@ _PyObject_CheckCrossInterpreterData(PyObject *obj)
int
_PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
{
PyThreadState *tstate = _PyThreadState_GET();
_PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = current_fast_get(runtime);
#ifdef Py_DEBUG
// The caller must hold the GIL
_Py_EnsureTstateNotNULL(tstate);
Expand Down Expand Up @@ -2391,8 +2392,9 @@ _PyInterpreterState_GetConfigCopy(PyConfig *config)
const PyConfig*
_Py_GetConfig(void)
{
_PyRuntimeState *runtime = &_PyRuntime;
assert(PyGILState_Check());
PyThreadState *tstate = _PyThreadState_GET();
PyThreadState *tstate = current_fast_get(runtime);
_Py_EnsureTstateNotNULL(tstate);
return _PyInterpreterState_GetConfig(tstate->interp);
}
Expand Down
0