Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 330db40

Browse files
author
Anselm Kruis
committed
Merge branch main into main-slp
2 parents 90cd86d + 396e0a8 commit 330db40

File tree

11 files changed

+100
-103
lines changed

11 files changed

+100
-103
lines changed

‎Include/cpython/pystate.h

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

‎Include/internal/pycore_object.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ 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: _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.
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.
2526
*
2627
* The PyObject_GC_Track() function is the public version of this macro.
2728
*/

‎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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ extern "C" {
2323
#include "pycore_slp_pystate.h"
2424
#endif
2525

26+
// forward
27+
struct pyruntimestate;
28+
29+
2630
/* ceval state */
2731

2832
struct _pending_calls {
@@ -71,6 +75,7 @@ struct _is {
7175

7276
struct _is *next;
7377
struct _ts *tstate_head;
78+
struct pyruntimestate *runtime;
7479

7580
int64_t id;
7681
int64_t id_refcount;
@@ -305,12 +310,8 @@ PyAPI_FUNC(void) _PyRuntime_Finalize(void);
305310

306311
/* Other */
307312

308-
PyAPI_FUNC(void) _PyThreadState_Init(
309-
_PyRuntimeState *runtime,
310-
PyThreadState *tstate);
311-
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(
312-
_PyRuntimeState *runtime,
313-
PyThreadState *tstate);
313+
PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *tstate);
314+
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
314315

315316
PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap(
316317
struct _gilstate_runtime_state *gilstate,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add PyInterpreterState.runtime (and use it).

‎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(&_PyRuntime, tstate);
999+
_PyThreadState_Init(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: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,9 @@ _PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
218218
}
219219

220220
static inline void
221-
exit_thread_if_finalizing(_PyRuntimeState*runtime, PyThreadState *tstate)
221+
exit_thread_if_finalizing(PyThreadState *tstate)
222222
{
223+
_PyRuntimeState *runtime = tstate->interp->runtime;
223224
/* _Py_Finalizing is protected by the GIL */
224225
if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
225226
drop_gil(&runtime->ceval, tstate);
@@ -237,7 +238,7 @@ PyEval_AcquireLock(void)
237238
Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
238239
}
239240
take_gil(ceval, tstate);
240-
exit_thread_if_finalizing(runtime, tstate);
241+
exit_thread_if_finalizing(tstate);
241242
}
242243

243244
void
@@ -258,14 +259,15 @@ PyEval_AcquireThread(PyThreadState *tstate)
258259
if (tstate == NULL) {
259260
Py_FatalError("PyEval_AcquireThread: NULL new thread state");
260261
}
262+
assert(tstate->interp != NULL);
261263

262-
_PyRuntimeState *runtime = &_PyRuntime;
264+
_PyRuntimeState *runtime = tstate->interp->runtime;
263265
struct _ceval_runtime_state *ceval = &runtime->ceval;
264266

265267
/* Check someone has called PyEval_InitThreads() to create the lock */
266268
assert(gil_created(&ceval->gil));
267269
take_gil(ceval, tstate);
268-
exit_thread_if_finalizing(runtime, tstate);
270+
exit_thread_if_finalizing(tstate);
269271
if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
270272
Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
271273
}
@@ -277,8 +279,9 @@ PyEval_ReleaseThread(PyThreadState *tstate)
277279
if (tstate == NULL) {
278280
Py_FatalError("PyEval_ReleaseThread: NULL thread state");
279281
}
282+
assert(tstate->interp != NULL);
280283

281-
_PyRuntimeState *runtime = &_PyRuntime;
284+
_PyRuntimeState *runtime = tstate->interp->runtime;
282285
PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
283286
if (new_tstate != tstate) {
284287
Py_FatalError("PyEval_ReleaseThread: wrong thread state");
@@ -309,7 +312,7 @@ _PyEval_ReInitThreads(_PyRuntimeState *runtime)
309312
}
310313

311314
/* Destroy all threads except the current one */
312-
_PyThreadState_DeleteExcept(runtime, current_tstate);
315+
_PyThreadState_DeleteExcept(current_tstate);
313316
}
314317

