Skip to content

Commit 277f129

Browse files
committed
Fix GC controller thread context
Following an upstream change, the GC controller thread now has a context, too.
1 parent e0f5b0c commit 277f129

File tree

8 files changed

+58
-31
lines changed

8 files changed

+58
-31
lines changed

mmtk/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ lazy_static = "1.1"
2020
# - change branch
2121
# - change repo name
2222
# But other changes including adding/removing whitespaces in commented lines may break the CI.
23-
mmtk = { git = "https://github.com/mmtk/mmtk-core.git", rev = "4ebb6b68f54a55bf8794ea01db3f2d0f28d15f4d" }
23+
#mmtk = { git = "https://github.com/mmtk/mmtk-core.git", rev = "4ebb6b68f54a55bf8794ea01db3f2d0f28d15f4d" }
2424
# Uncomment the following to build locally
2525
# mmtk = { path = "../repos/mmtk-core" }
26+
mmtk = { git = "https://github.com/wks/mmtk-core.git", branch = "refactor-scheduler" }
27+
2628

2729
[features]
2830
default = []

mmtk/src/api.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::UPCALLS;
55
use libc::c_char;
66
use mmtk::memory_manager;
77
use mmtk::plan::BarrierSelector;
8+
use mmtk::scheduler::GCController;
89
use mmtk::scheduler::GCWorker;
910
use mmtk::util::alloc::AllocatorSelector;
1011
use mmtk::util::opaque_pointer::*;
@@ -50,8 +51,14 @@ pub extern "C" fn openjdk_gc_init(calls: *const OpenJDK_Upcalls, heap_size: usiz
5051
}
5152

5253
#[no_mangle]
53-
pub extern "C" fn start_control_collector(tls: VMWorkerThread) {
54-
memory_manager::start_control_collector(&SINGLETON, tls);
54+
// We trust the gc_collector pointer is valid.
55+
#[allow(clippy::not_unsafe_ptr_arg_deref)]
56+
pub extern "C" fn start_control_collector(
57+
tls: VMWorkerThread,
58+
gc_controller: *mut GCController<OpenJDK>,
59+
) {
60+
let mut gc_controller = unsafe { Box::from_raw(gc_controller) };
61+
memory_manager::start_control_collector(tls, &mut gc_controller, &SINGLETON);
5562
}
5663

5764
#[no_mangle]

mmtk/src/collection.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use mmtk::scheduler::{GCWorker, WorkBucketStage};
1+
use mmtk::scheduler::WorkBucketStage;
22
use mmtk::scheduler::{ProcessEdgesWork, ScanStackRoot};
33
use mmtk::util::opaque_pointer::*;
4-
use mmtk::vm::{Collection, Scanning, VMBinding};
4+
use mmtk::vm::{Collection, GCThreadContext, Scanning, VMBinding};
55
use mmtk::{Mutator, MutatorContext};
66

77
use crate::OpenJDK;
@@ -19,6 +19,9 @@ extern "C" fn create_mutator_scan_work<E: ProcessEdgesWork<VM = OpenJDK>>(
1919
);
2020
}
2121

22+
const GC_THREAD_KIND_CONTROLLER: libc::c_int = 0;
23+
const GC_THREAD_KIND_WORKER: libc::c_int = 1;
24+
2225
impl Collection<OpenJDK> for VMCollection {
2326
/// With the presence of the "VM companion thread",
2427
/// the OpenJDK binding allows any MMTk GC thread to stop/start the world.
@@ -49,14 +52,18 @@ impl Collection<OpenJDK> for VMCollection {
4952
}
5053
}
5154

