Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use PyInterpreterState.runtime.
  • Loading branch information
ericsnowcurrently committed Jun 1, 2019
commit 701fe2f947804d88b6770aab25c70f15cb5b4822
6 changes: 3 additions & 3 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ struct _ts {
* if the thread holds the last reference to the lock, decref'ing the
* lock will delete the lock, and that may trigger arbitrary Python code
* if there's a weakref, with a callback, to the lock. But by this time
* _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest
* of C code can be allowed to run (in particular it must not be possible to
* release the GIL).
* _PyRuntimeState.gilstate.tstate_current is already NULL, so only the
* simplest of C code can be allowed to run (in particular it must not be
* possible to release the GIL).
* So instead of holding the lock directly, the tstate holds a weakref to
* the lock: that's the value of on_delete_data below. Decref'ing a
* weakref is harmless.
Expand Down
7 changes: 4 additions & 3 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ PyAPI_FUNC(int) _PyDict_CheckConsistency(PyObject *mp, int check_content);
* NB: While the object is tracked by the collector, it must be safe to call the
* ob_traverse method.
*
* Internal note: _PyRuntime.gc.generation0->_gc_prev doesn't have any bit flags
* because it's not object header. So we don't use _PyGCHead_PREV() and
* _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
* Internal note: _PyRuntimeState.gc.generation0->_gc_prev doesn't have
* any bit flags because it's not object header. So we don't use
* _PyGCHead_PREV() and _PyGCHead_SET_PREV() for it to avoid unnecessary
* bitwise operations.
*
* The PyObject_GC_Track() function is the public version of this macro.
*/
Expand Down
3 changes: 2 additions & 1 deletion Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,8 @@ t_bootstrap(void *boot_raw)

tstate = boot->tstate;
tstate->thread_id = PyThread_get_thread_ident();
_PyThreadState_Init(&_PyRuntime, tstate);
_PyRuntimeState *runtime = tstate->interp->runtime;
_PyThreadState_Init(runtime, tstate);
PyEval_AcquireThread(tstate);
tstate->interp->num_threads++;
res = PyObject_Call(boot->func, boot->args, boot->keyw);
Expand Down
13 changes: 8 additions & 5 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ PyEval_AcquireThread(PyThreadState *tstate)
if (tstate == NULL) {
Py_FatalError("PyEval_AcquireThread: NULL new thread state");
}
assert(tstate->interp != NULL);

_PyRuntimeState *runtime = &_PyRuntime;
_PyRuntimeState *runtime = tstate->interp->runtime;
struct _ceval_runtime_state *ceval = &runtime->ceval;

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

_PyRuntimeState *runtime = &_PyRuntime;
_PyRuntimeState *runtime = tstate->interp->runtime;
PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
if (new_tstate != tstate) {
Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Expand Down Expand Up @@ -337,12 +339,13 @@ PyEval_SaveThread(void)
void
PyEval_RestoreThread(PyThreadState *tstate)
{
_PyRuntimeState *runtime = &_PyRuntime;
struct _ceval_runtime_state *ceval = &runtime->ceval;

if (tstate == NULL) {
Py_FatalError("PyEval_RestoreThread: NULL tstate");
}
assert(tstate->interp != NULL);

_PyRuntimeState *runtime = tstate->interp->runtime;
struct _ceval_runtime_state *ceval = &runtime->ceval;
assert(gil_created(&ceval->gil));

int err = errno;
Expand Down
3 changes: 2 additions & 1 deletion Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ PyImport_Cleanup(void)
_PyGC_CollectNoFail();
/* Dump GC stats before it's too late, since it uses the warnings
machinery. */
_PyGC_DumpShutdownStats(&_PyRuntime);
_PyRuntimeState *runtime = interp->runtime;
_PyGC_DumpShutdownStats(runtime);

/* Now, if there are any modules left alive, clear their globals to
minimize potential leaks. All C extension modules actually end
Expand Down
13 changes: 8 additions & 5 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ _PyInterpreterState_Enable(_PyRuntimeState *runtime)
PyInterpreterState *
PyInterpreterState_New(void)
{
_PyRuntimeState *runtime = &_PyRuntime;

if (PySys_Audit("cpython.PyInterpreterState_New", NULL) < 0) {
return NULL;
}
Expand All @@ -203,7 +205,7 @@ PyInterpreterState_New(void)

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

interp->runtime = &_PyRuntime;
interp->runtime = runtime;

interp->id_refcount = -1;
interp->check_interval = 100;
Expand All @@ -226,7 +228,6 @@ PyInterpreterState_New(void)
#endif
#endif

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

HEAD_LOCK(runtime);
Expand Down Expand Up @@ -500,7 +501,8 @@ _PyInterpreterState_IDDecref(PyInterpreterState *interp)
if (interp->id_mutex == NULL) {
return;
}
struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
_PyRuntimeState *runtime = interp->runtime;
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
assert(interp->id_refcount != 0);
interp->id_refcount -= 1;
Expand Down Expand Up @@ -562,7 +564,7 @@ threadstate_getframe(PyThreadState *self)
static PyThreadState *
new_threadstate(PyInterpreterState *interp, int init)
{
_PyRuntimeState *runtime = &_PyRuntime;
_PyRuntimeState *runtime = interp->runtime;
PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
if (tstate == NULL) {
return NULL;
Expand Down Expand Up @@ -1132,8 +1134,9 @@ _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 = &_PyRuntime.gilstate;
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
}
Expand Down
14 changes: 8 additions & 6 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ should_audit(void)
if (!ts) {
return 0;
}
PyInterpreterState *is = ts ? ts->interp : NULL;
return _PyRuntime.audit_hook_head
PyInterpreterState *is = ts->interp;
_PyRuntimeState *runtime = is->runtime;
return runtime->audit_hook_head
|| (is && is->audit_hooks)
|| PyDTrace_AUDIT_ENABLED();
}
Expand Down Expand Up @@ -280,8 +281,8 @@ void _PySys_ClearAuditHooks(void) {
PySys_Audit("cpython._PySys_ClearAuditHooks", NULL);
PyErr_Clear();

_Py_AuditHookEntry *e = _PyRuntime.audit_hook_head, *n;
_PyRuntime.audit_hook_head = NULL;
_Py_AuditHookEntry *e = runtime->audit_hook_head, *n;
runtime->audit_hook_head = NULL;
while (e) {
n = e->next;
PyMem_RawFree(e);
Expand All @@ -292,6 +293,7 @@ void _PySys_ClearAuditHooks(void) {
int
PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
{
_PyRuntimeState *runtime = &_PyRuntime;
/* Invoke existing audit hooks to allow them an opportunity to abort. */
/* Cannot invoke hooks until we are initialized */
if (Py_IsInitialized()) {
Expand All @@ -305,10 +307,10 @@ PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
}
}

_Py_AuditHookEntry *e = _PyRuntime.audit_hook_head;
_Py_AuditHookEntry *e = runtime->audit_hook_head;
if (!e) {
e = (_Py_AuditHookEntry*)PyMem_RawMalloc(sizeof(_Py_AuditHookEntry));
_PyRuntime.audit_hook_head = e;
runtime->audit_hook_head = e;
} else {
while (e->next)
e = e->next;
Expand Down