Skip to content

Commit 0fd2c30

Browse files
authored
Revert "bpo-36818: Add PyInterpreterState.runtime field. (gh-13129)" (GH-13795)
This reverts commit 396e0a8.
1 parent 9535aff commit 0fd2c30

File tree

11 files changed

+103
-99
lines changed

11 files changed

+103
-99
lines changed

Include/cpython/pystate.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ struct _ts {
110110
* if the thread holds the last reference to the lock, decref'ing the
111111
* lock will delete the lock, and that may trigger arbitrary Python code
112112
* if there's a weakref, with a callback, to the lock. But by this time
113-
* _PyRuntimeState.gilstate.tstate_current is already NULL, so only the
114-
* simplest of C code can be allowed to run (in particular it must not be
115-
* possible to release the GIL).
113+
* _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest
114+
* of C code can be allowed to run (in particular it must not be possible to
115+
* release the GIL).
116116
* So instead of holding the lock directly, the tstate holds a weakref to
117117
* the lock: that's the value of on_delete_data below. Decref'ing a
118118
* weakref is harmless.

Include/internal/pycore_object.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ PyAPI_FUNC(int) _PyDict_CheckConsistency(PyObject *mp, int check_content);
1919
* NB: While the object is tracked by the collector, it must be safe to call the
2020
* ob_traverse method.
2121
*
22-
* Internal note: _PyRuntimeState.gc.generation0->_gc_prev doesn't have
23-
* any bit flags because it's not object header. So we don't use
24-
* _PyGCHead_PREV() and _PyGCHead_SET_PREV() for it to avoid unnecessary
25-
* bitwise operations.
22+
* Internal note: _PyRuntime.gc.generation0->_gc_prev doesn't have any bit flags
23+
* because it's not object header. So we don't use _PyGCHead_PREV() and
24+
* _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
2625
*
2726
* The PyObject_GC_Track() function is the public version of this macro.
2827
*/

Include/internal/pycore_pylifecycle.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ extern PyStatus _PyFaulthandler_Init(int enable);
3939
extern int _PyTraceMalloc_Init(int enable);
4040
extern PyObject * _PyBuiltin_Init(void);
4141
extern PyStatus _PySys_Create(
42+
_PyRuntimeState *runtime,
4243
PyInterpreterState *interp,
4344
PyObject **sysmod_p);
4445
extern PyStatus _PySys_SetPreliminaryStderr(PyObject *sysdict);
45-
extern int _PySys_InitMain(PyInterpreterState *interp);
46+
extern int _PySys_InitMain(
47+
_PyRuntimeState *runtime,
48+
PyInterpreterState *interp);
4649
extern PyStatus _PyImport_Init(PyInterpreterState *interp);
4750
extern PyStatus _PyExc_Init(void);
4851
extern PyStatus _PyErr_Init(void);
@@ -83,7 +86,10 @@ extern void _PyHash_Fini(void);
8386
extern int _PyTraceMalloc_Fini(void);
8487
extern void _PyWarnings_Fini(PyInterpreterState *interp);
8588

86-
extern void _PyGILState_Init(PyThreadState *tstate);
89+
extern void _PyGILState_Init(
90+
_PyRuntimeState *runtime,
91+
PyInterpreterState *interp,
92+
PyThreadState *tstate);
8793
extern void _PyGILState_Fini(_PyRuntimeState *runtime);
8894

8995
PyAPI_FUNC(void) _PyGC_DumpShutdownStats(_PyRuntimeState *runtime);

Include/internal/pycore_pystate.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ extern "C" {
1919
#include "pycore_pymem.h"
2020
#include "pycore_warnings.h"
2121

22-
// forward
23-
struct pyruntimestate;
24-
2522

2623
/* ceval state */
2724

@@ -71,7 +68,6 @@ struct _is {
7168

7269
struct _is *next;
7370
struct _ts *tstate_head;
74-
struct pyruntimestate *runtime;
7571

7672
int64_t id;
7773
int64_t id_refcount;
@@ -300,8 +296,12 @@ PyAPI_FUNC(void) _PyRuntime_Finalize(void);
300296

301297
/* Other */
302298

303-
PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *tstate);
304-
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
299+
PyAPI_FUNC(void) _PyThreadState_Init(
300+
_PyRuntimeState *runtime,
301+
PyThreadState *tstate);
302+
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(
303+
_PyRuntimeState *runtime,
304+
PyThreadState *tstate);
305305

306306
PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap(
307307
struct _gilstate_runtime_state *gilstate,

Misc/NEWS.d/next/Core and Builtins/2019-05-06-14-46-48.bpo-36818.5UDDLj.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.

Modules/_threadmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ t_bootstrap(void *boot_raw)
996996

997997
tstate = boot->tstate;
998998
tstate->thread_id = PyThread_get_thread_ident();
999-
_PyThreadState_Init(tstate);
999+
_PyThreadState_Init(&_PyRuntime, tstate);
10001000
PyEval_AcquireThread(tstate);
10011001
tstate->interp->num_threads++;
10021002
res = PyObject_Call(boot->func, boot->args, boot->keyw);

Python/ceval.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,8 @@ _PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
238238
}
239239

240240
static inline void
241-
exit_thread_if_finalizing(PyThreadState *tstate)
241+
exit_thread_if_finalizing(_PyRuntimeState *runtime, PyThreadState *tstate)
242242
{
243-
_PyRuntimeState *runtime = tstate->interp->runtime;
244243
/* _Py_Finalizing is protected by the GIL */
245244
if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
246245
drop_gil(&runtime->ceval, tstate);
@@ -287,7 +286,7 @@ PyEval_AcquireLock(void)
287286
Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
288287
}
289288
take_gil(ceval, tstate);
290-
exit_thread_if_finalizing(tstate);
289+
exit_thread_if_finalizing(runtime, tstate);
291290
}
292291

293292
void
@@ -308,15 +307,14 @@ PyEval_AcquireThread(PyThreadState *tstate)
308307
if (tstate == NULL) {
309308
Py_FatalError("PyEval_AcquireThread: NULL new thread state");
310309
}
311-
assert(tstate->interp != NULL);
312310

313-
_PyRuntimeState *runtime = tstate->interp->runtime;
311+
_PyRuntimeState *runtime = &_PyRuntime;
314312
struct _ceval_runtime_state *ceval = &runtime->ceval;
315313

316314
/* Check someone has called PyEval_InitThreads() to create the lock */
317315
assert(gil_created(&ceval->gil));
318316
take_gil(ceval, tstate);
319-
exit_thread_if_finalizing(tstate);
317+
exit_thread_if_finalizing(runtime, tstate);
320318
if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
321319
Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
322320
}
@@ -328,9 +326,8 @@ PyEval_ReleaseThread(PyThreadState *tstate)
328326
if (tstate == NULL) {
329327
Py_FatalError("PyEval_ReleaseThread: NULL thread state");
330328
}
331-
assert(tstate->interp != NULL);
332329

