Skip to content

Commit 5c2a483

Browse files
Do not pass PyRuntimeState if not needed.
1 parent 701fe2f commit 5c2a483

File tree

7 files changed

+59
-82
lines changed

7 files changed

+59
-82
lines changed

Include/internal/pycore_pylifecycle.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,10 @@ 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,
4342
PyInterpreterState *interp,
4443
PyObject **sysmod_p);
4544
extern PyStatus _PySys_SetPreliminaryStderr(PyObject *sysdict);
46-
extern int _PySys_InitMain(
47-
_PyRuntimeState *runtime,
48-
PyInterpreterState *interp);
45+
extern int _PySys_InitMain(PyInterpreterState *interp);
4946
extern PyStatus _PyImport_Init(PyInterpreterState *interp);
5047
extern PyStatus _PyExc_Init(void);
5148
extern PyStatus _PyErr_Init(void);
@@ -86,10 +83,7 @@ extern void _PyHash_Fini(void);
8683
extern int _PyTraceMalloc_Fini(void);
8784
extern void _PyWarnings_Fini(PyInterpreterState *interp);
8885

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

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

Include/internal/pycore_pystate.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,8 @@ PyAPI_FUNC(void) _PyRuntime_Finalize(void);
300300

301301
/* Other */
302302

303-
PyAPI_FUNC(void) _PyThreadState_Init(
304-
_PyRuntimeState *runtime,
305-
PyThreadState *tstate);
306-
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(
307-
_PyRuntimeState *runtime,
308-
PyThreadState *tstate);
303+
PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *tstate);
304+
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
309305

310306
PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap(
311307
struct _gilstate_runtime_state *gilstate,

Modules/_threadmodule.c

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

997997
tstate = boot->tstate;
998998
tstate->thread_id = PyThread_get_thread_ident();
999-
_PyRuntimeState *runtime = tstate->interp->runtime;
1000-
_PyThreadState_Init(runtime, tstate);
999+
_PyThreadState_Init(tstate);
10011000
PyEval_AcquireThread(tstate);
10021001
tstate->interp->num_threads++;
10031002
res = PyObject_Call(boot->func, boot->args, boot->keyw);

Python/ceval.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@ _PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
217217
}
218218

219219
static inline void
220-
exit_thread_if_finalizing(_PyRuntimeState *runtime, PyThreadState *tstate)
220+
exit_thread_if_finalizing(PyThreadState *tstate)
221221
{
222+
_PyRuntimeState *runtime = tstate->interp->runtime;
222223
/* _Py_Finalizing is protected by the GIL */
223224
if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
224225
drop_gil(&runtime->ceval, tstate);
@@ -236,7 +237,7 @@ PyEval_AcquireLock(void)
236237
Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
237238
}
238239
take_gil(ceval, tstate);
239-
exit_thread_if_finalizing(runtime, tstate);
240+
exit_thread_if_finalizing(tstate);
240241
}
241242

242243
void
@@ -265,7 +266,7 @@ PyEval_AcquireThread(PyThreadState *tstate)
265266
/* Check someone has called PyEval_InitThreads() to create the lock */
266267
assert(gil_created(&ceval->gil));
267268
take_gil(ceval, tstate);
268-
exit_thread_if_finalizing(runtime, tstate);
269+
exit_thread_if_finalizing(tstate);
269270
if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
270271
Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
271272
}
@@ -310,7 +311,7 @@ _PyEval_ReInitThreads(_PyRuntimeState *runtime)
310311
}
311312

312313
/* Destroy all threads except the current one */
313-
_PyThreadState_DeleteExcept(runtime, current_tstate);
314+
_PyThreadState_DeleteExcept(current_tstate);
314315
}
315316

316317
/* This function is used to signal that async exceptions are waiting to be
@@ -350,7 +351,7 @@ PyEval_RestoreThread(PyThreadState *tstate)
350351

351352
int err = errno;
352353
take_gil(ceval, tstate);
353-
exit_thread_if_finalizing(runtime, tstate);
354+
exit_thread_if_finalizing(tstate);
354355
errno = err;
355356

356357
_PyThreadState_Swap(&runtime->gilstate, tstate);
@@ -1144,7 +1145,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
11441145
take_gil(ceval, tstate);
11451146

11461147
/* Check if we should make a quick exit. */
1147-
exit_thread_if_finalizing(runtime, tstate);
1148+
exit_thread_if_finalizing(tstate);
11481149

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

Python/pylifecycle.c

