Skip to content

Commit 57e2127

Browse files
authored
fix thread state transition in harness (#157)
Use `ThreadInVMfromNative` for thread state transition.
1 parent 3cc0d71 commit 57e2127

File tree

5 files changed

+29
-22
lines changed

5 files changed

+29
-22
lines changed

mmtk/src/api.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,29 @@ pub extern "C" fn add_phantom_candidate(reff: ObjectReference) {
195195

196196
// The harness_begin()/end() functions are different than other API functions in terms of the thread state.
197197
// Other functions are called by the VM, thus the thread should already be in the VM state. But the harness
198-
// functions are called by the probe, and the thread is in JNI/application/native state. Thus we need an extra call
199-
// to switch the thread state (enter_vm/leave_vm)
198+
// functions are called by the probe, and the thread is in JNI/application/native state. Thus we need call
199+
// into VM to switch the thread state and VM will then call into mmtk-core again to do the actual work of
200+
// harness_begin() and harness_end()
200201

201202
#[no_mangle]
202203
pub extern "C" fn harness_begin(_id: usize) {
203-
let state = unsafe { ((*UPCALLS).enter_vm)() };
204+
unsafe { ((*UPCALLS).harness_begin)() };
205+
}
206+
207+
#[no_mangle]
208+
pub extern "C" fn mmtk_harness_begin_impl() {
204209
// Pass null as tls, OpenJDK binding does not rely on the tls value to block the current thread and do a GC
205210
memory_manager::harness_begin(&SINGLETON, VMMutatorThread(VMThread::UNINITIALIZED));
206-
unsafe { ((*UPCALLS).leave_vm)(state) };
207211
}
208212

209213
#[no_mangle]
210214
pub extern "C" fn harness_end(_id: usize) {
211-
let state = unsafe { ((*UPCALLS).enter_vm)() };
215+
unsafe { ((*UPCALLS).harness_end)() };
216+
}
217+
218+
#[no_mangle]
219+
pub extern "C" fn mmtk_harness_end_impl() {
212220
memory_manager::harness_end(&SINGLETON);
213-
unsafe { ((*UPCALLS).leave_vm)(state) };
214221
}
215222

216223
#[no_mangle]

mmtk/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ pub struct OpenJDK_Upcalls {
5252
pub get_object_size: extern "C" fn(object: ObjectReference) -> usize,
5353
pub get_mmtk_mutator: extern "C" fn(tls: VMMutatorThread) -> *mut Mutator<OpenJDK>,
5454
pub is_mutator: extern "C" fn(tls: VMThread) -> bool,
55-
pub enter_vm: extern "C" fn() -> i32,
56-
pub leave_vm: extern "C" fn(st: i32),
55+
pub harness_begin: extern "C" fn(),
56+
pub harness_end: extern "C" fn(),
5757
pub compute_klass_mem_layout_checksum: extern "C" fn() -> usize,
5858
pub offset_of_static_fields: extern "C" fn() -> i32,
5959
pub static_oop_field_count_offset: extern "C" fn() -> i32,

openjdk/CompileThirdPartyHeap.gmk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ MMTK_RUST_ROOT = $(THIRD_PARTY_HEAP)/../mmtk
77
MMTK_CPP_ROOT = $(THIRD_PARTY_HEAP)
88

99
# Grab OpenJDK version specified in mmtk/Cargo.toml and local OpenJDK version
10-
OPENJDK_VERSION=`cd $(MMTK_RUST_ROOT) ; cargo read-manifest --manifest-path=Cargo.toml | python -c 'import json,sys; print(json.load(sys.stdin)["metadata"]["openjdk"]["openjdk_version"])'`
10+
OPENJDK_VERSION=`cd $(MMTK_RUST_ROOT) ; cargo read-manifest --manifest-path=Cargo.toml | python3 -c 'import json,sys; print(json.load(sys.stdin)["metadata"]["openjdk"]["openjdk_version"])'`
1111
OPENJDK_LOCAL_VERSION=`git rev-parse HEAD`
1212

1313
ifdef MMTK_PLAN

openjdk/mmtk.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ typedef struct {
119119
size_t (*get_object_size) (void* object);
120120
void* (*get_mmtk_mutator) (void* tls);
121121
bool (*is_mutator) (void* tls);
122-
int (*enter_vm) ();
123-
void (*leave_vm) (int st);
122+
void (*harness_begin) ();
123+
void (*harness_end) ();
124124
size_t (*compute_klass_mem_layout_checksum) ();
125125
int (*offset_of_static_fields) ();
126126
int (*static_oop_field_count_offset) ();
@@ -169,8 +169,8 @@ extern void add_weak_candidate(void* ref, void* referent);
169169
extern void add_soft_candidate(void* ref, void* referent);
170170
extern void add_phantom_candidate(void* ref, void* referent);
171171

172-
extern void harness_begin(void *tls);
173-
extern void harness_end();
172+
extern void mmtk_harness_begin_impl();
173+
extern void mmtk_harness_end_impl();
174174

175175
#ifdef __cplusplus
176176
}

openjdk/mmtkUpcalls.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,21 +243,21 @@ static size_t mmtk_get_object_size(void* object) {
243243
return klass->oop_size(o) << LogHeapWordSize;
244244
}
245245

246-
static int mmtk_enter_vm() {
246+
static void mmtk_harness_begin() {
247247
assert(Thread::current()->is_Java_thread(), "Only Java thread can enter vm");
248248

249249
JavaThread* current = ((JavaThread*) Thread::current());
250-
JavaThreadState state = current->thread_state();
251-
current->set_thread_state(_thread_in_vm);
252-
return (int)state;
250+
ThreadInVMfromNative tiv(current);
251+
mmtk_harness_begin_impl();
252+
253253
}
254254

255-
static void mmtk_leave_vm(int st) {
255+
static void mmtk_harness_end() {
256256
assert(Thread::current()->is_Java_thread(), "Only Java thread can leave vm");
257257

258258
JavaThread* current = ((JavaThread*) Thread::current());
259-
assert(current->thread_state() == _thread_in_vm, "Cannot leave vm when the current thread is not in _thread_in_vm");
260-
current->set_thread_state((JavaThreadState)st);
259+
ThreadInVMfromNative tiv(current);
260+
mmtk_harness_end_impl();
261261
}
262262

263263
static int offset_of_static_fields() {
@@ -356,8 +356,8 @@ OpenJDK_Upcalls mmtk_upcalls = {
356356
mmtk_get_object_size,
357357
mmtk_get_mmtk_mutator,
358358
mmtk_is_mutator,
359-
mmtk_enter_vm,
360-
mmtk_leave_vm,
359+
mmtk_harness_begin,
360+
mmtk_harness_end,
361361
compute_klass_mem_layout_checksum,
362362
offset_of_static_fields,
363363
static_oop_field_count_offset,

0 commit comments

Comments
 (0)