mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
bpo-36710: Add PyInterpreterState.runtime field (GH-17270)
Add PyInterpreterState.runtime field: 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. Remove "_PyRuntimeState *runtime" parameter from functions already taking a "PyThreadState *tstate" parameter. _PyGC_Init() first parameter becomes "PyThreadState *tstate".
This commit is contained in:
parent
eb1cbbff1c
commit
01b1cc12e7
8 changed files with 81 additions and 94 deletions
|
@ -39,7 +39,6 @@ extern "C" {
|
|||
|
||||
/* Forward declarations */
|
||||
static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
|
||||
static void _PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate);
|
||||
|
||||
|
||||
static PyStatus
|
||||
|
@ -205,6 +204,9 @@ PyInterpreterState_New(void)
|
|||
memset(interp, 0, sizeof(*interp));
|
||||
interp->id_refcount = -1;
|
||||
|
||||
_PyRuntimeState *runtime = &_PyRuntime;
|
||||
interp->runtime = runtime;
|
||||
|
||||
PyConfig_InitPythonConfig(&interp->config);
|
||||
|
||||
interp->eval_frame = _PyEval_EvalFrameDefault;
|
||||
|
@ -216,7 +218,6 @@ PyInterpreterState_New(void)
|
|||
#endif
|
||||
#endif
|
||||
|
||||
_PyRuntimeState *runtime = &_PyRuntime;
|
||||
struct pyinterpreters *interpreters = &runtime->interpreters;
|
||||
|
||||
HEAD_LOCK(runtime);
|
||||
|
@ -250,9 +251,11 @@ PyInterpreterState_New(void)
|
|||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
@ -290,31 +293,25 @@ _PyInterpreterState_Clear(_PyRuntimeState *runtime, PyInterpreterState *interp)
|
|||
// 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) {
|
||||
|
@ -343,13 +340,6 @@ _PyInterpreterState_Delete(_PyRuntimeState *runtime,
|
|||
}
|
||||
|
||||
|
||||
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.
|
||||
|
@ -376,8 +366,8 @@ _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
|
|||
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);
|
||||
}
|
||||
|
@ -552,7 +542,7 @@ threadstate_getframe(PyThreadState *self)
|
|||
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;
|
||||
|
@ -608,7 +598,7 @@ new_threadstate(PyInterpreterState *interp, int init)
|
|||
tstate->id = ++interp->tstate_next_unique_id;
|
||||
|
||||
if (init) {
|
||||
_PyThreadState_Init(runtime, tstate);
|
||||
_PyThreadState_Init(tstate);
|
||||
}
|
||||
|
||||
HEAD_LOCK(runtime);
|
||||
|
@ -635,9 +625,9 @@ _PyThreadState_Prealloc(PyInterpreterState *interp)
|
|||
}
|
||||
|
||||
void
|
||||
_PyThreadState_Init(_PyRuntimeState *runtime, PyThreadState *tstate)
|
||||
_PyThreadState_Init(PyThreadState *tstate)
|
||||
{
|
||||
_PyGILState_NoteThreadState(&runtime->gilstate, tstate);
|
||||
_PyGILState_NoteThreadState(&tstate->interp->runtime->gilstate, tstate);
|
||||
}
|
||||
|
||||
PyObject*
|
||||
|
@ -803,8 +793,9 @@ PyThreadState_Clear(PyThreadState *tstate)
|
|||
|
||||
/* 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");
|
||||
}
|
||||
|
@ -827,10 +818,10 @@ tstate_delete_common(_PyRuntimeState *runtime, PyThreadState *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");
|
||||
}
|
||||
|
@ -839,14 +830,7 @@ _PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate)
|
|||
{
|
||||
PyThread_tss_set(&gilstate->autoTSSkey, NULL);
|
||||
}
|
||||
tstate_delete_common(runtime, tstate);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
PyThreadState_Delete(PyThreadState *tstate)
|
||||
{
|
||||
_PyThreadState_Delete(&_PyRuntime, tstate);
|
||||
tstate_delete_common(tstate);
|
||||
}
|
||||
|
||||
|
||||
|
@ -858,7 +842,7 @@ _PyThreadState_DeleteCurrent(_PyRuntimeState *runtime)
|
|||
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)
|
||||
{
|
||||
|
@ -1134,13 +1118,13 @@ PyThreadState_IsCurrent(PyThreadState *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");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue