Skip to content

Commit

Permalink
Fix GC controller thread context (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
qinsoon authored Feb 3, 2022
1 parent f49bb41 commit 69c672b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion mmtk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ log = "*"
# - change branch
# - change repo name
# But other changes including adding/removing whitespaces in commented lines may break the CI.
mmtk = { git = "https://github.com/mmtk/mmtk-core.git", rev = "4ebb6b68f54a55bf8794ea01db3f2d0f28d15f4d" }
mmtk = { git = "https://github.com/mmtk/mmtk-core.git", rev = "29afb0c2fc8896db95e967062a892d39589bae4d" }
# Uncomment the following and fix the path to mmtk-core to build locally
# mmtk = { path = "../repos/mmtk-core" }

Expand Down
21 changes: 14 additions & 7 deletions mmtk/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libc::c_char;
use libc::c_void;
use mmtk::memory_manager;
use mmtk::scheduler::GCWorker;
use mmtk::scheduler::{GCController, GCWorker};
use mmtk::util::opaque_pointer::*;
use mmtk::util::{Address, ObjectReference};
use mmtk::AllocationSemantics;
Expand All @@ -25,11 +25,6 @@ pub extern "C" fn v8_new_heap(calls: *const V8_Upcalls, heap_size: usize) -> *mu
mmtk as *mut c_void
}

#[no_mangle]
pub extern "C" fn start_control_collector(mmtk: &mut MMTK<V8>, tls: VMWorkerThread) {
memory_manager::start_control_collector(&*mmtk, tls);
}

#[no_mangle]
pub extern "C" fn bind_mutator(
mmtk: &'static mut MMTK<V8>,
Expand Down Expand Up @@ -71,6 +66,18 @@ pub extern "C" fn will_never_move(object: ObjectReference) -> bool {
!object.is_movable()
}

#[no_mangle]
// We trust the worker pointer is valid.
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn start_control_collector(
mmtk: &'static mut MMTK<V8>,
tls: VMWorkerThread,
gc_controller: *mut GCController<V8>,
) {
let mut gc_controller = unsafe { Box::from_raw(gc_controller) };
memory_manager::start_control_collector(&*mmtk, tls, &mut gc_controller);
}

#[no_mangle]
// We trust the worker pointer is valid.
#[allow(clippy::not_unsafe_ptr_arg_deref)]
Expand All @@ -80,7 +87,7 @@ pub extern "C" fn start_worker(
worker: *mut GCWorker<V8>,
) {
let mut worker = unsafe { Box::from_raw(worker) };
memory_manager::start_worker::<V8>(tls, &mut worker, mmtk);
memory_manager::start_worker::<V8>(mmtk, tls, &mut worker);
}

#[no_mangle]
Expand Down
22 changes: 14 additions & 8 deletions mmtk/src/collection.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use mmtk::scheduler::GCWorker;
use mmtk::scheduler::ProcessEdgesWork;
use mmtk::util::opaque_pointer::*;
use mmtk::vm::Collection;
use mmtk::vm::{Collection, GCThreadContext};
use mmtk::MutatorContext;

use UPCALLS;
use V8;

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

pub struct VMCollection {}

impl Collection<V8> for VMCollection {
Expand All @@ -28,14 +30,18 @@ impl Collection<V8> for VMCollection {
}
}

fn spawn_worker_thread(tls: VMThread, ctx: Option<Box<GCWorker<V8>>>) {
let ctx_ptr = if let Some(r) = ctx {
Box::into_raw(r)
} else {
std::ptr::null_mut()
fn spawn_gc_thread(tls: VMThread, ctx: GCThreadContext<V8>) {
let (ctx_ptr, kind) = match ctx {
GCThreadContext::Controller(b) => (
Box::into_raw(b) as *mut libc::c_void,
GC_THREAD_KIND_CONTROLLER,
),
GCThreadContext::Worker(b) => {
(Box::into_raw(b) as *mut libc::c_void, GC_THREAD_KIND_WORKER)
}
};
unsafe {
((*UPCALLS).spawn_worker_thread)(tls, ctx_ptr as usize as _);
((*UPCALLS).spawn_gc_thread)(tls, kind, ctx_ptr);
}
}

Expand Down
3 changes: 1 addition & 2 deletions mmtk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ extern crate log;
use std::ptr::null_mut;

use libc::c_void;
use mmtk::scheduler::GCWorker;
use mmtk::util::opaque_pointer::*;
use mmtk::util::ObjectReference;
use mmtk::vm::VMBinding;
Expand All @@ -27,7 +26,7 @@ pub mod scanning;
pub struct V8_Upcalls {
pub stop_all_mutators: extern "C" fn(tls: VMWorkerThread),
pub resume_mutators: extern "C" fn(tls: VMWorkerThread),
pub spawn_worker_thread: extern "C" fn(tls: VMThread, ctx: *mut GCWorker<V8>),
pub spawn_gc_thread: extern "C" fn(tls: VMThread, kind: libc::c_int, ctx: *mut libc::c_void),
pub block_for_gc: extern "C" fn(),
pub get_next_mutator: extern "C" fn() -> *mut Mutator<V8>,
pub reset_mutator_iterator: extern "C" fn(),
Expand Down
4 changes: 2 additions & 2 deletions v8/third_party/heap/mmtk/mmtk.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extern bool process(void* mmtk, char* name, char* value);
extern void scan_region(void *mmtk);
extern void handle_user_collection_request(void *mmtk, void *tls);

extern void start_control_collector(void *tls);
extern void start_control_collector(void *tls, void* controller);
extern void start_worker(void *tls, void* worker);

/**
Expand All @@ -73,7 +73,7 @@ extern uint8_t tph_archive_obj_to_space(void* arch, void* obj_ptr);
typedef struct {
void (*stop_all_mutators) (void *tls);
void (*resume_mutators) (void *tls);
void (*spawn_collector_thread) (void *tls, void *ctx);
void (*spawn_collector_thread) (void *tls, int, void *ctx);
void (*block_for_gc) ();
void* (*get_next_mutator) ();
void (*reset_mutator_iterator) ();
Expand Down
2 changes: 1 addition & 1 deletion v8/third_party/heap/mmtk/mmtkUpcalls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static void mmtk_resume_mutators(void *tls) {
UNIMPLEMENTED();
}

static void mmtk_spawn_collector_thread(void* tls, void* ctx) {
static void mmtk_spawn_collector_thread(void* tls, int kind, void* ctx) {
UNIMPLEMENTED();
}

Expand Down

0 comments on commit 69c672b

Please sign in to comment.