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

(Experimental) Join GC threads on the binding side. #54

Closed
wants to merge 6 commits into from
Closed
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
66 changes: 32 additions & 34 deletions mmtk/Cargo.lock

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

6 changes: 3 additions & 3 deletions mmtk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ probe = "0.5"
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"
#git = "https://github.com/wks/mmtk-core.git"
#rev = "7cb0b2c12be341c084e03f2cd943fad8ac088f83"

# Uncomment the following line to use mmtk-core from a local repository.
#path = "../../mmtk-core"
path = "../../mmtk-core"

[features]
default = []
Expand Down
9 changes: 2 additions & 7 deletions mmtk/src/abi.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::api::RubyMutator;
use crate::{upcalls, Ruby};
use mmtk::scheduler::{GCController, GCWorker};
use mmtk::scheduler::GCWorker;
use mmtk::util::{Address, ObjectReference, VMMutatorThread, VMWorkerThread};

// For the C binding
pub const OBJREF_OFFSET: usize = 8;
pub const MIN_OBJ_ALIGN: usize = 8; // Even on 32-bit machine. A Ruby object is at least 40 bytes large.

pub const GC_THREAD_KIND_CONTROLLER: libc::c_int = 0;
pub const GC_THREAD_KIND_WORKER: libc::c_int = 1;

const HAS_MOVED_GIVTBL: usize = 1 << 63;
Expand Down Expand Up @@ -244,10 +243,6 @@ impl GCThreadTLS {
}
}

pub fn for_controller(gc_context: *mut GCController<Ruby>) -> Self {
Self::new(GC_THREAD_KIND_CONTROLLER, gc_context as *mut libc::c_void)
}

pub fn for_worker(gc_context: *mut GCWorker<Ruby>) -> Self {
Self::new(GC_THREAD_KIND_WORKER, gc_context as *mut libc::c_void)
}
Expand All @@ -266,7 +261,7 @@ impl GCThreadTLS {
let result = &mut *ptr;
debug_assert!({
let kind = result.kind;
kind == GC_THREAD_KIND_CONTROLLER || kind == GC_THREAD_KIND_WORKER
kind == GC_THREAD_KIND_WORKER
});
result
}
Expand Down
11 changes: 11 additions & 0 deletions mmtk/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ pub extern "C" fn mmtk_initialize_collection(tls: VMThread) {
memory_manager::initialize_collection(mmtk(), tls)
}

#[no_mangle]
pub extern "C" fn mmtk_prepare_to_fork() {
mmtk().prepare_to_fork();
binding().join_all_gc_threads();
}

#[no_mangle]
pub extern "C" fn mmtk_after_fork(tls: VMThread) {
mmtk().after_fork(tls);
}

#[no_mangle]
pub extern "C" fn mmtk_enable_collection() {
memory_manager::enable_collection(mmtk())
Expand Down
19 changes: 19 additions & 0 deletions mmtk/src/binding.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::ffi::CString;
use std::sync::Mutex;
use std::thread::JoinHandle;

use libc::c_void;
use mmtk::util::ObjectReference;
Expand Down Expand Up @@ -36,6 +37,7 @@ pub struct RubyBinding {
pub weak_proc: WeakProcessor,
pub ppp_registry: PPPRegistry,
pub(crate) moved_givtbl: Mutex<HashMap<ObjectReference, MovedGIVTblEntry>>,
pub gc_thread_join_handles: Mutex<Vec<JoinHandle<()>>>
}

unsafe impl Sync for RubyBinding {}
Expand All @@ -58,6 +60,7 @@ impl RubyBinding {
weak_proc: WeakProcessor::new(),
ppp_registry: PPPRegistry::new(),
moved_givtbl: Default::default(),
gc_thread_join_handles: Default::default(),
}
}

Expand All @@ -75,4 +78,20 @@ impl RubyBinding {
}
plan_name.as_deref().unwrap().as_ptr()
}

pub fn join_all_gc_threads(&self) {
let handles = {
let mut guard = self.gc_thread_join_handles.lock().unwrap();
std::mem::take(&mut *guard)
};

debug!("Joining GC threads...");
let total = handles.len();
let mut joined = 0;
for handle in handles {
handle.join().unwrap();
joined += 1;
debug!("{joined}/{total} GC threads joined.");
}
}
}
Loading