bpo-38858: Add _Py_IsMainInterpreter(tstate) (GH-17293)

This commit is contained in:
Victor Stinner 2019-11-20 17:34:39 +01:00 committed by GitHub
parent db7925a1ca
commit fff7bbfdb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 3 deletions

View File

@ -269,6 +269,8 @@ PyAPI_FUNC(void) _PyRuntime_Finalize(void);
#define _Py_CURRENTLY_FINALIZING(runtime, tstate) \ #define _Py_CURRENTLY_FINALIZING(runtime, tstate) \
(runtime->finalizing == tstate) (runtime->finalizing == tstate)
PyAPI_FUNC(int) _Py_IsMainInterpreter(PyThreadState* tstate);
/* Variable and macro for in-line access to current thread /* Variable and macro for in-line access to current thread
and interpreter state */ and interpreter state */

View File

@ -1466,9 +1466,9 @@ static PyObject *
_thread__is_main_interpreter_impl(PyObject *module) _thread__is_main_interpreter_impl(PyObject *module)
/*[clinic end generated code: output=7dd82e1728339adc input=cc1eb00fd4598915]*/ /*[clinic end generated code: output=7dd82e1728339adc input=cc1eb00fd4598915]*/
{ {
_PyRuntimeState *runtime = &_PyRuntime; PyThreadState *tstate = _PyThreadState_GET();
PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp; int is_main = _Py_IsMainInterpreter(tstate);
return PyBool_FromLong(interp == runtime->interpreters.main); return PyBool_FromLong(is_main);
} }
static PyMethodDef thread_methods[] = { static PyMethodDef thread_methods[] = {

View File

@ -159,6 +159,12 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
#define HEAD_UNLOCK(runtime) \ #define HEAD_UNLOCK(runtime) \
PyThread_release_lock((runtime)->interpreters.mutex) PyThread_release_lock((runtime)->interpreters.mutex)
int
_Py_IsMainInterpreter(PyThreadState* tstate)
{
return (tstate->interp == tstate->interp->runtime->interpreters.main);
}
/* Forward declaration */ /* Forward declaration */
static void _PyGILState_NoteThreadState( static void _PyGILState_NoteThreadState(
struct _gilstate_runtime_state *gilstate, PyThreadState* tstate); struct _gilstate_runtime_state *gilstate, PyThreadState* tstate);