Lines changed: 8 additions & 7 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(runtime, interp, tstate);
548+
_PyGILState_Init(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(runtime, interp, &sysmod);
686+
status = _PySys_Create(interp, &sysmod);
687687
if (_PyStatus_EXCEPTION(status)) {
688688
return status;
689689
}
@@ -892,8 +892,9 @@ _Py_ReconfigureMainInterpreter(PyInterpreterState *interp)
892892
* non-zero return code.
893893
*/
894894
static PyStatus
895-
pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
895+
pyinit_main(PyInterpreterState *interp)
896896
{
897+
_PyRuntimeState *runtime = interp->runtime;
897898
if (!runtime->core_initialized) {
898899
return _PyStatus_ERR("runtime core not initialized");
899900
}
@@ -919,7 +920,7 @@ pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
919920
return _PyStatus_ERR("can't initialize time");
920921
}
921922

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

@@ -999,7 +1000,7 @@ _Py_InitializeMain(void)
9991000
_PyRuntimeState *runtime = &_PyRuntime;
10001001
PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
10011002

1002-
return pyinit_main(runtime, interp);
1003+
return pyinit_main(interp);
10031004
}
10041005

10051006

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

10281029
if (config->_init_main) {
1029-
status = pyinit_main(runtime, interp);
1030+
status = pyinit_main(interp);
10301031
if (_PyStatus_EXCEPTION(status)) {
10311032
return status;
10321033
}
@@ -1453,7 +1454,7 @@ new_interpreter(PyThreadState **tstate_p)
14531454
}
14541455
Py_INCREF(interp->sysdict);
14551456
PyDict_SetItemString(interp->sysdict, "modules", modules);
1456-
if (_PySys_InitMain(runtime, interp) < 0) {
1457+
if (_PySys_InitMain(interp) < 0) {
14571458
return _PyStatus_ERR("can't finish initializing sys");
14581459
}
14591460
}

Python/pystate.c

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ extern "C" {
3939

4040
/* Forward declarations */
4141
static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
42-
static void _PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate);
4342

4443

4544
static PyStatus
@@ -261,9 +260,11 @@ PyInterpreterState_New(void)
261260
}
262261

263262

264-
static void
265-
_PyInterpreterState_Clear(_PyRuntimeState *runtime, PyInterpreterState *interp)
263+
void
264+
PyInterpreterState_Clear(PyInterpreterState *interp)
266265
{
266+
_PyRuntimeState *runtime = interp->runtime;
267+
267268
if (PySys_Audit("cpython.PyInterpreterState_Clear", NULL) < 0) {
268269
PyErr_Clear();
269270
}
@@ -301,31 +302,25 @@ _PyInterpreterState_Clear(_PyRuntimeState *runtime, PyInterpreterState *interp)
301302
// objects have been cleaned up at the point.
302303
}
303304

304-
void
305-
PyInterpreterState_Clear(PyInterpreterState *interp)
306-
{
307-
_PyInterpreterState_Clear(&_PyRuntime, interp);
308-
}
309-
310305

