Skip to content
Merged
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
1 change: 1 addition & 0 deletions gc/mmtk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "mmtk_ruby"
version = "0.1.0"
authors = []
edition = "2024"
rust-version = "1.95"

[lib]
name = "mmtk_ruby"
Expand Down
15 changes: 2 additions & 13 deletions gc/mmtk/mmtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#endif

struct objspace {
bool user_gc_disabled;
bool measure_gc_time;
bool gc_stress;

Expand Down Expand Up @@ -839,23 +838,13 @@ rb_gc_impl_gc_disable(void *objspace_ptr, bool finish_current_gc)
bool
rb_gc_impl_user_gc_disabled_set(void *objspace_ptr, bool disable)
{
struct objspace *objspace = objspace_ptr;
const bool was = objspace->user_gc_disabled;
objspace->user_gc_disabled = disable;

if (was != disable) {
mmtk_set_gc_enabled(!disable);
objspace->user_gc_disabled = disable;
}

return was;
return mmtk_set_user_gc_disabled(disable);
}

bool
rb_gc_impl_user_gc_disabled_p(void *objspace_ptr)
{
struct objspace *objspace = objspace_ptr;
return objspace->user_gc_disabled;
return mmtk_user_gc_disabled_p();
}

bool
Expand Down
4 changes: 4 additions & 0 deletions gc/mmtk/mmtk.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ void mmtk_set_gc_enabled(bool enable);

bool mmtk_gc_enabled_p(void);

bool mmtk_set_user_gc_disabled(bool disable);

bool mmtk_user_gc_disabled_p(void);

size_t mmtk_max_non_los_default_alloc_bytes(void);

bool mmtk_plan_has_write_barrier(void);
Expand Down
55 changes: 17 additions & 38 deletions gc/mmtk/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ pub extern "C" fn mmtk_get_vo_bit_base_addr() -> usize {

#[unsafe(no_mangle)]
pub extern "C" fn mmtk_initialize_collection(tls: VMThread) {
memory_manager::initialize_collection(mmtk(), tls)
memory_manager::initialize_collection(mmtk(), tls);
crate::binding().gc_disable.on_collection_initialized();
}

#[unsafe(no_mangle)]
Expand Down Expand Up @@ -292,51 +293,29 @@ pub extern "C" fn mmtk_handle_user_collection_request(
force: bool,
exhaustive: bool,
) {
// GC.start must run even when GC is disabled. However, GC.stress=true does
// not run when GC is disabled. force is true when GC.start and false when
// GC.stress=true.
// TODO: This is not Ractor-safe. Another Ractor could disable GC, which
// would prevent the GC from running. Another Ractor could also enable
// GC, which would be lost.
let gc_was_disabled = force && !crate::mmtk().is_collection_enabled();
if gc_was_disabled {
crate::mmtk().enable_collection();
}

crate::mmtk().handle_user_collection_request(tls, force, exhaustive);

if gc_was_disabled {
// While GC was enabled, another Ractor may have requested a GC that hasn't run yet.
// MMTk can't disable GC until that GC runs, and it needs this thread to stop first.
// So block for it and try again. Since a GC is already requested, this waits for that
// GC instead of starting a new one.
while crate::mmtk().disable_collection().is_err() {
crate::mmtk().handle_user_collection_request(tls, true, false);
}
}
crate::binding()
.gc_disable
.handle_user_collection_request(tls, force, exhaustive);
}

#[unsafe(no_mangle)]
pub extern "C" fn mmtk_set_gc_enabled(enable: bool) {
if enable {
crate::mmtk().enable_collection();
} else if crate::mmtk().is_collection_enabled() {
// We must not call disable_collection if GC is already disabled because
// MMTk keeps track of the depth of disable_collection. This would require
// enable_collection calls equal to the number of disable_collection calls
// before GC is enabled.

// We cannot panic on failing to disable GC because
// DURING_GC_COULD_MALLOC_REGION_START disables GC during GC. It's
// fine to ignore the failure because MMTk's malloc does not trigger
// GC.
let _ = crate::mmtk().disable_collection();
}
crate::binding().gc_disable.set_internal_disabled(!enable);
}

#[unsafe(no_mangle)]
pub extern "C" fn mmtk_gc_enabled_p() -> bool {
crate::mmtk().is_collection_enabled()
!crate::binding().gc_disable.internal_disabled()
}

#[unsafe(no_mangle)]
pub extern "C" fn mmtk_set_user_gc_disabled(disable: bool) -> bool {
crate::binding().gc_disable.set_user_disabled(disable)
}

#[unsafe(no_mangle)]
pub extern "C" fn mmtk_user_gc_disabled_p() -> bool {
crate::binding().gc_disable.user_disabled()
}

// =============== Object allocation ===============
Expand Down
3 changes: 3 additions & 0 deletions gc/mmtk/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use mmtk::util::ObjectReference;

use crate::Ruby;
use crate::abi::RubyBindingOptions;
use crate::gc_disable::GcDisableState;
use crate::pinning_registry::PinningRegistry;
use crate::weak_proc::WeakProcessor;

Expand Down Expand Up @@ -35,6 +36,7 @@ pub struct RubyBinding {
pub pinning_registry: PinningRegistry,
pub gc_thread_join_handles: Mutex<Vec<JoinHandle<()>>>,
pub wb_unprotected_objects: Mutex<HashSet<ObjectReference>>,
pub gc_disable: GcDisableState,
}

unsafe impl Sync for RubyBinding {}
Expand All @@ -54,6 +56,7 @@ impl RubyBinding {
pinning_registry: PinningRegistry::new(),
gc_thread_join_handles: Default::default(),
wb_unprotected_objects: Default::default(),
gc_disable: Default::default(),
}
}

Expand Down
3 changes: 3 additions & 0 deletions gc/mmtk/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl Collection<Ruby> for VMCollection {
F: FnMut(&'static mut mmtk::Mutator<Ruby>),
{
upcalls::stop_the_world();
crate::binding().gc_disable.on_pause_start();

if crate::mmtk().get_plan().current_gc_may_move_object() {
CURRENT_GC_MAY_MOVE.store(true, Ordering::Relaxed);
Expand All @@ -47,6 +48,8 @@ impl Collection<Ruby> for VMCollection {
}

fn resume_mutators(_tls: VMWorkerThread) {
crate::binding().gc_disable.on_pause_end();

let current_gc_may_move = CURRENT_GC_MAY_MOVE.load(Ordering::Relaxed);

if current_gc_may_move {
Expand Down
Loading
Loading