@@ -3322,7 +3322,7 @@ Py_ssize_t
33223322_PyInterpreterState_GuardCountdown (PyInterpreterState * interp )
33233323{
33243324 assert (interp != NULL );
3325- Py_ssize_t count = _Py_atomic_load_uintptr_relaxed (& interp -> finalization_guards );
3325+ Py_ssize_t count = _Py_atomic_load_uintptr (& interp -> finalization_guards );
33263326 assert (count >= 0 );
33273327 return count ;
33283328}
@@ -3346,7 +3346,11 @@ try_acquire_interp_guard(PyInterpreterState *interp, PyInterpreterGuard *guard)
33463346 if (expected == _PyInterpreterGuard_GUARDS_NOT_ALLOWED ) {
33473347 return -1 ;
33483348 }
3349- } while (_Py_atomic_compare_exchange_uintptr (& interp -> finalization_guards , & expected , expected + 1 ) == 0 );
3349+ } while (_Py_atomic_compare_exchange_uintptr (& interp -> finalization_guards ,
3350+ & expected ,
3351+ expected + 1 ) == 0 );
3352+ assert (_Py_atomic_load_uintptr (& interp -> finalization_guards ) > 0 );
3353+ assert (_Py_atomic_load_uintptr (& interp -> finalization_guards ) != _PyInterpreterGuard_GUARDS_NOT_ALLOWED );
33503354
33513355 guard -> interp = interp ;
33523356 return 0 ;
@@ -3501,7 +3505,7 @@ PyThreadState_Ensure(PyInterpreterGuard *guard)
35013505 fresh_tstate -> ensure .delete_on_release = 1 ;
35023506
35033507 if (attached_tstate != NULL ) {
3504- return (PyThreadStateToken * )PyThreadState_Swap (fresh_tstate );
3508+ return (PyThreadStateToken * )PyThreadState_Swap (fresh_tstate );
35053509 }
35063510
35073511 _PyThreadState_Attach (fresh_tstate );
@@ -3570,11 +3574,11 @@ PyThreadState_Release(PyThreadStateToken *token)
35703574 (void )check_tstate ;
35713575 assert (check_tstate == tstate );
35723576
3573- if (owned_guard != NULL ) {
3574- PyInterpreterGuard_Close (owned_guard );
3575- }
3576-
35773577 if (tstate -> ensure .delete_on_release ) {
35783578 PyThreadState_Delete (tstate );
35793579 }
3580+
3581+ if (owned_guard != NULL ) {
3582+ PyInterpreterGuard_Close (owned_guard );
3583+ }
35803584}
0 commit comments