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);
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 */
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);
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;
/* Other */
PyAPI_FUNC(void) _PyThreadState_Init(
- _PyRuntimeState *runtime,
PyThreadState *tstate);
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(
_PyRuntimeState *runtime,
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);
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) {
int
PyEval_ThreadsInitialized(void)
{
- return gil_created(&_PyRuntime.ceval.gil);
+ _PyRuntimeState *runtime = &_PyRuntime;
+ return gil_created(&runtime->ceval.gil);
}
void
}
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);
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
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");
}
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");
void
PyEval_RestoreThread(PyThreadState *tstate)
{
- _PyRuntimeState *runtime = &_PyRuntime;
+ _PyRuntimeState *runtime = tstate->interp->runtime;
struct _ceval_runtime_state *ceval = &runtime->ceval;
if (tstate == NULL) {
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);
int
Py_GetRecursionLimit(void)
{
- return _PyRuntime.ceval.recursion_limit;
+ struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
+ return ceval->recursion_limit;
}
void
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
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");
static PyFrameObject *
_PyEval_GetFrame(PyThreadState *tstate)
{
- return _PyRuntime.gilstate.getframe(tstate);
+ _PyRuntimeState *runtime = tstate->interp->runtime;
+ return runtime->gilstate.getframe(tstate);
}
PyFrameObject *
_PyEval_FiniThreads(&runtime->ceval);
/* Auto-thread-state API */
- _PyGILState_Init(runtime, tstate);
+ _PyGILState_Init(tstate);
/* Create the GIL */
PyEval_InitThreads();
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;
}
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;
}
* 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");
}
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");
}
}
_PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
- return pyinit_main(runtime, tstate);
+ return pyinit_main(tstate);
}
config = &tstate->interp->config;
if (config->_init_main) {
- status = pyinit_main(runtime, tstate);
+ status = pyinit_main(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
}
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();
}
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");
}
}
/* Forward declarations */
static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
-static void _PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate);
static PyStatus
memset(interp, 0, sizeof(*interp));
interp->id_refcount = -1;
+ _PyRuntimeState *runtime = &_PyRuntime;
+ interp->runtime = runtime;
+
PyConfig_InitPythonConfig(&interp->config);
interp->eval_frame = _PyEval_EvalFrameDefault;
#endif
#endif
- _PyRuntimeState *runtime = &_PyRuntime;
struct pyinterpreters *interpreters = &runtime->interpreters;
HEAD_LOCK(runtime);
}
-static void
-_PyInterpreterState_Clear(_PyRuntimeState *runtime, PyInterpreterState *interp)
+void
+PyInterpreterState_Clear(PyInterpreterState *interp)
{
+ _PyRuntimeState *runtime = interp->runtime;
+
if (PySys_Audit("cpython.PyInterpreterState_Clear", NULL) < 0) {
PyErr_Clear();
}
// objects have been cleaned up at the point.
}
-void
-PyInterpreterState_Clear(PyInterpreterState *interp)
-{
- _PyInterpreterState_Clear(&_PyRuntime, interp);
-}
-
static void
-zapthreads(_PyRuntimeState *runtime, PyInterpreterState *interp)
+zapthreads(PyInterpreterState *interp)
{
PyThreadState *p;
/* No need to lock the mutex here because this should only happen
when the threads are all really dead (XXX famous last words). */
while ((p = interp->tstate_head) != NULL) {
- _PyThreadState_Delete(runtime, p);
+ PyThreadState_Delete(p);
}
}
-static void
-_PyInterpreterState_Delete(_PyRuntimeState *runtime,
- PyInterpreterState *interp)
+void
+PyInterpreterState_Delete(PyInterpreterState *interp)
{
+ _PyRuntimeState *runtime = interp->runtime;
struct pyinterpreters *interpreters = &runtime->interpreters;
- zapthreads(runtime, interp);
+ zapthreads(interp);
HEAD_LOCK(runtime);
PyInterpreterState **p;
for (p = &interpreters->head; ; p = &(*p)->next) {
}
-void
-PyInterpreterState_Delete(PyInterpreterState *interp)
-{
- _PyInterpreterState_Delete(&_PyRuntime, interp);
-}
-
-
/*
* Delete all interpreter states except the main interpreter. If there
* is a current interpreter state, it *must* be the main interpreter.
continue;
}
- _PyInterpreterState_Clear(runtime, interp); // XXX must activate?
- zapthreads(runtime, interp);
+ PyInterpreterState_Clear(interp); // XXX must activate?
+ zapthreads(interp);
if (interp->id_mutex != NULL) {
PyThread_free_lock(interp->id_mutex);
}
static PyThreadState *
new_threadstate(PyInterpreterState *interp, int init)
{
- _PyRuntimeState *runtime = &_PyRuntime;
+ _PyRuntimeState *runtime = interp->runtime;
PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
if (tstate == NULL) {
return NULL;
tstate->id = ++interp->tstate_next_unique_id;
if (init) {
- _PyThreadState_Init(runtime, tstate);
+ _PyThreadState_Init(tstate);
}
HEAD_LOCK(runtime);
}
void
-_PyThreadState_Init(_PyRuntimeState *runtime, PyThreadState *tstate)
+_PyThreadState_Init(PyThreadState *tstate)
{
- _PyGILState_NoteThreadState(&runtime->gilstate, tstate);
+ _PyGILState_NoteThreadState(&tstate->interp->runtime->gilstate, tstate);
}
PyObject*
/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
static void
-tstate_delete_common(_PyRuntimeState *runtime, PyThreadState *tstate)
+tstate_delete_common(PyThreadState *tstate)
{
+ _PyRuntimeState *runtime = tstate->interp->runtime;
if (tstate == NULL) {
Py_FatalError("PyThreadState_Delete: NULL tstate");
}
}
-static void
-_PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate)
+void
+PyThreadState_Delete(PyThreadState *tstate)
{
- struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
+ struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
Py_FatalError("PyThreadState_Delete: tstate is still current");
}
{
PyThread_tss_set(&gilstate->autoTSSkey, NULL);
}
- tstate_delete_common(runtime, tstate);
-}
-
-
-void
-PyThreadState_Delete(PyThreadState *tstate)
-{
- _PyThreadState_Delete(&_PyRuntime, tstate);
+ tstate_delete_common(tstate);
}
if (tstate == NULL)
Py_FatalError(
"PyThreadState_DeleteCurrent: no current tstate");
- tstate_delete_common(runtime, tstate);
+ tstate_delete_common(tstate);
if (gilstate->autoInterpreterState &&
PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
{
Py_Initialize/Py_FinalizeEx
*/
void
-_PyGILState_Init(_PyRuntimeState *runtime, PyThreadState *tstate)
+_PyGILState_Init(PyThreadState *tstate)
{
/* must init with valid states */
assert(tstate != NULL);
assert(tstate->interp != NULL);
- struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
+ struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
Py_FatalError("Could not allocate TSS entry");
};
static PyObject*
-make_flags(_PyRuntimeState *runtime, PyThreadState *tstate)
+make_flags(PyThreadState *tstate)
{
- int pos = 0;
- PyObject *seq;
- const PyPreConfig *preconfig = &runtime->preconfig;
- const PyConfig *config = &tstate->interp->config;
+ PyInterpreterState *interp = tstate->interp;
+ const PyPreConfig *preconfig = &interp->runtime->preconfig;
+ const PyConfig *config = &interp->config;
- seq = PyStructSequence_New(&FlagsType);
- if (seq == NULL)
+ PyObject *seq = PyStructSequence_New(&FlagsType);
+ if (seq == NULL) {
return NULL;
+ }
+ int pos = 0;
#define SetFlag(flag) \
PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag))
} while (0)
static PyStatus
-_PySys_InitCore(_PyRuntimeState *runtime, PyThreadState *tstate,
- PyObject *sysdict)
+_PySys_InitCore(PyThreadState *tstate, PyObject *sysdict)
{
PyObject *version_info;
int res;
}
}
/* Set flags to their default values (updated by _PySys_InitMain()) */
- SET_SYS_FROM_STRING("flags", make_flags(runtime, tstate));
+ SET_SYS_FROM_STRING("flags", make_flags(tstate));
#if defined(MS_WINDOWS)
/* getwindowsversion */
int
-_PySys_InitMain(_PyRuntimeState *runtime, PyThreadState *tstate)
+_PySys_InitMain(PyThreadState *tstate)
{
PyObject *sysdict = tstate->interp->sysdict;
const PyConfig *config = &tstate->interp->config;
#undef SET_SYS_FROM_WSTR
/* Set flags to their final values */
- SET_SYS_FROM_STRING_INT_RESULT("flags", make_flags(runtime, tstate));
+ SET_SYS_FROM_STRING_INT_RESULT("flags", make_flags(tstate));
/* prevent user from creating new instances */
FlagsType.tp_init = NULL;
FlagsType.tp_new = NULL;
/* Create sys module without all attributes: _PySys_InitMain() should be called
later to add remaining attributes. */
PyStatus
-_PySys_Create(_PyRuntimeState *runtime, PyThreadState *tstate,
- PyObject **sysmod_p)
+_PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)
{
PyInterpreterState *interp = tstate->interp;
return status;
}
- status = _PySys_InitCore(runtime, tstate, sysdict);
+ status = _PySys_InitCore(tstate, sysdict);
if (_PyStatus_EXCEPTION(status)) {
return status;
}