333-
_PyRuntimeState *runtime = tstate->interp->runtime;
330+
_PyRuntimeState *runtime = &_PyRuntime;
334331
PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
335332
if (new_tstate != tstate) {
336333
Py_FatalError("PyEval_ReleaseThread: wrong thread state");
@@ -361,7 +358,7 @@ _PyEval_ReInitThreads(_PyRuntimeState *runtime)
361358
}
362359

363360
/* Destroy all threads except the current one */
364-
_PyThreadState_DeleteExcept(current_tstate);
361+
_PyThreadState_DeleteExcept(runtime, current_tstate);
365362
}
366363

367364
/* This function is used to signal that async exceptions are waiting to be
@@ -390,18 +387,17 @@ PyEval_SaveThread(void)
390387
void
391388
PyEval_RestoreThread(PyThreadState *tstate)
392389
{
390+
_PyRuntimeState *runtime = &_PyRuntime;
391+
struct _ceval_runtime_state *ceval = &runtime->ceval;
392+
393393
if (tstate == NULL) {
394394
Py_FatalError("PyEval_RestoreThread: NULL tstate");
395395
}
396-
assert(tstate->interp != NULL);
397-
398-
_PyRuntimeState *runtime = tstate->interp->runtime;
399-
struct _ceval_runtime_state *ceval = &runtime->ceval;
400396
assert(gil_created(&ceval->gil));
401397

402398
int err = errno;
403399
take_gil(ceval, tstate);
404-
exit_thread_if_finalizing(tstate);
400+
exit_thread_if_finalizing(runtime, tstate);
405401
errno = err;
406402

407403
_PyThreadState_Swap(&runtime->gilstate, tstate);
@@ -1250,7 +1246,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
12501246
take_gil(ceval, tstate);
12511247

12521248
/* Check if we should make a quick exit. */
1253-
exit_thread_if_finalizing(tstate);
1249+
exit_thread_if_finalizing(runtime, tstate);
12541250