311306
static void
312-
zapthreads(_PyRuntimeState *runtime, PyInterpreterState *interp)
307+
zapthreads(PyInterpreterState *interp)
313308
{
314-
PyThreadState *p;
309+
PyThreadState *ts;
315310
/* No need to lock the mutex here because this should only happen
316311
when the threads are all really dead (XXX famous last words). */
317-
while ((p = interp->tstate_head) != NULL) {
318-
_PyThreadState_Delete(runtime, p);
312+
while ((ts = interp->tstate_head) != NULL) {
313+
PyThreadState_Delete(ts);
319314
}
320315
}
321316

322317

323-
static void
324-
_PyInterpreterState_Delete(_PyRuntimeState *runtime,
325-
PyInterpreterState *interp)
318+
void
319+
PyInterpreterState_Delete(PyInterpreterState *interp)
326320
{
321+
_PyRuntimeState *runtime = interp->runtime;
327322
struct pyinterpreters *interpreters = &runtime->interpreters;
328-
zapthreads(runtime, interp);
323+
zapthreads(interp);
329324
HEAD_LOCK(runtime);
330325
PyInterpreterState **p;
331326
for (p = &interpreters->head; ; p = &(*p)->next) {
@@ -354,13 +349,6 @@ _PyInterpreterState_Delete(_PyRuntimeState *runtime,
354349
}
355350

356351

357-
void
358-
PyInterpreterState_Delete(PyInterpreterState *interp)
359-
{
360-
_PyInterpreterState_Delete(&_PyRuntime, interp);
361-
}
362-
363-
364352
/*
365353
* Delete all interpreter states except the main interpreter. If there
366354
* is a current interpreter state, it *must* be the main interpreter.
@@ -387,8 +375,8 @@ _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
387375
continue;
388376
}
389377

390-
_PyInterpreterState_Clear(runtime, interp); // XXX must activate?
391-
zapthreads(runtime, interp);
378+
PyInterpreterState_Clear(interp); // XXX must activate?
379+
zapthreads(interp);
392380
if (interp->id_mutex != NULL) {
393381
PyThread_free_lock(interp->id_mutex);
394382
}
@@ -620,7 +608,7 @@ new_threadstate(PyInterpreterState *interp, int init)
620608
tstate->id = ++interp->tstate_next_unique_id;
621609

622610
if (init) {
623-
_PyThreadState_Init(runtime, tstate);
611+
_PyThreadState_Init(tstate);
624612
}
625613

626614
HEAD_LOCK(runtime);
@@ -647,8 +635,9 @@ _PyThreadState_Prealloc(PyInterpreterState *interp)
647635
}
648636

649637
void
650-
_PyThreadState_Init(_PyRuntimeState *runtime, PyThreadState *tstate)
638+
_PyThreadState_Init(PyThreadState *tstate)
651639
{
640+
_PyRuntimeState *runtime = tstate->interp->runtime;
652641
_PyGILState_NoteThreadState(&runtime->gilstate, tstate);
653642
}
654643

@@ -813,7 +802,7 @@ PyThreadState_Clear(PyThreadState *tstate)
813802

814803
/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
815804
static void
816-
tstate_delete_common(_PyRuntimeState *runtime, PyThreadState *tstate)
805+
tstate_delete_common(PyThreadState *tstate)
817806
{
818807
if (tstate == NULL) {
819808
Py_FatalError("PyThreadState_Delete: NULL tstate");
@@ -822,6 +811,7 @@ tstate_delete_common(_PyRuntimeState *runtime, PyThreadState *tstate)
822811
if (interp == NULL) {
823812
Py_FatalError("PyThreadState_Delete: NULL interp");
824813
}
814+
_PyRuntimeState *runtime = interp->runtime;
825815
HEAD_LOCK(runtime);
826816
if (tstate->prev)
827817
tstate->prev->next = tstate->next;
@@ -837,9 +827,10 @@ tstate_delete_common(_PyRuntimeState *runtime, PyThreadState *tstate)
837827
}
838828

839829

840-
static void
841-
_PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate)
830+
void
831+
PyThreadState_Delete(PyThreadState *tstate)
842832
{
833+
_PyRuntimeState *runtime = tstate->interp->runtime;
843834
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
844835
if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
845836
Py_FatalError("PyThreadState_Delete: tstate is still current");
@@ -849,14 +840,7 @@ _PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate)
849840
{
850841
PyThread_tss_set(&gilstate->autoTSSkey, NULL);
851842
}
852-
tstate_delete_common(runtime, tstate);
853-
}
854-
855-
856-
void
857-
PyThreadState_Delete(PyThreadState *tstate)
858-
{
859-
_PyThreadState_Delete(&_PyRuntime, tstate);
843+
tstate_delete_common(tstate);
860844
}
861845

862846

@@ -868,7 +852,7 @@ _PyThreadState_DeleteCurrent(_PyRuntimeState *runtime)
868852
if (tstate == NULL)
869853
Py_FatalError(
870854
"PyThreadState_DeleteCurrent: no current tstate");
871-
tstate_delete_common(runtime, tstate);
855+
tstate_delete_common(tstate);
872856
if (gilstate->autoInterpreterState &&
873857
PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
874858
{
@@ -893,9 +877,10 @@ PyThreadState_DeleteCurrent()
893877
* be kept in those other interpreteres.
894878
*/
895879
void
896-
_PyThreadState_DeleteExcept(_PyRuntimeState *runtime, PyThreadState *tstate)
880+
_PyThreadState_DeleteExcept(PyThreadState *tstate)
897881
{
898882
PyInterpreterState *interp = tstate->interp;
883+
_PyRuntimeState *runtime = interp->runtime;
899884
PyThreadState *p, *next, *garbage;
900885
HEAD_LOCK(runtime);
901886
/* Remove all thread states, except tstate, from the linked list of
@@ -1145,12 +1130,14 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
11451130
Py_Initialize/Py_FinalizeEx
11461131
*/
11471132
void
1148-
_PyGILState_Init(_PyRuntimeState *runtime,
1149-
PyInterpreterState *interp, PyThreadState *tstate)
1133+
_PyGILState_Init(PyThreadState *tstate)
11501134
{
11511135
/* must init with valid states */
1152-
assert(interp != NULL);
11531136
assert(tstate != NULL);
1137+
PyInterpreterState *interp = tstate->interp;
1138+
assert(interp != NULL);
1139+
_PyRuntimeState *runtime = interp->runtime;
1140+
assert(runtime != NULL);
11541141

11551142
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
11561143

0 commit comments

Comments
 (0)