Skip to content

Commit 0980bc6

Browse files
wksmmtkgc-bot
andauthored
Remove coordinator and support forking (#53)
Upstream PR: Remove coordinator and support forking --------- Co-authored-by: mmtkgc-bot <[email protected]>
1 parent cfb7e0d commit 0980bc6

File tree

6 files changed

+81
-72
lines changed

6 files changed

+81
-72
lines changed

mmtk/Cargo.lock

+17-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mmtk/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ edition = "2021"
1212
# Metadata for the Ruby repository
1313
[package.metadata.ci-repos.ruby]
1414
repo = "mmtk/ruby" # This is used by actions/checkout, so the format is "owner/repo", not URL.
15-
rev = "6d1596f7a4401d267d2b208c42521edc35020a8e "
15+
rev = "e173f879e6e4f1566cca83513e7bfeb4f39331a8"
1616

1717
[lib]
1818
name = "mmtk_ruby"
@@ -37,7 +37,7 @@ features = ["is_mmtk_object", "object_pinning"]
3737

3838
# Uncomment the following lines to use mmtk-core from the official repository.
3939
git = "https://github.com/mmtk/mmtk-core.git"
40-
rev = "dd84218933deab50196fc77828cc1446cb829881"
40+
rev = "5ab62f96c006475285b00b6d20a8b1bf83b74a4d"
4141

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

mmtk/src/abi.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use crate::api::RubyMutator;
22
use crate::{upcalls, Ruby};
3-
use mmtk::scheduler::{GCController, GCWorker};
3+
use mmtk::scheduler::GCWorker;
44
use mmtk::util::{Address, ObjectReference, VMMutatorThread, VMWorkerThread};
55

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

10-
pub const GC_THREAD_KIND_CONTROLLER: libc::c_int = 0;
1110
pub const GC_THREAD_KIND_WORKER: libc::c_int = 1;
1211

1312
const HAS_MOVED_GIVTBL: usize = 1 << 63;
@@ -244,10 +243,6 @@ impl GCThreadTLS {
244243
}
245244
}
246245

247-
pub fn for_controller(gc_context: *mut GCController<Ruby>) -> Self {
248-
Self::new(GC_THREAD_KIND_CONTROLLER, gc_context as *mut libc::c_void)
249-
}
250-
251246
pub fn for_worker(gc_context: *mut GCWorker<Ruby>) -> Self {
252247
Self::new(GC_THREAD_KIND_WORKER, gc_context as *mut libc::c_void)
253248
}
@@ -266,7 +261,7 @@ impl GCThreadTLS {
266261
let result = &mut *ptr;
267262
debug_assert!({
268263
let kind = result.kind;
269-
kind == GC_THREAD_KIND_CONTROLLER || kind == GC_THREAD_KIND_WORKER
264+
kind == GC_THREAD_KIND_WORKER
270265
});
271266
result
272267
}

mmtk/src/api.rs

+11
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ pub extern "C" fn mmtk_initialize_collection(tls: VMThread) {
156156
memory_manager::initialize_collection(mmtk(), tls)
157157
}
158158

159+
#[no_mangle]
160+
pub extern "C" fn mmtk_prepare_to_fork() {
161+
mmtk().prepare_to_fork();
162+
binding().join_all_gc_threads();
163+
}
164+
165+
#[no_mangle]
166+
pub extern "C" fn mmtk_after_fork(tls: VMThread) {
167+
mmtk().after_fork(tls);
168+
}
169+
159170
#[no_mangle]
160171
pub extern "C" fn mmtk_enable_collection() {
161172
BINDING_FAST.gc_enabled.store(true, Ordering::Relaxed);

mmtk/src/binding.rs

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::HashMap;
22
use std::ffi::CString;
33
use std::sync::atomic::AtomicBool;
44
use std::sync::Mutex;
5+
use std::thread::JoinHandle;
56

67
use libc::c_void;
78
use mmtk::util::ObjectReference;
@@ -50,6 +51,7 @@ pub struct RubyBinding {
5051
pub weak_proc: WeakProcessor,
5152
pub ppp_registry: PPPRegistry,
5253
pub(crate) moved_givtbl: Mutex<HashMap<ObjectReference, MovedGIVTblEntry>>,
54+
pub gc_thread_join_handles: Mutex<Vec<JoinHandle<()>>>,
5355
}
5456

5557
unsafe impl Sync for RubyBinding {}
@@ -72,6 +74,7 @@ impl RubyBinding {
7274
weak_proc: WeakProcessor::new(),
7375
ppp_registry: PPPRegistry::new(),
7476
moved_givtbl: Default::default(),
77+
gc_thread_join_handles: Default::default(),
7578
}
7679
}
7780