12551251
if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
12561252
Py_FatalError("ceval: orphan tstate");

Python/import.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,7 @@ PyImport_Cleanup(void)
541541
_PyGC_CollectNoFail();
542542
/* Dump GC stats before it's too late, since it uses the warnings
543543
machinery. */
544-
_PyRuntimeState *runtime = interp->runtime;
545-
_PyGC_DumpShutdownStats(runtime);
544+
_PyGC_DumpShutdownStats(&_PyRuntime);
546545

547546
/* Now, if there are any modules left alive, clear their globals to
548547
minimize potential leaks. All C extension modules actually end

Python/pylifecycle.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
545545
_PyEval_FiniThreads(&runtime->ceval);
546546

547547
/* Auto-thread-state API */
548-
_PyGILState_Init(tstate);
548+
_PyGILState_Init(runtime, interp, tstate);
549549

550550
/* Create the GIL */
551551
PyEval_InitThreads();
@@ -683,7 +683,7 @@ pyinit_config(_PyRuntimeState *runtime,
683683
}
684684

685685
PyObject *sysmod;
686-
status = _PySys_Create(interp, &sysmod);
686+
status = _PySys_Create(runtime, interp, &sysmod);
687687
if (_PyStatus_EXCEPTION(status)) {
688688
return status;
689689
}
@@ -892,9 +892,8 @@ _Py_ReconfigureMainInterpreter(PyInterpreterState *interp)
892892
* non-zero return code.
893893
*/
894894
static PyStatus
895-
pyinit_main(PyInterpreterState *interp)
895+
pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
896896
{
897-
_PyRuntimeState *runtime = interp->runtime;
898897
if (!runtime->core_initialized) {
899898
return _PyStatus_ERR("runtime core not initialized");
900899
}
@@ -920,7 +919,7 @@ pyinit_main(PyInterpreterState *interp)
920919
return _PyStatus_ERR("can't initialize time");
921920
}
922921

923-
if (_PySys_InitMain(interp) < 0) {
922+
if (_PySys_InitMain(runtime, interp) < 0) {
924923
return _PyStatus_ERR("can't finish initializing sys");
925924
}
926925

@@ -1000,7 +999,7 @@ _Py_InitializeMain(void)
1000999
_PyRuntimeState *runtime = &_PyRuntime;
10011000
PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
10021001

1003-
return pyinit_main(interp);
1002+
return pyinit_main(runtime, interp);
10041003
}
10051004

10061005

@@ -1027,7 +1026,7 @@ Py_InitializeFromConfig(const PyConfig *config)
10271026
config = &interp->config;
10281027

10291028
if (config->_init_main) {
1030-
status = pyinit_main(interp);
1029+
status = pyinit_main(runtime, interp);
10311030
if (_PyStatus_EXCEPTION(status)) {
10321031
return status;
10331032
}
@@ -1457,7 +1456,7 @@ new_interpreter(PyThreadState **tstate_p)
14571456
}
14581457
Py_INCREF(interp->sysdict);
14591458
PyDict_SetItemString(interp->sysdict, "modules", modules);
1460-
if (_PySys_InitMain(interp) < 0) {
1459+
if (_PySys_InitMain(runtime, interp) < 0) {
14611460
return _PyStatus_ERR("can't finish initializing sys");
14621461
}
14631462
}

0 commit comments

Comments
 (0)