Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions Include/internal/pycore_pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,12 @@ extern PyStatus _PyFaulthandler_Init(int enable);
extern int _PyTraceMalloc_Init(int enable);
extern PyObject * _PyBuiltin_Init(PyThreadState *tstate);
extern PyStatus _PySys_Create(
struct pyruntimestate *runtime,
PyThreadState *tstate,
PyObject **sysmod_p);
extern PyStatus _PySys_SetPreliminaryStderr(PyObject *sysdict);
extern PyStatus _PySys_ReadPreinitWarnOptions(PyWideStringList *options);
extern PyStatus _PySys_ReadPreinitXOptions(PyConfig *config);
extern int _PySys_InitMain(
struct pyruntimestate *runtime,
PyThreadState *tstate);
extern int _PySys_InitMain(PyThreadState *tstate);
extern PyStatus _PyImport_Init(PyThreadState *tstate);
extern PyStatus _PyExc_Init(void);
extern PyStatus _PyErr_Init(void);
Expand All @@ -57,7 +54,7 @@ extern PyStatus _Py_HashRandomization_Init(const PyConfig *);

extern PyStatus _PyTypes_Init(void);
extern PyStatus _PyImportZip_Init(PyThreadState *tstate);
extern PyStatus _PyGC_Init(struct pyruntimestate *runtime);
extern PyStatus _PyGC_Init(PyThreadState *tstate);


/* Various internal finalizers */
Expand Down Expand Up @@ -89,9 +86,7 @@ extern void _PyHash_Fini(void);
extern void _PyTraceMalloc_Fini(void);
extern void _PyWarnings_Fini(PyInterpreterState *interp);

extern void _PyGILState_Init(
struct pyruntimestate *runtime,
PyThreadState *tstate);
extern void _PyGILState_Init(PyThreadState *tstate);
extern void _PyGILState_Fini(struct pyruntimestate *runtime);

PyAPI_FUNC(void) _PyGC_DumpShutdownStats(struct pyruntimestate *runtime);
Expand Down
6 changes: 5 additions & 1 deletion Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ struct _is {
struct _is *next;
struct _ts *tstate_head;

/* Reference to the _PyRuntime global variable. This field exists
to not have to pass runtime in addition to tstate to a function.
Get runtime from tstate: tstate->interp->runtime. */
struct pyruntimestate *runtime;

int64_t id;
int64_t id_refcount;
int requires_idref;
Expand Down Expand Up @@ -301,7 +306,6 @@ PyAPI_FUNC(void) _PyRuntime_Finalize(void);
/* Other */

PyAPI_FUNC(void) _PyThreadState_Init(
_PyRuntimeState *runtime,
PyThreadState *tstate);
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(
_PyRuntimeState *runtime,
Expand Down
2 changes: 1 addition & 1 deletion Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ t_bootstrap(void *boot_raw)
runtime = boot->runtime;
tstate = boot->tstate;
tstate->thread_id = PyThread_get_thread_ident();
_PyThreadState_Init(runtime, tstate);
_PyThreadState_Init(tstate);
PyEval_AcquireThread(tstate);
tstate->interp->num_threads++;
res = PyObject_Call(boot->func, boot->args, boot->keyw);
Expand Down
4 changes: 2 additions & 2 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ _PyGC_InitializeRuntime(struct _gc_runtime_state *state)


PyStatus
_PyGC_Init(_PyRuntimeState *runtime)
_PyGC_Init(PyThreadState *tstate)
{
struct _gc_runtime_state *state = &runtime->gc;
struct _gc_runtime_state *state = &tstate->interp->runtime->gc;
if (state->garbage == NULL) {
state->garbage = PyList_New(0);
if (state->garbage == NULL) {
Expand Down
28 changes: 16 additions & 12 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ static size_t opcache_global_misses = 0;
int
PyEval_ThreadsInitialized(void)
{
return gil_created(&_PyRuntime.ceval.gil);
_PyRuntimeState *runtime = &_PyRuntime;
return gil_created(&runtime->ceval.gil);
}

void
Expand Down Expand Up @@ -235,8 +236,9 @@ _PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
}

static inline void
exit_thread_if_finalizing(_PyRuntimeState *runtime, PyThreadState *tstate)
exit_thread_if_finalizing(PyThreadState *tstate)
{
_PyRuntimeState *runtime = tstate->interp->runtime;
/* _Py_Finalizing is protected by the GIL */
if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
drop_gil(&runtime->ceval, tstate);
Expand Down Expand Up @@ -283,7 +285,7 @@ PyEval_AcquireLock(void)
Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
}
take_gil(ceval, tstate);
exit_thread_if_finalizing(runtime, tstate);
exit_thread_if_finalizing(tstate);
}

void
Expand All @@ -305,13 +307,13 @@ PyEval_AcquireThread(PyThreadState *tstate)
Py_FatalError("PyEval_AcquireThread: NULL new thread state");
}

_PyRuntimeState *runtime = &_PyRuntime;
_PyRuntimeState *runtime = tstate->interp->runtime;
struct _ceval_runtime_state *ceval = &runtime->ceval;

/* Check someone has called PyEval_InitThreads() to create the lock */
assert(gil_created(&ceval->gil));
take_gil(ceval, tstate);
exit_thread_if_finalizing(runtime, tstate);
exit_thread_if_finalizing(tstate);
if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
}
Expand All @@ -324,7 +326,7 @@ PyEval_ReleaseThread(PyThreadState *tstate)
Py_FatalError("PyEval_ReleaseThread: NULL thread state");
}