@@ -89,4 +92,20 @@ impl RubyBinding {
8992
}
9093
plan_name.as_deref().unwrap().as_ptr()
9194
}
95+
96+
pub fn join_all_gc_threads(&self) {
97+
let handles = {
98+
let mut guard = self.gc_thread_join_handles.lock().unwrap();
99+
std::mem::take(&mut *guard)
100+
};
101+
102+
debug!("Joining GC threads...");
103+
let total = handles.len();
104+
let mut joined = 0;
105+
for handle in handles {
106+
handle.join().unwrap();
107+
joined += 1;
108+
debug!("{joined}/{total} GC threads joined.");
109+
}
110+
}
92111
}

mmtk/src/collection.rs

+30-46
Original file line numberDiff line numberDiff line change
@@ -37,53 +37,37 @@ impl Collection<Ruby> for VMCollection {
3737
}
3838

3939
fn spawn_gc_thread(_tls: VMThread, ctx: GCThreadContext<Ruby>) {
40-
match ctx {
41-
GCThreadContext::Controller(mut controller) => {
42-
thread::Builder::new()
43-
.name("MMTk Controller Thread".to_string())
44-
.spawn(move || {
45-
debug!("Hello! This is MMTk Controller Thread running!");
46-
crate::register_gc_thread(thread::current().id());
47-
let ptr_controller = &mut *controller as *mut GCController<Ruby>;
48-
let gc_thread_tls =
49-
Box::into_raw(Box::new(GCThreadTLS::for_controller(ptr_controller)));
50-
(upcalls().init_gc_worker_thread)(gc_thread_tls);
51-
memory_manager::start_control_collector(
52-
mmtk(),
53-
GCThreadTLS::to_vwt(gc_thread_tls),
54-
&mut controller,
55-
);
40+
let join_handle = match ctx {
41+
GCThreadContext::Worker(mut worker) => thread::Builder::new()
42+
.name("MMTk Worker Thread".to_string())
43+
.spawn(move || {
44+
let ordinal = worker.ordinal;
45+
debug!(
46+
"Hello! This is MMTk Worker Thread running! ordinal: {}",
47+
ordinal
48+
);
49+
crate::register_gc_thread(thread::current().id());
50+
let ptr_worker = &mut *worker as *mut GCWorker<Ruby>;
51+
let gc_thread_tls =
52+
Box::into_raw(Box::new(GCThreadTLS::for_worker(ptr_worker)));
53+
(upcalls().init_gc_worker_thread)(gc_thread_tls);
54+
memory_manager::start_worker(
55+
mmtk(),
56+
GCThreadTLS::to_vwt(gc_thread_tls),
57+
worker,
58+
);
59+
debug!(
60+
"An MMTk Worker Thread is quitting. Good bye! ordinal: {}",
61+
ordinal
62+
);
63+
crate::unregister_gc_thread(thread::current().id());
64+
})
65+
.unwrap(),
66+
};
5667

57-
// Currently the MMTk controller thread should run forever.
58-
// This is an unlikely event, but we log it anyway.
59-
warn!("The MMTk Controller Thread is quitting!");
60-
crate::unregister_gc_thread(thread::current().id());
61-
})
62-
.unwrap();
63-
}
64-
GCThreadContext::Worker(mut worker) => {
65-
thread::Builder::new()
66-
.name("MMTk Worker Thread".to_string())
67-
.spawn(move || {
68-
debug!("Hello! This is MMTk Worker Thread running!");
69-
crate::register_gc_thread(thread::current().id());
70-
let ptr_worker = &mut *worker as *mut GCWorker<Ruby>;
71-
let gc_thread_tls =
72-
Box::into_raw(Box::new(GCThreadTLS::for_worker(ptr_worker)));
73-
(upcalls().init_gc_worker_thread)(gc_thread_tls);
74-
memory_manager::start_worker(
75-
mmtk(),
76-
GCThreadTLS::to_vwt(gc_thread_tls),
77-
&mut worker,
78-
);
79-
80-
// Currently all MMTk worker threads should run forever.
81-
// This is an unlikely event, but we log it anyway.
82-
warn!("An MMTk Worker Thread is quitting!");
83-
crate::unregister_gc_thread(thread::current().id());
84-
})
85-
.unwrap();
86-
}
68+
{
69+
let mut handles = crate::binding().gc_thread_join_handles.lock().unwrap();
70+
handles.push(join_handle);
8771
}
8872
}
8973

0 commit comments

Comments
 (0)