315318
/* This function is used to signal that async exceptions are waiting to be
@@ -338,17 +341,18 @@ PyEval_SaveThread(void)
338341
void
339342
PyEval_RestoreThread(PyThreadState *tstate)
340343
{
341-
_PyRuntimeState *runtime = &_PyRuntime;
342-
struct _ceval_runtime_state *ceval = &runtime->ceval;
343-
344344
if (tstate == NULL) {
345345
Py_FatalError("PyEval_RestoreThread: NULL tstate");
346346
}
347+
assert(tstate->interp != NULL);
348+
349+
_PyRuntimeState *runtime = tstate->interp->runtime;
350+
struct _ceval_runtime_state *ceval = &runtime->ceval;
347351
assert(gil_created(&ceval->gil));
348352

349353
int err = errno;
350354
take_gil(ceval, tstate);
351-
exit_thread_if_finalizing(runtime, tstate);
355+
exit_thread_if_finalizing(tstate);
352356
errno = err;
353357

354358
_PyThreadState_Swap(&runtime->gilstate, tstate);
@@ -1322,7 +1326,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
13221326
take_gil(ceval, tstate);
13231327

13241328
/* Check if we should make a quick exit. */
1325-
exit_thread_if_finalizing(runtime, tstate);
1329+
exit_thread_if_finalizing(tstate);
13261330

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

‎Python/import.c

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

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

‎Python/pylifecycle.c

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

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

552552
/* Create the GIL */
553553
PyEval_InitThreads();
@@ -690,7 +690,7 @@ pyinit_config(_PyRuntimeState *runtime,
690690
}
691691

692692
PyObject *sysmod;
693-
status = _PySys_Create(runtime, interp, &sysmod);
693+
status = _PySys_Create(interp, &sysmod);
694694
if (_PyStatus_EXCEPTION(status)) {
695695
return status;
696696
}
@@ -899,8 +899,9 @@ _Py_ReconfigureMainInterpreter(PyInterpreterState *interp)
899899
* non-zero return code.
900900
*/
901901
static PyStatus
902-
pyinit_main(_PyRuntimeState*runtime, PyInterpreterState *interp)
902+
pyinit_main(PyInterpreterState *interp)
903903
{
904+
_PyRuntimeState *runtime = interp->runtime;
904905
if (!runtime->core_initialized) {
905906
return _PyStatus_ERR("runtime core not initialized");
906907
}
@@ -926,7 +927,7 @@ pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
926927
return _PyStatus_ERR("can't initialize time");
927928
}
928929

929-
if (_PySys_InitMain(runtime, interp) < 0) {
930+
if (_PySys_InitMain(interp) < 0) {
930931
return _PyStatus_ERR("can't finish initializing sys");
931932
}
932933

@@ -1006,7 +1007,7 @@ _Py_InitializeMain(void)
10061007
_PyRuntimeState *runtime = &_PyRuntime;
10071008
PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
10081009

1009-
return pyinit_main(runtime, interp);
1010+
return pyinit_main(interp);
10101011
}
10111012

10121013

@@ -1033,7 +1034,7 @@ Py_InitializeFromConfig(const PyConfig *config)
10331034
config = &interp->config;
10341035

10351036
if (config->_init_main) {
1036-
status = pyinit_main(runtime, interp);
1037+
status = pyinit_main(interp);
10371038
if (_PyStatus_EXCEPTION(status)) {
10381039
return status;
10391040
}
@@ -1466,7 +1467,7 @@ new_interpreter(PyThreadState **tstate_p)
14661467
}
14671468
Py_INCREF(interp->sysdict);
14681469
PyDict_SetItemString(interp->sysdict, "modules", modules);
1469-
if (_PySys_InitMain(runtime, interp) < 0) {
1470+
if (_PySys_InitMain(interp) < 0) {
14701471
return _PyStatus_ERR("can't finish initializing sys");
14711472
}
14721473
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /