From 0453e494b6e23c876c1bb097966f2c68ec72fada Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 17 Mar 2025 12:31:55 +0100 Subject: gh-131238: Convert pycore_pystate.h static inline to functions (#131352) Convert static inline functions to functions: * _Py_IsMainThread() * _PyInterpreterState_Main() * _Py_IsMainInterpreterFinalizing() * _Py_GetMainConfig() --- Python/pystate.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'Python/pystate.c') diff --git a/Python/pystate.c b/Python/pystate.c index 99f8774d446..68a7426a260 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -3137,3 +3137,41 @@ _PyThreadState_ClearMimallocHeaps(PyThreadState *tstate) } #endif } + + +int +_Py_IsMainThread(void) +{ + unsigned long thread = PyThread_get_thread_ident(); + return (thread == _PyRuntime.main_thread); +} + + +PyInterpreterState * +_PyInterpreterState_Main(void) +{ + return _PyRuntime.interpreters.main; +} + + +int +_Py_IsMainInterpreterFinalizing(PyInterpreterState *interp) +{ + /* bpo-39877: Access _PyRuntime directly rather than using + tstate->interp->runtime to support calls from Python daemon threads. + After Py_Finalize() has been called, tstate can be a dangling pointer: + point to PyThreadState freed memory. */ + return (_PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL && + interp == &_PyRuntime._main_interpreter); +} + + +const PyConfig * +_Py_GetMainConfig(void) +{ + PyInterpreterState *interp = _PyInterpreterState_Main(); + if (interp == NULL) { + return NULL; + } + return _PyInterpreterState_GetConfig(interp); +} -- cgit v1.2.3