Skip to content

Commit 038e4d6

Browse files
authored
gh-130605: Use relaxed atomics to set the GIL switch interval (gh-130654)
The interval may be concurrently read by a thread attempting to acquire the GIL.
1 parent c1b4a6b commit 038e4d6

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Python/ceval_gil.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,10 @@ take_gil(PyThreadState *tstate)
325325
while (_Py_atomic_load_int_relaxed(&gil->locked)) {
326326
unsigned long saved_switchnum = gil->switch_number;
327327

328-
unsigned long interval = (gil->interval >= 1 ? gil->interval : 1);
328+
unsigned long interval = _Py_atomic_load_ulong_relaxed(&gil->interval);
329+
if (interval < 1) {
330+
interval = 1;
331+
}
329332
int timed_out = 0;
330333
COND_TIMED_WAIT(gil->cond, gil->mutex, interval, timed_out);
331334

@@ -420,15 +423,15 @@ void _PyEval_SetSwitchInterval(unsigned long microseconds)
420423
PyInterpreterState *interp = _PyInterpreterState_GET();
421424
struct _gil_runtime_state *gil = interp->ceval.gil;
422425
assert(gil != NULL);
423-
gil->interval = microseconds;
426+
_Py_atomic_store_ulong_relaxed(&gil->interval, microseconds);
424427
}
425428

426429
unsigned long _PyEval_GetSwitchInterval(void)
427430
{
428431
PyInterpreterState *interp = _PyInterpreterState_GET();
429432
struct _gil_runtime_state *gil = interp->ceval.gil;
430433
assert(gil != NULL);
431-
return gil->interval;
434+
return _Py_atomic_load_ulong_relaxed(&gil->interval);
432435
}
433436

434437

0 commit comments

Comments
 (0)