Skip to content

Commit a747442

Browse files
committed
gh-109693: Use pyatomic.h for signal module
1 parent 02cdaef commit a747442

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

Include/internal/pycore_signal.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ PyAPI_FUNC(void) _Py_RestoreSignals(void);
3838
#define INVALID_FD (-1)
3939

4040
struct _signals_runtime_state {
41-
volatile struct {
42-
_Py_atomic_int tripped;
41+
struct {
42+
int tripped;
4343
/* func is atomic to ensure that PyErr_SetInterrupt is async-signal-safe
4444
* (even though it would probably be otherwise, anyway).
4545
*/
46-
_Py_atomic_address func;
46+
uintptr_t func;
4747
} handlers[Py_NSIG];
4848

4949
volatile struct {
@@ -64,7 +64,7 @@ struct _signals_runtime_state {
6464
} wakeup;
6565

6666
/* Speed up sigcheck() when none tripped */
67-
_Py_atomic_int is_tripped;
67+
int is_tripped;
6868

6969
/* These objects necessarily belong to the main interpreter. */
7070
PyObject *default_handler;

Modules/signalmodule.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
/* XXX Signals should be recorded per thread, now we have thread state. */
55

66
#include "Python.h"
7-
#include "pycore_atomic.h" // _Py_atomic_int
87
#include "pycore_call.h" // _PyObject_Call()
98
#include "pycore_ceval.h" // _PyEval_SignalReceived()
109
#include "pycore_emscripten_signal.h" // _Py_CHECK_EMSCRIPTEN_SIGNALS
@@ -124,13 +123,13 @@ typedef struct {
124123
Py_LOCAL_INLINE(PyObject *)
125124
get_handler(int i)
126125
{
127-
return (PyObject *)_Py_atomic_load(&Handlers[i].func);
126+
return (PyObject *)_Py_atomic_load_uintptr(&Handlers[i].func);
128127
}
129128

130129
Py_LOCAL_INLINE(void)
131130
set_handler(int i, PyObject* func)
132131
{
133-
_Py_atomic_store(&Handlers[i].func, (uintptr_t)func);
132+
_Py_atomic_store_uintptr(&Handlers[i].func, (uintptr_t)func);
134133
}
135134

136135

@@ -267,11 +266,11 @@ report_wakeup_send_error(void* data)
267266
static void
268267
trip_signal(int sig_num)
269268
{
270-
_Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
269+
_Py_atomic_store_int_relaxed(&Handlers[sig_num].tripped, 1);
271270

272271
/* Set is_tripped after setting .tripped, as it gets
273272
cleared in PyErr_CheckSignals() before .tripped. */
274-
_Py_atomic_store(&is_tripped, 1);
273+
_Py_atomic_store_int(&is_tripped, 1);
275274

276275
/* Signals are always handled by the main interpreter */
277276
PyInterpreterState *interp = _PyInterpreterState_Main();
@@ -1731,7 +1730,7 @@ _PySignal_Fini(void)
17311730
// Restore default signals and clear handlers
17321731
for (int signum = 1; signum < Py_NSIG; signum++) {
17331732
PyObject *func = get_handler(signum);
1734-
_Py_atomic_store_relaxed(&Handlers[signum].tripped, 0);
1733+
_Py_atomic_store_int_relaxed(&Handlers[signum].tripped, 0);
17351734
set_handler(signum, NULL);
17361735
if (func != NULL
17371736
&& func != Py_None
@@ -1785,7 +1784,7 @@ int
17851784
_PyErr_CheckSignalsTstate(PyThreadState *tstate)
17861785
{
17871786
_Py_CHECK_EMSCRIPTEN_SIGNALS();
1788-
if (!_Py_atomic_load(&is_tripped)) {
1787+
if (!_Py_atomic_load_int(&is_tripped)) {
17891788
return 0;
17901789
}
17911790

@@ -1803,15 +1802,15 @@ _PyErr_CheckSignalsTstate(PyThreadState *tstate)
18031802
* we receive a signal i after we zero is_tripped and before we
18041803
* check Handlers[i].tripped.
18051804
*/
1806-
_Py_atomic_store(&is_tripped, 0);
1805+
_Py_atomic_store_int(&is_tripped, 0);
18071806

18081807
_PyInterpreterFrame *frame = _PyThreadState_GetFrame(tstate);
18091808
signal_state_t *state = &signal_global_state;
18101809
for (int i = 1; i < Py_NSIG; i++) {
1811-
if (!_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
1810+
if (!_Py_atomic_load_int_relaxed(&Handlers[i].tripped)) {
18121811
continue;
18131812
}
1814-
_Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
1813+
_Py_atomic_store_int_relaxed(&Handlers[i].tripped, 0);
18151814

18161815
/* Signal handlers can be modified while a signal is received,
18171816
* and therefore the fact that trip_signal() or PyErr_SetInterrupt()
@@ -1857,7 +1856,7 @@ _PyErr_CheckSignalsTstate(PyThreadState *tstate)
18571856
}
18581857
if (!result) {
18591858
/* On error, re-schedule a call to _PyErr_CheckSignalsTstate() */
1860-
_Py_atomic_store(&is_tripped, 1);
1859+
_Py_atomic_store_int(&is_tripped, 1);
18611860
return -1;
18621861
}
18631862

@@ -1975,7 +1974,7 @@ _PySignal_Init(int install_signal_handlers)
19751974
#endif
19761975

19771976
for (int signum = 1; signum < Py_NSIG; signum++) {
1978-
_Py_atomic_store_relaxed(&Handlers[signum].tripped, 0);
1977+
_Py_atomic_store_int_relaxed(&Handlers[signum].tripped, 0);
19791978
}
19801979

19811980
if (install_signal_handlers) {
@@ -1997,11 +1996,11 @@ _PyOS_InterruptOccurred(PyThreadState *tstate)
19971996
return 0;
19981997
}
19991998

2000-
if (!_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
1999+
if (!_Py_atomic_load_int_relaxed(&Handlers[SIGINT].tripped)) {
20012000
return 0;
20022001
}
20032002

2004-
_Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
2003+
_Py_atomic_store_int_relaxed(&Handlers[SIGINT].tripped, 0);
20052004
return 1;
20062005
}
20072006

@@ -2019,13 +2018,13 @@ PyOS_InterruptOccurred(void)
20192018
static void
20202019
_clear_pending_signals(void)
20212020
{
2022-
if (!_Py_atomic_load(&is_tripped)) {
2021+
if (!_Py_atomic_load_int(&is_tripped)) {
20232022
return;
20242023
}
20252024

2026-
_Py_atomic_store(&is_tripped, 0);
2025+
_Py_atomic_store_int(&is_tripped, 0);
20272026
for (int i = 1; i < Py_NSIG; ++i) {
2028-
_Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
2027+
_Py_atomic_store_int_relaxed(&Handlers[i].tripped, 0);
20292028
}
20302029
}
20312030

Python/ceval_gil.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ update_eval_breaker_from_thread(PyInterpreterState *interp, PyThreadState *tstat
7676
_Py_set_eval_breaker_bit(interp, _PY_CALLS_TO_DO_BIT, 1);
7777
}
7878
if (_Py_ThreadCanHandleSignals(interp)) {
79-
if (_Py_atomic_load(&_PyRuntime.signals.is_tripped)) {
79+
if (_Py_atomic_load_int(&_PyRuntime.signals.is_tripped)) {
8080
_Py_set_eval_breaker_bit(interp, _PY_SIGNALS_PENDING_BIT, 1);
8181
}
8282
}

0 commit comments

Comments
 (0)