Skip to content

Commit fa274dd

Browse files
colesburyseehwan80
authored andcommitted
pythongh-130605: Use relaxed atomics to set the GIL switch interval (pythongh-130654)
The interval may be concurrently read by a thread attempting to acquire the GIL.
1 parent b1fb9eb commit fa274dd

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Python/ceval_gil.c

+6-3
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)