aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/gc_free_threading.c
diff options
context:
space:
mode:
authorDonghee Na <donghee.na@python.org>2025-02-07 07:41:13 +0900
committerGitHub <noreply@github.com>2025-02-07 07:41:13 +0900
commitb184abf074c0e1f379a238f07da5616460f36b93 (patch)
tree3c3e8845575417e4fdf53c96671ec933f4321582 /Python/gc_free_threading.c
parent4e3330f054b91049c7260eb02b1e2c3808958e11 (diff)
downloadcpython-b184abf074c0e1f379a238f07da5616460f36b93.tar.gz
cpython-b184abf074c0e1f379a238f07da5616460f36b93.zip
gh-129533: Update PyGC_Enable/Disable/IsEnabled to use atomic operation (gh-129563)
Diffstat (limited to 'Python/gc_free_threading.c')
-rw-r--r--Python/gc_free_threading.c18
1 files changed, 7 insertions, 11 deletions
diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c
index 10c76a67979..9e459da3a44 100644
--- a/Python/gc_free_threading.c
+++ b/Python/gc_free_threading.c
@@ -1864,7 +1864,8 @@ gc_should_collect(GCState *gcstate)
{
int count = _Py_atomic_load_int_relaxed(&gcstate->young.count);
int threshold = gcstate->young.threshold;
- if (count <= threshold || threshold == 0 || !gcstate->enabled) {
+ int gc_enabled = _Py_atomic_load_int_relaxed(&gcstate->enabled);
+ if (count <= threshold || threshold == 0 || !gc_enabled) {
return false;
}
// Avoid quadratic behavior by scaling threshold to the number of live
@@ -2340,25 +2341,21 @@ int
PyGC_Enable(void)
{
GCState *gcstate = get_gc_state();
- int old_state = gcstate->enabled;
- gcstate->enabled = 1;
- return old_state;
+ return _Py_atomic_exchange_int(&gcstate->enabled, 1);
}
int
PyGC_Disable(void)
{
GCState *gcstate = get_gc_state();
- int old_state = gcstate->enabled;
- gcstate->enabled = 0;
- return old_state;
+ return _Py_atomic_exchange_int(&gcstate->enabled, 0);
}
int
PyGC_IsEnabled(void)
{
GCState *gcstate = get_gc_state();
- return gcstate->enabled;
+ return _Py_atomic_load_int_relaxed(&gcstate->enabled);
}
/* Public API to invoke gc.collect() from C */
@@ -2368,7 +2365,7 @@ PyGC_Collect(void)
PyThreadState *tstate = _PyThreadState_GET();
GCState *gcstate = &tstate->interp->gc;
- if (!gcstate->enabled) {
+ if (!_Py_atomic_load_int_relaxed(&gcstate->enabled)) {
return 0;
}
@@ -2527,8 +2524,7 @@ _PyObject_GC_Link(PyObject *op)
void
_Py_RunGC(PyThreadState *tstate)
{
- GCState *gcstate = get_gc_state();
- if (!gcstate->enabled) {
+ if (!PyGC_IsEnabled()) {
return;
}
gc_collect_main(tstate, 0, _Py_GC_REASON_HEAP);