@@ -1059,7 +1059,8 @@ gc_should_collect(GCState *gcstate)
1059
1059
{
1060
1060
int count = _Py_atomic_load_int_relaxed (& gcstate -> generations [0 ].count );
1061
1061
int threshold = gcstate -> generations [0 ].threshold ;
1062
- if (count <= threshold || threshold == 0 || !gcstate -> enabled ) {
1062
+ int gc_enabled = _Py_atomic_load_int_relaxed (& gcstate -> enabled );
1063
+ if (count <= threshold || threshold == 0 || !gc_enabled ) {
1063
1064
return false;
1064
1065
}
1065
1066
// Avoid quadratic behavior by scaling threshold to the number of live
@@ -1495,25 +1496,21 @@ int
1495
1496
PyGC_Enable (void )
1496
1497
{
1497
1498
GCState * gcstate = get_gc_state ();
1498
- int old_state = gcstate -> enabled ;
1499
- gcstate -> enabled = 1 ;
1500
- return old_state ;
1499
+ return _Py_atomic_exchange_int (& gcstate -> enabled , 1 );
1501
1500
}
1502
1501
1503
1502
int
1504
1503
PyGC_Disable (void )
1505
1504
{
1506
1505
GCState * gcstate = get_gc_state ();
1507
- int old_state = gcstate -> enabled ;
1508
- gcstate -> enabled = 0 ;
1509
- return old_state ;
1506
+ return _Py_atomic_exchange_int (& gcstate -> enabled , 0 );
1510
1507
}
1511
1508
1512
1509
int
1513
1510
PyGC_IsEnabled (void )
1514
1511
{
1515
1512
GCState * gcstate = get_gc_state ();
1516
- return gcstate -> enabled ;
1513
+ return _Py_atomic_load_int_relaxed ( & gcstate -> enabled ) ;
1517
1514
}
1518
1515
1519
1516
/* Public API to invoke gc.collect() from C */
@@ -1523,7 +1520,7 @@ PyGC_Collect(void)
1523
1520
PyThreadState * tstate = _PyThreadState_GET ();
1524
1521
GCState * gcstate = & tstate -> interp -> gc ;
1525
1522
1526
- if (!gcstate -> enabled ) {
1523
+ if (!_Py_atomic_load_int_relaxed ( & gcstate -> enabled ) ) {
1527
1524
return 0 ;
1528
1525
}
1529
1526
@@ -1681,8 +1678,7 @@ _PyObject_GC_Link(PyObject *op)
1681
1678
void
1682
1679
_Py_RunGC (PyThreadState * tstate )
1683
1680
{
1684
- GCState * gcstate = get_gc_state ();
1685
- if (!gcstate -> enabled ) {
1681
+ if (!PyGC_IsEnabled ()) {
1686
1682
return ;
1687
1683
}
1688
1684
gc_collect_main (tstate , 0 , _Py_GC_REASON_HEAP );
0 commit comments