_PyRuntimeState *runtime = &_PyRuntime;
_PyRuntimeState *runtime = tstate->interp->runtime;
PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
if (new_tstate != tstate) {
Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Expand Down Expand Up @@ -384,7 +386,7 @@ PyEval_SaveThread(void)
void
PyEval_RestoreThread(PyThreadState *tstate)
{
_PyRuntimeState *runtime = &_PyRuntime;
_PyRuntimeState *runtime = tstate->interp->runtime;
struct _ceval_runtime_state *ceval = &runtime->ceval;

if (tstate == NULL) {
Expand All @@ -394,7 +396,7 @@ PyEval_RestoreThread(PyThreadState *tstate)

int err = errno;
take_gil(ceval, tstate);
exit_thread_if_finalizing(runtime, tstate);
exit_thread_if_finalizing(tstate);
errno = err;

_PyThreadState_Swap(&runtime->gilstate, tstate);
Expand Down Expand Up @@ -649,7 +651,8 @@ _PyEval_Initialize(struct _ceval_runtime_state *state)
int
Py_GetRecursionLimit(void)
{
return _PyRuntime.ceval.recursion_limit;
struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
return ceval->recursion_limit;
}

void
Expand All @@ -668,7 +671,7 @@ Py_SetRecursionLimit(int new_limit)
int
_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
{
_PyRuntimeState *runtime = &_PyRuntime;
_PyRuntimeState *runtime = tstate->interp->runtime;
int recursion_limit = runtime->ceval.recursion_limit;

#ifdef USE_STACKCHECK
Expand Down Expand Up @@ -1245,7 +1248,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
take_gil(ceval, tstate);

/* Check if we should make a quick exit. */
exit_thread_if_finalizing(runtime, tstate);
exit_thread_if_finalizing(tstate);

if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Py_FatalError("ceval: orphan tstate");
Expand Down Expand Up @@ -4806,7 +4809,8 @@ _PyEval_GetAsyncGenFinalizer(void)
static PyFrameObject *
_PyEval_GetFrame(PyThreadState *tstate)
{
return _PyRuntime.gilstate.getframe(tstate);
_PyRuntimeState *runtime = tstate->interp->runtime;
return runtime->gilstate.getframe(tstate);
}

PyFrameObject *
Expand Down
23 changes: 12 additions & 11 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
_PyEval_FiniThreads(&runtime->ceval);

/* Auto-thread-state API */
_PyGILState_Init(runtime, tstate);
_PyGILState_Init(tstate);

/* Create the GIL */
PyEval_InitThreads();
Expand All @@ -558,11 +558,11 @@ pycore_create_interpreter(_PyRuntimeState *runtime,


static PyStatus
pycore_init_types(_PyRuntimeState *runtime)
pycore_init_types(PyThreadState *tstate)
{
PyStatus status;

status = _PyGC_Init(runtime);
status = _PyGC_Init(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
Expand Down Expand Up @@ -690,13 +690,13 @@ pyinit_config(_PyRuntimeState *runtime,
config = &tstate->interp->config;
*tstate_p = tstate;

status = pycore_init_types(runtime);
status = pycore_init_types(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}

PyObject *sysmod;
status = _PySys_Create(runtime, tstate, &sysmod);
status = _PySys_Create(tstate, &sysmod);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
Expand Down Expand Up @@ -915,8 +915,9 @@ _Py_ReconfigureMainInterpreter(PyInterpreterState *interp)
* non-zero return code.
*/
static PyStatus
pyinit_main(_PyRuntimeState *runtime, PyThreadState *tstate)
pyinit_main(PyThreadState *tstate)
{
_PyRuntimeState *runtime = tstate->interp->runtime;
if (!runtime->core_initialized) {
return _PyStatus_ERR("runtime core not initialized");
}
Expand All @@ -943,7 +944,7 @@ pyinit_main(_PyRuntimeState *runtime, PyThreadState *tstate)
return _PyStatus_ERR("can't initialize time");
}

if (_PySys_InitMain(runtime, tstate) < 0) {
if (_PySys_InitMain(tstate) < 0) {
return _PyStatus_ERR("can't finish initializing sys");
}

Expand Down Expand Up @@ -1022,7 +1023,7 @@ _Py_InitializeMain(void)
}
_PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
return pyinit_main(runtime, tstate);
return pyinit_main(tstate);
}


Expand All @@ -1049,7 +1050,7 @@ Py_InitializeFromConfig(const PyConfig *config)
config = &tstate->interp->config;

if (config->_init_main) {
status = pyinit_main(runtime, tstate);
status = pyinit_main(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
Expand Down Expand Up @@ -1454,7 +1455,7 @@ new_interpreter(PyThreadState **tstate_p)
}
config = &interp->config;

status = pycore_init_types(runtime);
status = pycore_init_types(tstate);

/* XXX The following is lax in error checking */
PyObject *modules = PyDict_New();
Expand All @@ -1471,7 +1472,7 @@ new_interpreter(PyThreadState **tstate_p)
}
Py_INCREF(interp->sysdict);
PyDict_SetItemString(interp->sysdict, "modules", modules);
if (_PySys_InitMain(runtime, tstate) < 0) {
if (_PySys_InitMain(tstate) < 0) {
return _PyStatus_ERR("can't finish initializing sys");
}
}
Expand Down
Loading