@@ -99,10 +99,7 @@ class sigset_t_converter(CConverter):
99
99
may not be the thread that received the signal.
100
100
*/
101
101
102
- #include <sys/types.h> /* For pid_t */
103
102
#include "pythread.h"
104
- static unsigned long main_thread ;
105
- static PyInterpreterState * main_interp ;
106
103
107
104
static volatile struct {
108
105
_Py_atomic_int tripped ;
@@ -190,10 +187,12 @@ itimer_retval(struct itimerval *iv)
190
187
#endif
191
188
192
189
static int
193
- is_main (void )
190
+ is_main (_PyRuntimeState * runtime )
194
191
{
195
- return PyThread_get_thread_ident () == main_thread &&
196
- _PyInterpreterState_Get () == main_interp ;
192
+ unsigned long thread = PyThread_get_thread_ident ();
193
+ PyInterpreterState * interp = _PyRuntimeState_GetThreadState (runtime )-> interp ;
194
+ return (thread == runtime -> main_thread
195
+ && interp == runtime -> interpreters .main );
197
196
}
198
197
199
198
static PyObject *
@@ -474,7 +473,9 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
474
473
return NULL ;
475
474
}
476
475
#endif
477
- if (!is_main ()) {
476
+
477
+ _PyRuntimeState * runtime = & _PyRuntime ;
478
+ if (!is_main (runtime )) {
478
479
PyErr_SetString (PyExc_ValueError ,
479
480
"signal only works in main thread" );
480
481
return NULL ;
@@ -691,7 +692,8 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
691
692
return NULL ;
692
693
#endif
693
694
694
- if (!is_main ()) {
695
+ _PyRuntimeState * runtime = & _PyRuntime ;
696
+ if (!is_main (runtime )) {
695
697
PyErr_SetString (PyExc_ValueError ,
696
698
"set_wakeup_fd only works in main thread" );
697
699
return NULL ;
@@ -1329,9 +1331,6 @@ PyInit__signal(void)
1329
1331
PyObject * m , * d , * x ;
1330
1332
int i ;
1331
1333
1332
- main_thread = PyThread_get_thread_ident ();
1333
- main_interp = _PyInterpreterState_Get ();
1334
-
1335
1334
/* Create the module and add the functions */
1336
1335
m = PyModule_Create (& signalmodule );
1337
1336
if (m == NULL )
@@ -1622,7 +1621,8 @@ finisignal(void)
1622
1621
int
1623
1622
PyErr_CheckSignals (void )
1624
1623
{
1625
- if (!is_main ()) {
1624
+ _PyRuntimeState * runtime = & _PyRuntime ;
1625
+ if (!is_main (runtime )) {
1626
1626
return 0 ;
1627
1627
}
1628
1628
@@ -1716,7 +1716,8 @@ int
1716
1716
PyOS_InterruptOccurred (void )
1717
1717
{
1718
1718
if (_Py_atomic_load_relaxed (& Handlers [SIGINT ].tripped )) {
1719
- if (!is_main ()) {
1719
+ _PyRuntimeState * runtime = & _PyRuntime ;
1720
+ if (!is_main (runtime )) {
1720
1721
return 0 ;
1721
1722
}
1722
1723
_Py_atomic_store_relaxed (& Handlers [SIGINT ].tripped , 0 );
@@ -1744,14 +1745,13 @@ _PySignal_AfterFork(void)
1744
1745
* in both processes if they came in just before the fork() but before
1745
1746
* the interpreter had an opportunity to call the handlers. issue9535. */
1746
1747
_clear_pending_signals ();
1747
- main_thread = PyThread_get_thread_ident ();
1748
- main_interp = _PyInterpreterState_Get ();
1749
1748
}
1750
1749
1751
1750
int
1752
1751
_PyOS_IsMainThread (void )
1753
1752
{
1754
- return is_main ();
1753
+ _PyRuntimeState * runtime = & _PyRuntime ;
1754
+ return is_main (runtime );
1755
1755
}
1756
1756
1757
1757
#ifdef MS_WINDOWS
0 commit comments