Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions oscars/src/collectors/mark_sweep/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ impl MarkSweepGarbageCollector {
pub fn pools_len(&self) -> usize {
self.allocator.borrow().pools_len()
}

/// Returns true when the collector is not inside an active collection
/// cycle, i.e. it is safe to run external finalizer-sensitive paths.
pub fn finalizer_safe(&self) -> bool {
!self.is_collecting.get()
}
}

impl Drop for MarkSweepGarbageCollector {
Expand Down
54 changes: 54 additions & 0 deletions oscars/src/collectors/mark_sweep/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,60 @@ mod gc_edge_cases {
let _value = *flag.borrow();
}

#[test]
fn finalizer_safe_reflects_collecting_state() {
use core::sync::atomic::{AtomicU8, Ordering};

// 0 = not observed, 1 = true, 2 = false
static OBSERVED: AtomicU8 = AtomicU8::new(0);

struct Probe {
collector: core::ptr::NonNull<MarkSweepGarbageCollector>,
}

impl Finalize for Probe {}

unsafe impl Trace for Probe {
unsafe fn trace(&self, _color: crate::mark_sweep::TraceColor) {
let safe = unsafe { self.collector.as_ref().finalizer_safe() };
OBSERVED.store(if safe { 1 } else { 2 }, Ordering::SeqCst);
}

fn run_finalizer(&self) {
Finalize::finalize(self);
}
}

let collector = &mut MarkSweepGarbageCollector::default()
.with_page_size(256)
.with_heap_threshold(512);

assert!(
collector.finalizer_safe(),
"collector should report finalizer-safe while idle"
);

OBSERVED.store(0, Ordering::SeqCst);
let _root = Gc::new_in(
Probe {
collector: core::ptr::NonNull::from(&*collector),
},
collector,
);

collector.collect();

assert_eq!(
OBSERVED.load(Ordering::SeqCst),
2,
"trace-time observation should see finalizer_safe() == false during collection"
);
assert!(
collector.finalizer_safe(),
"collector should return to finalizer-safe state after collection"
);
}

// ---- Multiple collections on the same graph ---------------------------

/// Run GC repeatedly while objects are still alive to verify that
Expand Down
6 changes: 6 additions & 0 deletions oscars/src/collectors/mark_sweep_arena2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ impl MarkSweepGarbageCollector {
pub fn arenas_len(&self) -> usize {
self.allocator.borrow().arenas_len()
}

/// Returns true when the collector is not inside an active collection
/// cycle, i.e. it is safe to run external finalizer-sensitive paths.
pub fn finalizer_safe(&self) -> bool {
!self.is_collecting.get()
}
}

impl Drop for MarkSweepGarbageCollector {
Expand Down
54 changes: 54 additions & 0 deletions oscars/src/collectors/mark_sweep_arena2/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,60 @@ mod gc_edge_cases {
let _value = *flag.borrow();
}

#[test]
fn finalizer_safe_reflects_collecting_state() {
use core::sync::atomic::{AtomicU8, Ordering};

// 0 = not observed, 1 = true, 2 = false
static OBSERVED: AtomicU8 = AtomicU8::new(0);

struct Probe {
collector: core::ptr::NonNull<MarkSweepGarbageCollector>,
}

impl Finalize for Probe {}

unsafe impl Trace for Probe {
unsafe fn trace(&self, _color: TraceColor) {
let safe = unsafe { self.collector.as_ref().finalizer_safe() };
OBSERVED.store(if safe { 1 } else { 2 }, Ordering::SeqCst);
}

fn run_finalizer(&self) {
Finalize::finalize(self);
}
}

let collector = &mut MarkSweepGarbageCollector::default()
.with_arena_size(256)
.with_heap_threshold(512);

assert!(
collector.finalizer_safe(),
"collector should report finalizer-safe while idle"
);

OBSERVED.store(0, Ordering::SeqCst);
let _root = Gc::new_in(
Probe {
collector: core::ptr::NonNull::from(&*collector),
},
collector,
);

collector.collect();

assert_eq!(
OBSERVED.load(Ordering::SeqCst),
2,
"trace-time observation should see finalizer_safe() == false during collection"
);
assert!(
collector.finalizer_safe(),
"collector should return to finalizer-safe state after collection"
);
}

// ---- Multiple collections on the same graph ---------------------------

/// Run GC repeatedly while objects are still alive to verify that
Expand Down