@@ -9,7 +9,7 @@ use crate::object::{GC_NO_OWNER, GC_PERMANENT, GC_REACHABLE, GC_UNTRACKED, GcLin
99use crate :: vm:: stop_the_world:: StopTheWorldGroup ;
1010use crate :: { AsObject , PyObject , PyObjectRef } ;
1111use core:: ptr:: NonNull ;
12- use core:: sync:: atomic:: { AtomicBool , AtomicU16 , AtomicU32 , AtomicUsize , Ordering } ;
12+ use core:: sync:: atomic:: { AtomicBool , AtomicI32 , AtomicU16 , AtomicU32 , AtomicUsize , Ordering } ;
1313
1414fn elapsed_secs (
1515 #[ cfg( target_arch = "wasm32" ) ] _start : ( ) ,
@@ -64,16 +64,16 @@ pub struct GcStats {
6464/// what its own collections have done.
6565pub struct GcGeneration {
6666 /// Threshold for triggering collection
67- threshold : AtomicU32 ,
67+ threshold : AtomicI32 ,
6868 /// Collection statistics
6969 stats : PyMutex < GcStats > ,
7070}
7171
7272impl GcGeneration {
7373 #[ must_use]
74- pub const fn new ( threshold : u32 ) -> Self {
74+ pub const fn new ( threshold : i32 ) -> Self {
7575 Self {
76- threshold : AtomicU32 :: new ( threshold) ,
76+ threshold : AtomicI32 :: new ( threshold) ,
7777 stats : PyMutex :: new ( GcStats {
7878 collections : 0 ,
7979 collected : 0 ,
@@ -86,11 +86,11 @@ impl GcGeneration {
8686
8787 /// Relaxed: this is policy read once per allocation, and a collection
8888 /// racing `gc.set_threshold()` may use either value.
89- pub fn threshold ( & self ) -> u32 {
89+ pub fn threshold ( & self ) -> i32 {
9090 self . threshold . load ( Ordering :: Relaxed )
9191 }
9292
93- pub fn set_threshold ( & self , value : u32 ) {
93+ pub fn set_threshold ( & self , value : i32 ) {
9494 self . threshold . store ( value, Ordering :: Relaxed ) ;
9595 }
9696
@@ -408,6 +408,15 @@ impl GcState {
408408 /// If generation is None, returns all such objects.
409409 /// If generation is Some(n), returns those in generation n only.
410410 pub fn get_objects ( & self , generation : Option < i32 > , owner : GcOwner ) -> Vec < PyObjectRef > {
411+ self . get_objects_impl ( generation, owner, true )
412+ }
413+
414+ fn get_objects_impl (
415+ & self ,
416+ generation : Option < i32 > ,
417+ owner : GcOwner ,
418+ include_permanent : bool ,
419+ ) -> Vec < PyObjectRef > {
411420 fn collect_from_list (
412421 list : & LinkedList < GcLink > ,
413422 owner : GcOwner ,
@@ -424,7 +433,9 @@ impl GcState {
424433 for gen_list in & self . generation_lists {
425434 result. extend ( collect_from_list ( & gen_list. read ( ) , owner) ) ;
426435 }
427- result. extend ( collect_from_list ( & self . permanent_list . read ( ) , owner) ) ;
436+ if include_permanent {
437+ result. extend ( collect_from_list ( & self . permanent_list . read ( ) , owner) ) ;
438+ }
428439 result
429440 }
430441 Some ( g) if ( 0 ..=2 ) . contains ( & g) => {
@@ -446,7 +457,7 @@ impl GcState {
446457 // Check gen0 threshold
447458 let count0 = self . counts [ 0 ] . load ( Ordering :: Relaxed ) as u32 ;
448459 let threshold0 = gc. generations [ 0 ] . threshold ( ) ;
449- if threshold0 > 0 && count0 >= threshold0 {
460+ if threshold0 != 0 && i64 :: from ( count0) > i64 :: from ( threshold0) {
450461 #[ cfg( feature = "threading" ) ]
451462 {
452463 // Defer to the next bytecode safepoint. Collecting here would
@@ -474,6 +485,17 @@ impl GcState {
474485 gc : & GcInterpreterState ,
475486 generation : usize ,
476487 force : bool ,
488+ ) -> CollectResult {
489+ self . collect_with_callbacks ( gc, generation, force, || { } , |_| { } )
490+ }
491+
492+ fn collect_with_callbacks (
493+ & self ,
494+ gc : & GcInterpreterState ,
495+ generation : usize ,
496+ force : bool ,
497+ before : impl FnOnce ( ) ,
498+ after : impl FnOnce ( & CollectResult ) ,
477499 ) -> CollectResult {
478500 if !force && !gc. is_enabled ( ) {
479501 #[ cfg( feature = "threading" ) ]
@@ -492,6 +514,15 @@ impl GcState {
492514 #[ cfg( feature = "threading" ) ]
493515 gc. scheduled . store ( false , Ordering :: Relaxed ) ;
494516
517+ before ( ) ;
518+ // Drop the body's generation locks, STW guards and temporary roots
519+ // before Python callbacks run, but retain the collecting lock.
520+ let result = self . collect_locked ( gc, generation) ;
521+ after ( & result) ;
522+ result
523+ }
524+
525+ fn collect_locked ( & self , gc : & GcInterpreterState , generation : usize ) -> CollectResult {
495526 let start_time = cfg_select ! {
496527 target_arch = "wasm32" => ( ) ,
497528 _ => std:: time:: Instant :: now( ) ,
@@ -1270,7 +1301,7 @@ impl GcInterpreterState {
12701301
12711302 /// Get debug flags
12721303 pub fn get_debug ( & self ) -> GcDebugFlags {
1273- GcDebugFlags :: from_bits_truncate ( self . debug . load ( Ordering :: SeqCst ) )
1304+ GcDebugFlags :: from_bits_retain ( self . debug . load ( Ordering :: SeqCst ) )
12741305 }
12751306
12761307 /// Set debug flags
@@ -1279,7 +1310,7 @@ impl GcInterpreterState {
12791310 }
12801311
12811312 /// Get thresholds for all generations
1282- pub fn get_threshold ( & self ) -> ( u32 , u32 , u32 ) {
1313+ pub fn get_threshold ( & self ) -> ( i32 , i32 , i32 ) {
12831314 (
12841315 self . generations [ 0 ] . threshold ( ) ,
12851316 self . generations [ 1 ] . threshold ( ) ,
@@ -1288,7 +1319,7 @@ impl GcInterpreterState {
12881319 }
12891320
12901321 /// Set thresholds
1291- pub fn set_threshold ( & self , t0 : u32 , t1 : Option < u32 > , t2 : Option < u32 > ) {
1322+ pub fn set_threshold ( & self , t0 : i32 , t1 : Option < i32 > , t2 : Option < i32 > ) {
12921323 self . generations [ 0 ] . set_threshold ( t0) ;
12931324 if let Some ( t1) = t1 {
12941325 self . generations [ 1 ] . set_threshold ( t1) ;
@@ -1317,11 +1348,30 @@ impl GcInterpreterState {
13171348 gc_state ( ) . collect_inner ( self , generation, true )
13181349 }
13191350
1351+ /// Keep the existing collector exclusion active through Python callbacks.
1352+ pub ( crate ) fn collect_force_with_callbacks (
1353+ & self ,
1354+ generation : usize ,
1355+ before : impl FnOnce ( ) ,
1356+ after : impl FnOnce ( & CollectResult ) ,
1357+ ) -> CollectResult {
1358+ gc_state ( ) . collect_with_callbacks ( self , generation, true , before, after)
1359+ }
1360+
13201361 /// The tracked objects this interpreter can reach (for gc.get_objects).
13211362 pub fn get_objects ( & self , generation : Option < i32 > ) -> Vec < PyObjectRef > {
13221363 gc_state ( ) . get_objects ( generation, self . owner )
13231364 }
13241365
1366+ /// Public introspection excludes permanent objects, unlike shutdown's
1367+ /// internal snapshot, which must still be able to reach frozen objects.
1368+ pub ( crate ) fn get_objects_for_introspection (
1369+ & self ,
1370+ generation : Option < i32 > ,
1371+ ) -> Vec < PyObjectRef > {
1372+ gc_state ( ) . get_objects_impl ( generation, self . owner , false )
1373+ }
1374+
13251375 /// Move the objects this interpreter could collect into the permanent
13261376 /// generation.
13271377 pub fn freeze ( & self ) {
@@ -1506,6 +1556,9 @@ mod tests {
15061556 first. enter ( |vm| {
15071557 vm. state . gc . set_threshold ( 1 , None , None ) ;
15081558 assert ! ( !allocations. maybe_collect( & vm. state. gc) ) ;
1559+ assert ! ( !vm. state. gc. scheduled. load( Ordering :: Relaxed ) ) ;
1560+ allocations. counts [ 0 ] . store ( 2 , Ordering :: Relaxed ) ;
1561+ assert ! ( !allocations. maybe_collect( & vm. state. gc) ) ;
15091562 } ) ;
15101563 second. enter ( |vm| {
15111564 assert ! ( !vm. state. gc. scheduled. load( Ordering :: Relaxed ) ) ;
0 commit comments