Skip to content

Commit 1f335ca

Browse files
committed
pythongh-129533: Update PyGC_Enable/Disable/IsEnabled to use CAS operation
1 parent cf4c4ec commit 1f335ca

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Update :c:func:`PyGC_Enable()`, :c:func:`PyGC_Disable()`,
2+
:c:func:`PyGC_IsEnabled()` to use CAS operation for thread-safety at
3+
free-threading build. Patch by Donghee Na.

Python/gc_free_threading.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -1952,25 +1952,29 @@ int
19521952
PyGC_Enable(void)
19531953
{
19541954
GCState *gcstate = get_gc_state();
1955-
int old_state = gcstate->enabled;
1956-
gcstate->enabled = 1;
1955+
int old_state;
1956+
do {
1957+
old_state = gcstate->enabled;
1958+
} while (!_Py_atomic_compare_exchange_int(&gcstate->enabled, &old_state, 1));
19571959
return old_state;
19581960
}
19591961

19601962
int
19611963
PyGC_Disable(void)
19621964
{
19631965
GCState *gcstate = get_gc_state();
1964-
int old_state = gcstate->enabled;
1965-
gcstate->enabled = 0;
1966+
int old_state;
1967+
do {
1968+
old_state = gcstate->enabled;
1969+
} while (!_Py_atomic_compare_exchange_int(&gcstate->enabled, &old_state, 0));
19661970
return old_state;
19671971
}
19681972

19691973
int
19701974
PyGC_IsEnabled(void)
19711975
{
19721976
GCState *gcstate = get_gc_state();
1973-
return gcstate->enabled;
1977+
return _Py_atomic_load_int(&(gcstate->enabled));
19741978
}
19751979

19761980
/* Public API to invoke gc.collect() from C */

0 commit comments

Comments
 (0)