Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ask from binding if GC is disabled #52

Merged
merged 3 commits into from
Feb 7, 2024
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
68 changes: 34 additions & 34 deletions mmtk/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mmtk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ features = ["is_mmtk_object", "object_pinning"]

# Uncomment the following lines to use mmtk-core from the official repository.
git = "https://github.com/mmtk/mmtk-core.git"
rev = "42754a5f23fdb513039d7f07e52014ded42c98bf"
rev = "7cafe560139211c0e3a74815d2e288278506099d"

# Uncomment the following line to use mmtk-core from a local repository.
#path = "../../mmtk-core"
Expand Down
2 changes: 1 addition & 1 deletion mmtk/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl RubyObjectAccess {

pub fn suffix_size() -> usize {
// In RACTOR_CHECK_MODE, Ruby hides a field after each object to hold the Ractor ID.
unsafe { crate::BINDING_FAST.suffix_size }
unsafe { crate::BINDING_FAST_MUT.suffix_size }
}

pub fn object_size(&self) -> usize {
Expand Down
6 changes: 4 additions & 2 deletions mmtk/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![allow(clippy::not_unsafe_ptr_arg_deref)]

use std::ffi::CStr;
use std::sync::atomic::Ordering;

use crate::abi;
use crate::abi::RawVecOfObjRef;
Expand All @@ -11,6 +12,7 @@ use crate::binding;
use crate::binding::RubyBinding;
use crate::mmtk;
use crate::Ruby;
use crate::BINDING_FAST;
use mmtk::memory_manager;
use mmtk::memory_manager::mmtk_init;
use mmtk::util::alloc::AllocatorInfo;
Expand Down Expand Up @@ -156,12 +158,12 @@ pub extern "C" fn mmtk_initialize_collection(tls: VMThread) {

#[no_mangle]
pub extern "C" fn mmtk_enable_collection() {
memory_manager::enable_collection(mmtk())
BINDING_FAST.gc_enabled.store(true, Ordering::Relaxed);
}

#[no_mangle]
pub extern "C" fn mmtk_disable_collection() {
memory_manager::disable_collection(mmtk())
BINDING_FAST.gc_enabled.store(false, Ordering::Relaxed);
}

#[no_mangle]
Expand Down
20 changes: 17 additions & 3 deletions mmtk/src/binding.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::ffi::CString;
use std::sync::atomic::AtomicBool;
use std::sync::Mutex;

use libc::c_void;
Expand All @@ -12,12 +13,25 @@ use crate::ppp::PPPRegistry;
use crate::weak_proc::WeakProcessor;
use crate::Ruby;

#[derive(Default)]
pub struct RubyBindingFast {
pub suffix_size: usize,
pub gc_enabled: AtomicBool,
}

impl RubyBindingFast {
pub const fn new() -> Self {
Self {
// Mimic the old behavior when the gc_enabled flag was in mmtk-core.
// We may refactor it so that it is false by default.
gc_enabled: AtomicBool::new(true),
}
}
}

pub struct RubyBindingFastMut {
pub suffix_size: usize,
}

impl RubyBindingFastMut {
pub const fn new() -> Self {
Self { suffix_size: 0 }
}
Expand Down Expand Up @@ -48,7 +62,7 @@ impl RubyBinding {
upcalls: *const abi::RubyUpcalls,
) -> Self {
unsafe {
crate::BINDING_FAST.suffix_size = binding_options.suffix_size;
crate::BINDING_FAST_MUT.suffix_size = binding_options.suffix_size;
}
Self {
mmtk,
Expand Down
5 changes: 5 additions & 0 deletions mmtk/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ use mmtk::memory_manager;
use mmtk::scheduler::*;
use mmtk::util::{VMMutatorThread, VMThread, VMWorkerThread};
use mmtk::vm::{Collection, GCThreadContext};
use std::sync::atomic::Ordering;
use std::thread;

pub struct VMCollection {}

impl Collection<Ruby> for VMCollection {
fn is_collection_enabled() -> bool {
crate::BINDING_FAST.gc_enabled.load(Ordering::Relaxed)
}

fn stop_all_mutators<F>(tls: VMWorkerThread, mut mutator_visitor: F)
where
F: FnMut(&'static mut mmtk::Mutator<Ruby>),
Expand Down
7 changes: 5 additions & 2 deletions mmtk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::sync::Mutex;
use std::thread::ThreadId;

use abi::RubyUpcalls;
use binding::{RubyBinding, RubyBindingFast};
use binding::{RubyBinding, RubyBindingFast, RubyBindingFastMut};
use mmtk::vm::edge_shape::{SimpleEdge, UnimplementedMemorySlice};
use mmtk::vm::VMBinding;
use mmtk::MMTK;
Expand Down Expand Up @@ -55,9 +55,12 @@ impl VMBinding for Ruby {
/// The singleton object for the Ruby binding itself.
pub static BINDING: OnceCell<RubyBinding> = OnceCell::new();

/// Some data needs to be accessed fast.
pub static BINDING_FAST: RubyBindingFast = RubyBindingFast::new();

/// Some data needs to be accessed fast.
/// We sacrifice safety for speed using unsynchronized global variables.
pub static mut BINDING_FAST: RubyBindingFast = RubyBindingFast::new();
pub static mut BINDING_FAST_MUT: RubyBindingFastMut = RubyBindingFastMut::new();

pub fn binding<'b>() -> &'b RubyBinding {
BINDING
Expand Down
Loading