Revert "bpo-36818: Add PyInterpreterState.runtime field. (gh-13129)" (GH-13795)

This reverts commit 396e0a8d9d.
This commit is contained in:
Victor Stinner 2019-06-04 03:15:09 +02:00 committed by GitHub
parent 9535aff942
commit 0fd2c300c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 103 additions and 99 deletions

View file

@ -39,6 +39,7 @@ extern "C" {
/* Forward declarations */
static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
static void _PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate);
static PyStatus
@ -191,8 +192,6 @@ _PyInterpreterState_Enable(_PyRuntimeState *runtime)
PyInterpreterState *
PyInterpreterState_New(void)
{
_PyRuntimeState *runtime = &_PyRuntime;
if (PySys_Audit("cpython.PyInterpreterState_New", NULL) < 0) {
return NULL;
}
@ -203,9 +202,6 @@ PyInterpreterState_New(void)
}
memset(interp, 0, sizeof(*interp));
interp->runtime = runtime;
interp->id_refcount = -1;
interp->check_interval = 100;
@ -227,6 +223,7 @@ PyInterpreterState_New(void)
#endif
#endif
_PyRuntimeState *runtime = &_PyRuntime;
struct pyinterpreters *interpreters = &runtime->interpreters;
HEAD_LOCK(runtime);
@ -260,11 +257,9 @@ PyInterpreterState_New(void)
}
void
PyInterpreterState_Clear(PyInterpreterState *interp)
static void
_PyInterpreterState_Clear(_PyRuntimeState *runtime, PyInterpreterState *interp)
{
_PyRuntimeState *runtime = interp->runtime;
if (PySys_Audit("cpython.PyInterpreterState_Clear", NULL) < 0) {
PyErr_Clear();
}
@ -302,25 +297,31 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
// objects have been cleaned up at the point.
}
void
PyInterpreterState_Clear(PyInterpreterState *interp)
{
_PyInterpreterState_Clear(&_PyRuntime, interp);
}
static void
zapthreads(PyInterpreterState *interp)
zapthreads(_PyRuntimeState *runtime, PyInterpreterState *interp)
{
PyThreadState *ts;
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 ((ts = interp->tstate_head) != NULL) {
PyThreadState_Delete(ts);
while ((p = interp->tstate_head) != NULL) {
_PyThreadState_Delete(runtime, p);
}
}
void
PyInterpreterState_Delete(PyInterpreterState *interp)
static void
_PyInterpreterState_Delete(_PyRuntimeState *runtime,
PyInterpreterState *interp)
{
_PyRuntimeState *runtime = interp->runtime;
struct pyinterpreters *interpreters = &runtime->interpreters;
zapthreads(interp);
zapthreads(runtime, interp);
HEAD_LOCK(runtime);
PyInterpreterState **p;
for (p = &interpreters->head; ; p = &(*p)->next) {
@ -349,6 +350,13 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
}
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.
@ -375,8 +383,8 @@ _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
continue;
}
PyInterpreterState_Clear(interp); // XXX must activate?
zapthreads(interp);
_PyInterpreterState_Clear(runtime, interp); // XXX must activate?
zapthreads(runtime, interp);
if (interp->id_mutex != NULL) {
PyThread_free_lock(interp->id_mutex);
}
@ -489,8 +497,7 @@ _PyInterpreterState_IDDecref(PyInterpreterState *interp)
if (interp->id_mutex == NULL) {
return;
}
_PyRuntimeState *runtime = interp->runtime;
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
assert(interp->id_refcount != 0);
interp->id_refcount -= 1;
@ -552,7 +559,7 @@ threadstate_getframe(PyThreadState *self)
static PyThreadState *
new_threadstate(PyInterpreterState *interp, int init)
{
_PyRuntimeState *runtime = interp->runtime;
_PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
if (tstate == NULL) {
return NULL;
@ -608,7 +615,7 @@ new_threadstate(PyInterpreterState *interp, int init)
tstate->id = ++interp->tstate_next_unique_id;
if (init) {
_PyThreadState_Init(tstate);
_PyThreadState_Init(runtime, tstate);
}
HEAD_LOCK(runtime);
@ -635,9 +642,8 @@ _PyThreadState_Prealloc(PyInterpreterState *interp)
}
void
_PyThreadState_Init(PyThreadState *tstate)
_PyThreadState_Init(_PyRuntimeState *runtime, PyThreadState *tstate)
{
_PyRuntimeState *runtime = tstate->interp->runtime;
_PyGILState_NoteThreadState(&runtime->gilstate, tstate);
}
@ -802,7 +808,7 @@ PyThreadState_Clear(PyThreadState *tstate)
/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
static void
tstate_delete_common(PyThreadState *tstate)
tstate_delete_common(_PyRuntimeState *runtime, PyThreadState *tstate)
{
if (tstate == NULL) {
Py_FatalError("PyThreadState_Delete: NULL tstate");
@ -811,7 +817,6 @@ tstate_delete_common(PyThreadState *tstate)
if (interp == NULL) {
Py_FatalError("PyThreadState_Delete: NULL interp");
}
_PyRuntimeState *runtime = interp->runtime;
HEAD_LOCK(runtime);
if (tstate->prev)
tstate->prev->next = tstate->next;
@ -827,10 +832,9 @@ tstate_delete_common(PyThreadState *tstate)
}
void
PyThreadState_Delete(PyThreadState *tstate)
static void
_PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate)
{
_PyRuntimeState *runtime = tstate->interp->runtime;
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
Py_FatalError("PyThreadState_Delete: tstate is still current");
@ -840,7 +844,14 @@ PyThreadState_Delete(PyThreadState *tstate)
{
PyThread_tss_set(&gilstate->autoTSSkey, NULL);
}
tstate_delete_common(tstate);
tstate_delete_common(runtime, tstate);
}
void
PyThreadState_Delete(PyThreadState *tstate)
{
_PyThreadState_Delete(&_PyRuntime, tstate);
}
@ -852,7 +863,7 @@ _PyThreadState_DeleteCurrent(_PyRuntimeState *runtime)
if (tstate == NULL)
Py_FatalError(
"PyThreadState_DeleteCurrent: no current tstate");
tstate_delete_common(tstate);
tstate_delete_common(runtime, tstate);
if (gilstate->autoInterpreterState &&
PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
{
@ -877,10 +888,9 @@ PyThreadState_DeleteCurrent()
* be kept in those other interpreteres.
*/
void
_PyThreadState_DeleteExcept(PyThreadState *tstate)
_PyThreadState_DeleteExcept(_PyRuntimeState *runtime, PyThreadState *tstate)
{
PyInterpreterState *interp = tstate->interp;
_PyRuntimeState *runtime = interp->runtime;
PyThreadState *p, *next, *garbage;
HEAD_LOCK(runtime);
/* Remove all thread states, except tstate, from the linked list of
@ -1119,9 +1129,8 @@ _PyThread_CurrentFrames(void)
static int
PyThreadState_IsCurrent(PyThreadState *tstate)
{
_PyRuntimeState *runtime = tstate->interp->runtime;
/* Must be the tstate for this thread */
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
}
@ -1130,14 +1139,12 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
Py_Initialize/Py_FinalizeEx
*/
void
_PyGILState_Init(PyThreadState *tstate)
_PyGILState_Init(_PyRuntimeState *runtime,
PyInterpreterState *interp, PyThreadState *tstate)
{
/* must init with valid states */
assert(tstate != NULL);
PyInterpreterState *interp = tstate->interp;
assert(interp != NULL);
_PyRuntimeState *runtime = interp->runtime;
assert(runtime != NULL);
assert(tstate != NULL);
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;