Skip to content

Commit 701fe2f

Browse files
Use PyInterpreterState.runtime.
1 parent 1a3e836 commit 701fe2f

File tree

7 files changed

+35
-24
lines changed

7 files changed

+35
-24
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-
* _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).
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).
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: 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
*/

Modules/_threadmodule.c

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

997997
tstate = boot->tstate;
998998
tstate->thread_id = PyThread_get_thread_ident();
999-
_PyThreadState_Init(&_PyRuntime, tstate);
999+
_PyRuntimeState *runtime = tstate->interp->runtime;
1000+
_PyThreadState_Init(runtime, tstate);
10001001
PyEval_AcquireThread(tstate);
10011002
tstate->interp->num_threads++;
10021003
res = PyObject_Call(boot->func, boot->args, boot->keyw);

Python/ceval.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,9 @@ PyEval_AcquireThread(PyThreadState *tstate)
257257
if (tstate == NULL) {
258258
Py_FatalError("PyEval_AcquireThread: NULL new thread state");
259259
}
260+
assert(tstate->interp != NULL);
260261

261-
_PyRuntimeState *runtime = &_PyRuntime;
262+
_PyRuntimeState *runtime = tstate->interp->runtime;
262263
struct _ceval_runtime_state *ceval = &runtime->ceval;
263264

264265
/* Check someone has called PyEval_InitThreads() to create the lock */
@@ -276,8 +277,9 @@ PyEval_ReleaseThread(PyThreadState *tstate)
276277
if (tstate == NULL) {
277278
Py_FatalError("PyEval_ReleaseThread: NULL thread state");
278279
}
280+
assert(tstate->interp != NULL);
279281

280-
_PyRuntimeState *runtime = &_PyRuntime;
282+
_PyRuntimeState *runtime = tstate->interp->runtime;
281283
PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
282284
if (new_tstate != tstate) {
283285
Py_FatalError("PyEval_ReleaseThread: wrong thread state");
@@ -337,12 +339,13 @@ PyEval_SaveThread(void)
337339
void
338340
PyEval_RestoreThread(PyThreadState *tstate)
339341
{
340-
_PyRuntimeState *runtime = &_PyRuntime;
341-
struct _ceval_runtime_state *ceval = &runtime->ceval;
342-
343342
if (tstate == NULL) {
344343
Py_FatalError("PyEval_RestoreThread: NULL tstate");
345344
}
345+
assert(tstate->interp != NULL);
346+
347+
_PyRuntimeState *runtime = tstate->interp->runtime;
348+
struct _ceval_runtime_state *ceval = &runtime->ceval;
346349
assert(gil_created(&ceval->gil));
347350

348351
int err = errno;

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/pystate.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ _PyInterpreterState_Enable(_PyRuntimeState *runtime)
192192
PyInterpreterState *
193193
PyInterpreterState_New(void)
194194
{
195+
_PyRuntimeState *runtime = &_PyRuntime;
196+
195197
if (PySys_Audit("cpython.PyInterpreterState_New", NULL) < 0) {
196198
return NULL;
197199
}
@@ -203,7 +205,7 @@ PyInterpreterState_New(void)
203205

204206
memset(interp, 0, sizeof(*interp));
205207

206-
interp->runtime = &_PyRuntime;
208+
interp->runtime = runtime;
207209

208210
interp->id_refcount = -1;
209211
interp->check_interval = 100;
@@ -226,7 +228,6 @@ PyInterpreterState_New(void)
226228
#endif
227229
#endif
228230

229-
_PyRuntimeState *runtime = &_PyRuntime;
230231
struct pyinterpreters *interpreters = &runtime->interpreters;
231232

232233
HEAD_LOCK(runtime);
@@ -500,7 +501,8 @@ _PyInterpreterState_IDDecref(PyInterpreterState *interp)
500501
if (interp->id_mutex == NULL) {
501502
return;
502503
}
503-
struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
504+
_PyRuntimeState *runtime = interp->runtime;
505+
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
504506
PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
505507
assert(interp->id_refcount != 0);
506508
interp->id_refcount -= 1;
@@ -562,7 +564,7 @@ threadstate_getframe(PyThreadState *self)
562564
static PyThreadState *
563565
new_threadstate(PyInterpreterState *interp, int init)
564566
{
565-
_PyRuntimeState *runtime = &_PyRuntime;
567+
_PyRuntimeState *runtime = interp->runtime;
566568
PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
567569
if (tstate == NULL) {
568570
return NULL;
@@ -1132,8 +1134,9 @@ _PyThread_CurrentFrames(void)
11321134
static int
11331135
PyThreadState_IsCurrent(PyThreadState *tstate)
11341136
{
1137+
_PyRuntimeState *runtime = tstate->interp->runtime;
11351138
/* Must be the tstate for this thread */
1136-
struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1139+
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
11371140
assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
11381141
return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
11391142
}

Python/sysmodule.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ should_audit(void)
120120
if (!ts) {
121121
return 0;
122122
}
123-
PyInterpreterState *is = ts ? ts->interp : NULL;
124-
return _PyRuntime.audit_hook_head
123+
PyInterpreterState *is = ts->interp;
124+
_PyRuntimeState *runtime = is->runtime;
125+
return runtime->audit_hook_head
125126
|| (is && is->audit_hooks)
126127
|| PyDTrace_AUDIT_ENABLED();
127128
}
@@ -280,8 +281,8 @@ void _PySys_ClearAuditHooks(void) {
280281
PySys_Audit("cpython._PySys_ClearAuditHooks", NULL);
281282
PyErr_Clear();
282283

283-
_Py_AuditHookEntry *e = _PyRuntime.audit_hook_head, *n;
284-
_PyRuntime.audit_hook_head = NULL;
284+
_Py_AuditHookEntry *e = runtime->audit_hook_head, *n;
285+
runtime->audit_hook_head = NULL;
285286
while (e) {
286287
n = e->next;
287288
PyMem_RawFree(e);
@@ -292,6 +293,7 @@ void _PySys_ClearAuditHooks(void) {
292293
int
293294
PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
294295
{
296+
_PyRuntimeState *runtime = &_PyRuntime;
295297
/* Invoke existing audit hooks to allow them an opportunity to abort. */
296298
/* Cannot invoke hooks until we are initialized */
297299
if (Py_IsInitialized()) {
@@ -305,10 +307,10 @@ PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
305307
}
306308
}
307309

308-
_Py_AuditHookEntry *e = _PyRuntime.audit_hook_head;
310+
_Py_AuditHookEntry *e = runtime->audit_hook_head;
309311
if (!e) {
310312
e = (_Py_AuditHookEntry*)PyMem_RawMalloc(sizeof(_Py_AuditHookEntry));
311-
_PyRuntime.audit_hook_head = e;
313+
runtime->audit_hook_head = e;
312314
} else {
313315
while (e->next)
314316
e = e->next;

0 commit comments

Comments
 (0)