Skip to content

Commit d8613dc

Browse files
authored
bpo-37031: Reuse _PyRuntime.main_thread in signalmodule.c (GH-13538)
Remove main_thread and main_interp variables from signalmodule.c: reuse _PyRuntime which already track the main thread and the main interpreter. * Remove #include <sys/types.h> which became useless: getpid() call has been removed. * Add runtime argument to is_main() * is_main() now gets the interpreter from runtime.
1 parent 2a37f8f commit d8613dc

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

Modules/signalmodule.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ class sigset_t_converter(CConverter):
9999
may not be the thread that received the signal.
100100
*/
101101

102-
#include <sys/types.h> /* For pid_t */
103102
#include "pythread.h"
104-
static unsigned long main_thread;
105-
static PyInterpreterState *main_interp;
106103

107104
static volatile struct {
108105
_Py_atomic_int tripped;
@@ -190,10 +187,12 @@ itimer_retval(struct itimerval *iv)
190187
#endif
191188

192189
static int
193-
is_main(void)
190+
is_main(_PyRuntimeState *runtime)
194191
{
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);
197196
}
198197

199198
static PyObject *
@@ -474,7 +473,9 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
474473
return NULL;
475474
}
476475
#endif
477-
if (!is_main()) {
476+
477+
_PyRuntimeState *runtime = &_PyRuntime;
478+
if (!is_main(runtime)) {
478479
PyErr_SetString(PyExc_ValueError,
479480
"signal only works in main thread");
480481
return NULL;
@@ -691,7 +692,8 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
691692
return NULL;
692693
#endif
693694

694-
if (!is_main()) {
695+
_PyRuntimeState *runtime = &_PyRuntime;
696+
if (!is_main(runtime)) {
695697
PyErr_SetString(PyExc_ValueError,
696698
"set_wakeup_fd only works in main thread");
697699
return NULL;
@@ -1329,9 +1331,6 @@ PyInit__signal(void)
13291331
PyObject *m, *d, *x;
13301332
int i;
13311333

1332-
main_thread = PyThread_get_thread_ident();
1333-
main_interp = _PyInterpreterState_Get();
1334-
13351334
/* Create the module and add the functions */
13361335
m = PyModule_Create(&signalmodule);
13371336
if (m == NULL)
@@ -1622,7 +1621,8 @@ finisignal(void)
16221621
int
16231622
PyErr_CheckSignals(void)
16241623
{
1625-
if (!is_main()) {
1624+
_PyRuntimeState *runtime = &_PyRuntime;
1625+
if (!is_main(runtime)) {
16261626
return 0;
16271627
}
16281628

@@ -1716,7 +1716,8 @@ int
17161716
PyOS_InterruptOccurred(void)
17171717
{
17181718
if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
1719-
if (!is_main()) {
1719+
_PyRuntimeState *runtime = &_PyRuntime;
1720+
if (!is_main(runtime)) {
17201721
return 0;
17211722
}
17221723
_Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
@@ -1744,14 +1745,13 @@ _PySignal_AfterFork(void)
17441745
* in both processes if they came in just before the fork() but before
17451746
* the interpreter had an opportunity to call the handlers. issue9535. */
17461747
_clear_pending_signals();
1747-
main_thread = PyThread_get_thread_ident();
1748-
main_interp = _PyInterpreterState_Get();
17491748
}
17501749

17511750
int
17521751
_PyOS_IsMainThread(void)
17531752
{
1754-
return is_main();
1753+
_PyRuntimeState *runtime = &_PyRuntime;
1754+
return is_main(runtime);
17551755
}
17561756

17571757
#ifdef MS_WINDOWS

0 commit comments

Comments
 (0)