summaryrefslogtreecommitdiff
path: root/Include/internal/pycore_pystate.h
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-03-20 13:38:58 +0100
committerGitHub <noreply@github.com>2020-03-20 13:38:58 +0100
commitd2a8e5b42c5e9c4e745a0589043a8aebb49f8ca2 (patch)
treebf76400eb6529986ba4319faf4664a70a7303869 /Include/internal/pycore_pystate.h
parentda2914db4b6f786a1e9f0b424efeeb6ca9418912 (diff)
downloadcpython-git-d2a8e5b42c5e9c4e745a0589043a8aebb49f8ca2.tar.gz
bpo-40010: COMPUTE_EVAL_BREAKER() checks for subinterpreter (GH-19087)
COMPUTE_EVAL_BREAKER() now also checks if the Python thread state belongs to the main interpreter. Don't break the evaluation loop if there are pending signals but the Python thread state it belongs to a subinterpeter. * Add _Py_IsMainThread() function. * Add _Py_ThreadCanHandleSignals() function.
Diffstat (limited to 'Include/internal/pycore_pystate.h')
-rw-r--r--Include/internal/pycore_pystate.h28
1 files changed, 27 insertions, 1 deletions
diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h
index 92eeac762f..da034e185a 100644
--- a/Include/internal/pycore_pystate.h
+++ b/Include/internal/pycore_pystate.h
@@ -294,7 +294,33 @@ _PyRuntimeState_SetFinalizing(_PyRuntimeState *runtime, PyThreadState *tstate) {
_Py_atomic_store_relaxed(&runtime->_finalizing, (uintptr_t)tstate);
}
-PyAPI_FUNC(int) _Py_IsMainInterpreter(PyThreadState* tstate);
+/* Check if the current thread is the main thread.
+ Use _Py_IsMainInterpreter() to check if it's the main interpreter. */
+static inline int
+_Py_IsMainThread(void)
+{
+ unsigned long thread = PyThread_get_thread_ident();
+ return (thread == _PyRuntime.main_thread);
+}
+
+
+static inline int
+_Py_IsMainInterpreter(PyThreadState* tstate)
+{
+ /* Use directly _PyRuntime rather than tstate->interp->runtime, since
+ this function is used in performance critical code path (ceval) */
+ return (tstate->interp == _PyRuntime.interpreters.main);
+}
+
+
+/* Only handle signals on the main thread of the main interpreter. */
+static inline int
+_Py_ThreadCanHandleSignals(PyThreadState *tstate)
+{
+ /* Use directly _PyRuntime rather than tstate->interp->runtime, since
+ this function is used in performance critical code path (ceval) */
+ return (_Py_IsMainThread() && _Py_IsMainInterpreter(tstate));
+}
/* Variable and macro for in-line access to current thread