52-
fn spawn_worker_thread(tls: VMThread, ctx: Option<Box<GCWorker<OpenJDK>>>) {
53-
let ctx_ptr = if let Some(r) = ctx {
54-
Box::into_raw(r)
55-
} else {
56-
std::ptr::null_mut()
55+
fn spawn_gc_thread(tls: VMThread, ctx: GCThreadContext<OpenJDK>) {
56+
let (ctx_ptr, kind) = match ctx {
57+
GCThreadContext::Controller(c) => (
58+
Box::into_raw(c) as *mut libc::c_void,
59+
GC_THREAD_KIND_CONTROLLER,
60+
),
61+
GCThreadContext::Worker(w) => {
62+
(Box::into_raw(w) as *mut libc::c_void, GC_THREAD_KIND_WORKER)
63+
}
5764
};
5865
unsafe {
59-
((*UPCALLS).spawn_worker_thread)(tls, ctx_ptr as usize as _);
66+
((*UPCALLS).spawn_gc_thread)(tls, kind, ctx_ptr);
6067
}
6168
}
6269

mmtk/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ extern crate lazy_static;
1313
use std::ptr::null_mut;
1414

1515
use libc::{c_char, c_void, uintptr_t};
16-
use mmtk::scheduler::GCWorker;
1716
use mmtk::util::opaque_pointer::*;
1817
use mmtk::util::{Address, ObjectReference};
1918
use mmtk::vm::VMBinding;
@@ -45,7 +44,7 @@ pub struct OpenJDK_Upcalls {
4544
create_stack_scan_work: *const extern "C" fn(&'static mut Mutator<OpenJDK>),
4645
),
4746
pub resume_mutators: extern "C" fn(tls: VMWorkerThread),
48-
pub spawn_worker_thread: extern "C" fn(tls: VMThread, ctx: *mut GCWorker<OpenJDK>),
47+
pub spawn_gc_thread: extern "C" fn(tls: VMThread, kind: libc::c_int, ctx: *mut libc::c_void),
4948
pub block_for_gc: extern "C" fn(),
5049
pub get_next_mutator: extern "C" fn() -> *mut Mutator<OpenJDK>,
5150
pub reset_mutator_iterator: extern "C" fn(),

openjdk/mmtk.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ extern bool process_bulk(char* options);
7878
extern void scan_region();
7979
extern void handle_user_collection_request(void *tls);
8080

81-
extern void start_control_collector(void *tls);
81+
extern void start_control_collector(void *tls, void *context);
8282
extern void start_worker(void *tls, void* worker);
8383

8484
/**
@@ -100,7 +100,7 @@ typedef NewBuffer (*ProcessEdgesFn)(void** buf, size_t len, size_t cap);
100100
typedef struct {
101101
void (*stop_all_mutators) (void *tls, void (*create_stack_scan_work)(void* mutator));
102102
void (*resume_mutators) (void *tls);
103-
void (*spawn_collector_thread) (void *tls, void *ctx);
103+
void (*spawn_collector_thread) (void *tls, int kind, void *ctx);
104104
void (*block_for_gc) ();
105105
void* (*get_next_mutator) ();
106106
void (*reset_mutator_iterator) ();

openjdk/mmtkContextThread.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
#include "mmtk.h"
2727
#include "mmtkContextThread.hpp"
2828

29-
MMTkContextThread::MMTkContextThread() : NamedThread() {
29+
MMTkContextThread::MMTkContextThread(void *context) : NamedThread(), context_(context) {
3030
set_name("MMTk Controller Context Thread");
3131
}
3232

3333
void MMTkContextThread::run() {
3434
this->initialize_named_thread();
35-
start_control_collector((void*) this);
35+
start_control_collector((void*) this, context_);
3636
}

openjdk/mmtkContextThread.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
#include "runtime/thread.hpp"
3030

3131
class MMTkContextThread: public NamedThread {
32+
void* context_;
3233
public:
3334
// Constructor
34-
MMTkContextThread();
35+
MMTkContextThread(void* context);
3536

3637
// No destruction allowed
3738
~MMTkContextThread() {

openjdk/mmtkUpcalls.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,33 @@ static void mmtk_resume_mutators(void *tls) {
8686
log_debug(gc)("Mutators notified.");
8787
}
8888

89-
static void mmtk_spawn_collector_thread(void* tls, void* ctx) {
90-
if (ctx == NULL) {
91-
MMTkContextThread* t = new MMTkContextThread();
92-
if (!os::create_thread(t, os::pgc_thread)) {
93-
printf("Failed to create thread");
94-
guarantee(false, "panic");
89+
static const int GC_THREAD_KIND_CONTROLLER = 0;
90+
static const int GC_THREAD_KIND_WORKER = 1;
91+
static void mmtk_spawn_collector_thread(void* tls, int kind, void* ctx) {
92+
switch (kind) {
93+
case GC_THREAD_KIND_CONTROLLER: {
94+
MMTkContextThread* t = new MMTkContextThread(ctx);
95+
if (!os::create_thread(t, os::pgc_thread)) {
96+
printf("Failed to create thread");
97+
guarantee(false, "panic");
98+
}
99+
os::start_thread(t);
100+
break;
101+
}
102+
case GC_THREAD_KIND_WORKER: {
103+
MMTkHeap::heap()->new_collector_thread();
104+
MMTkCollectorThread* t = new MMTkCollectorThread(ctx);
105+
if (!os::create_thread(t, os::pgc_thread)) {
106+
printf("Failed to create thread");
107+
guarantee(false, "panic");
108+
}
109+
os::start_thread(t);
110+
break;
95111
}
96-
os::start_thread(t);
97-
} else {
98-
MMTkHeap::heap()->new_collector_thread();
99-
MMTkCollectorThread* t = new MMTkCollectorThread(ctx);
100-
if (!os::create_thread(t, os::pgc_thread)) {
101-
printf("Failed to create thread");
112+
default: {
113+
printf("Unexpected thread kind: %d\n", kind);
102114
guarantee(false, "panic");
103115
}
104-
os::start_thread(t);
105116
}
106117
}
107118

0 commit comments

Comments
 (0)