https://github.com/python/cpython/commit/038e4d606bdc3e38f74514ae3ddfdc7a48b7b19e
commit: 038e4d606bdc3e38f74514ae3ddfdc7a48b7b19e
branch: main
author: Sam Gross <colesb...@gmail.com>
committer: colesbury <colesb...@gmail.com>
date: 2025-02-28T09:27:18-05:00
summary:

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.

files:
M Python/ceval_gil.c

diff --git a/Python/ceval_gil.c b/Python/ceval_gil.c
index 416eec01052224..7a3cd8d8044739 100644
--- a/Python/ceval_gil.c
+++ b/Python/ceval_gil.c
@@ -325,7 +325,10 @@ take_gil(PyThreadState *tstate)
     while (_Py_atomic_load_int_relaxed(&gil->locked)) {
         unsigned long saved_switchnum = gil->switch_number;
 
-        unsigned long interval = (gil->interval >= 1 ? gil->interval : 1);
+        unsigned long interval = _Py_atomic_load_ulong_relaxed(&gil->interval);
+        if (interval < 1) {
+            interval = 1;
+        }
         int timed_out = 0;
         COND_TIMED_WAIT(gil->cond, gil->mutex, interval, timed_out);
 
@@ -420,7 +423,7 @@ void _PyEval_SetSwitchInterval(unsigned long microseconds)
     PyInterpreterState *interp = _PyInterpreterState_GET();
     struct _gil_runtime_state *gil = interp->ceval.gil;
     assert(gil != NULL);
-    gil->interval = microseconds;
+    _Py_atomic_store_ulong_relaxed(&gil->interval, microseconds);
 }
 
 unsigned long _PyEval_GetSwitchInterval(void)
@@ -428,7 +431,7 @@ unsigned long _PyEval_GetSwitchInterval(void)
     PyInterpreterState *interp = _PyInterpreterState_GET();
     struct _gil_runtime_state *gil = interp->ceval.gil;
     assert(gil != NULL);
-    return gil->interval;
+    return _Py_atomic_load_ulong_relaxed(&gil->interval);
 }
 
 

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to