summaryrefslogtreecommitdiffstats
path: root/Include
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-03-20 12:38:58 (GMT)
committerGitHub <noreply@github.com>2020-03-20 12:38:58 (GMT)
commitd2a8e5b42c5e9c4e745a0589043a8aebb49f8ca2 (patch)
treebf76400eb6529986ba4319faf4664a70a7303869 /Include
parentda2914db4b6f786a1e9f0b424efeeb6ca9418912 (diff)
downloadcpython-d2a8e5b42c5e9c4e745a0589043a8aebb49f8ca2.zip
cpython-d2a8e5b42c5e9c4e745a0589043a8aebb49f8ca2.tar.gz
cpython-d2a8e5b42c5e9c4e745a0589043a8aebb49f8ca2.tar.bz2
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')
-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 92eeac7..da034e1 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