@@ -1864,7 +1864,8 @@ gc_should_collect(GCState *gcstate)
1864
1864
{
1865
1865
int count = _Py_atomic_load_int_relaxed (& gcstate -> young .count );
1866
1866
int threshold = gcstate -> young .threshold ;
1867
- if (count <= threshold || threshold == 0 || !gcstate -> enabled ) {
1867
+ int gc_enabled = _Py_atomic_load_int_relaxed (& gcstate -> enabled );
1868
+ if (count <= threshold || threshold == 0 || !gc_enabled ) {
1868
1869
return false;
1869
1870
}
1870
1871
// Avoid quadratic behavior by scaling threshold to the number of live
@@ -2340,25 +2341,21 @@ int
2340
2341
PyGC_Enable (void )
2341
2342
{
2342
2343
GCState * gcstate = get_gc_state ();
2343
- int old_state = gcstate -> enabled ;
2344
- gcstate -> enabled = 1 ;
2345
- return old_state ;
2344
+ return _Py_atomic_exchange_int (& gcstate -> enabled , 1 );
2346
2345
}
2347
2346
2348
2347
int
2349
2348
PyGC_Disable (void )
2350
2349
{
2351
2350
GCState * gcstate = get_gc_state ();
2352
- int old_state = gcstate -> enabled ;
2353
- gcstate -> enabled = 0 ;
2354
- return old_state ;
2351
+ return _Py_atomic_exchange_int (& gcstate -> enabled , 0 );
2355
2352
}
2356
2353
2357
2354
int
2358
2355
PyGC_IsEnabled (void )
2359
2356
{
2360
2357
GCState * gcstate = get_gc_state ();
2361
- return gcstate -> enabled ;
2358
+ return _Py_atomic_load_int_relaxed ( & gcstate -> enabled ) ;
2362
2359
}
2363
2360
2364
2361
/* Public API to invoke gc.collect() from C */
@@ -2368,7 +2365,7 @@ PyGC_Collect(void)
2368
2365
PyThreadState * tstate = _PyThreadState_GET ();
2369
2366
GCState * gcstate = & tstate -> interp -> gc ;
2370
2367
2371
- if (!gcstate -> enabled ) {
2368
+ if (!_Py_atomic_load_int_relaxed ( & gcstate -> enabled ) ) {
2372
2369
return 0 ;
2373
2370
}
2374
2371
@@ -2527,8 +2524,7 @@ _PyObject_GC_Link(PyObject *op)
2527
2524
void
2528
2525
_Py_RunGC (PyThreadState * tstate )
2529
2526
{
2530
- GCState * gcstate = get_gc_state ();
2531
- if (!gcstate -> enabled ) {
2527
+ if (!PyGC_IsEnabled ()) {
2532
2528
return ;
2533
2529
}
2534
2530
gc_collect_main (tstate , 0 , _Py_GC_REASON_HEAP );
0 commit comments