Skip to content

Commit 3458f47

Browse files
committed
Remove the global JavaThreadIteratorWithHandle
1 parent cf89130 commit 3458f47

File tree

5 files changed

+61
-34
lines changed

5 files changed

+61
-34
lines changed

mmtk/src/abi.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,38 @@ pub fn validate_memory_layouts() {
404404
^ mem::size_of::<InstanceClassLoaderKlass>()
405405
^ mem::size_of::<TypeArrayKlass>()
406406
^ mem::size_of::<ObjArrayKlass>()
407+
^ mem::size_of::<JavaThreadIteratorWithHandle>()
407408
};
408409
assert_eq!(vm_checksum, binding_checksum);
409410
}
411+
412+
// hotspot/share/runtime/threadSMR.hpp
413+
#[repr(C)]
414+
pub struct SafeThreadsListPtr {
415+
_previous: *mut libc::c_void,
416+
_thread: *mut libc::c_void,
417+
_list: *mut libc::c_void,
418+
_has_ref_count: bool,
419+
_needs_release: bool,
420+
}
421+
422+
#[repr(C)]
423+
pub struct ElapsedTimer {
424+
_counter: i64,
425+
_start_counter: i64,
426+
_active: bool,
427+
}
428+
429+
#[repr(C)]
430+
pub struct ThreadsListHandle {
431+
vtable: *mut libc::c_void,
432+
_list_ptr: SafeThreadsListPtr,
433+
_timer: ElapsedTimer,
434+
}
435+
436+
#[repr(C)]
437+
pub struct JavaThreadIteratorWithHandle {
438+
vtable: *mut libc::c_void,
439+
_tlh: ThreadsListHandle,
440+
_index: u32,
441+
}

mmtk/src/active_plan.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
11
use crate::OpenJDK;
22
use crate::SINGLETON;
33
use crate::UPCALLS;
4+
use crate::abi::JavaThreadIteratorWithHandle;
45
use mmtk::util::opaque_pointer::*;
56
use mmtk::vm::ActivePlan;
67
use mmtk::Mutator;
78
use mmtk::Plan;
8-
use std::sync::{Mutex, MutexGuard};
9+
use std::marker::PhantomData;
10+
use std::mem::MaybeUninit;
911

1012
struct OpenJDKMutatorIterator<'a> {
11-
_guard: MutexGuard<'a, ()>,
13+
handle: MaybeUninit<JavaThreadIteratorWithHandle>,
14+
_p: PhantomData<&'a ()>
1215
}
1316

1417
impl<'a> OpenJDKMutatorIterator<'a> {
15-
fn new(guard: MutexGuard<'a, ()>) -> Self {
16-
// Reset mutator iterator
18+
fn new() -> Self {
19+
let mut iter = Self {
20+
handle: MaybeUninit::uninit(),
21+
_p: PhantomData,
22+
};
23+
// Create JavaThreadIteratorWithHandle
1724
unsafe {
18-
((*UPCALLS).reset_mutator_iterator)();
25+
((*UPCALLS).new_java_thread_iterator)(iter.handle.as_mut_ptr());
1926
}
20-
Self { _guard: guard }
27+
iter
2128
}
2229
}
2330

2431
impl<'a> Iterator for OpenJDKMutatorIterator<'a> {
2532
type Item = &'a mut Mutator<OpenJDK>;
2633

2734
fn next(&mut self) -> Option<Self::Item> {
28-
let next = unsafe { ((*UPCALLS).get_next_mutator)() };
35+
let next = unsafe { ((*UPCALLS).java_thread_iterator_next)(self.handle.as_mut_ptr()) };
2936
if next.is_null() {
3037
None
3138
} else {
@@ -53,15 +60,11 @@ impl ActivePlan<OpenJDK> for VMActivePlan {
5360
}
5461

5562
fn mutators<'a>() -> Box<dyn Iterator<Item = &'a mut Mutator<OpenJDK>> + 'a> {
56-
let guard = MUTATOR_ITERATOR_LOCK.lock().unwrap();
57-
Box::new(OpenJDKMutatorIterator::new(guard))
63+
Box::new(OpenJDKMutatorIterator::new())
5864
}
5965

6066
fn number_of_mutators() -> usize {
6167
unsafe { ((*UPCALLS).number_of_mutators)() }
6268
}
6369
}
6470

65-
lazy_static! {
66-
pub static ref MUTATOR_ITERATOR_LOCK: Mutex<()> = Mutex::new(());
67-
}

mmtk/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ pub struct OpenJDK_Upcalls {
6262
pub spawn_gc_thread: extern "C" fn(tls: VMThread, kind: libc::c_int, ctx: *mut libc::c_void),
6363
pub block_for_gc: extern "C" fn(),
6464
pub out_of_memory: extern "C" fn(tls: VMThread, err_kind: AllocationError),
65-
pub get_next_mutator: extern "C" fn() -> *mut Mutator<OpenJDK>,
66-
pub reset_mutator_iterator: extern "C" fn(),
6765
pub scan_object: extern "C" fn(trace: *mut c_void, object: ObjectReference, tls: OpaquePointer),
6866
pub dump_object: extern "C" fn(object: ObjectReference),
6967
pub get_object_size: extern "C" fn(object: ObjectReference) -> usize,
@@ -95,6 +93,8 @@ pub struct OpenJDK_Upcalls {
9593
pub schedule_finalizer: extern "C" fn(),
9694
pub prepare_for_roots_re_scanning: extern "C" fn(),
9795
pub enqueue_references: extern "C" fn(objects: *const ObjectReference, len: usize),
96+
pub new_java_thread_iterator: extern "C" fn(iter: *mut abi::JavaThreadIteratorWithHandle),
97+
pub java_thread_iterator_next: extern "C" fn(iter: *mut abi::JavaThreadIteratorWithHandle) -> *mut Mutator<OpenJDK>,
9898
}
9999

100100
pub static mut UPCALLS: *const OpenJDK_Upcalls = null_mut();

openjdk/mmtk.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ typedef struct {
149149
void (*spawn_gc_thread) (void *tls, int kind, void *ctx);
150150
void (*block_for_gc) ();
151151
void (*out_of_memory) (void *tls, MMTkAllocationError err_kind);
152-
void* (*get_next_mutator) ();
153-
void (*reset_mutator_iterator) ();
154152
void (*scan_object) (void* trace, void* object, void* tls);
155153
void (*dump_object) (void* object);
156154
size_t (*get_object_size) (void* object);
@@ -182,6 +180,8 @@ typedef struct {
182180
void (*schedule_finalizer)();
183181
void (*prepare_for_roots_re_scanning)();
184182
void (*enqueue_references)(void** objects, size_t len);
183+
void (*new_java_thread_iterator)(void*);
184+
void* (*java_thread_iterator_next)(void*);
185185
} OpenJDK_Upcalls;
186186

187187
extern void openjdk_gc_init(OpenJDK_Upcalls *calls);

openjdk/mmtkUpcalls.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,27 +190,18 @@ struct MaybeUninit {
190190
char _data[sizeof(T)];
191191
};
192192

193-
static MaybeUninit<JavaThreadIteratorWithHandle> jtiwh;
194-
static bool mutator_iteration_start = true;
193+
static void mmtk_new_java_thread_iterator(void* iter) {
194+
*(JavaThreadIteratorWithHandle*)iter = JavaThreadIteratorWithHandle();
195+
}
195196

196-
static void* mmtk_get_next_mutator() {
197-
if (mutator_iteration_start) {
198-
*jtiwh = JavaThreadIteratorWithHandle();
199-
mutator_iteration_start = false;
200-
}
201-
JavaThread *thr = jtiwh->next();
197+
static void* mmtk_java_thread_iterator_next(void* iter) {
198+
JavaThread *thr = ((JavaThreadIteratorWithHandle*)iter)->next();
202199
if (thr == NULL) {
203-
mutator_iteration_start = true;
204200
return NULL;
205201
}
206202
return (void*) &thr->third_party_heap_mutator;
207203
}
208204

209-
static void mmtk_reset_mutator_iterator() {
210-
mutator_iteration_start = true;
211-
}
212-
213-
214205
static void mmtk_scan_all_thread_roots(EdgesClosure closure) {
215206
MMTkRootsClosure2 cl(closure);
216207
MMTkHeap::heap()->scan_thread_roots(cl);
@@ -276,7 +267,8 @@ static size_t compute_klass_mem_layout_checksum() {
276267
^ sizeof(InstanceMirrorKlass)
277268
^ sizeof(InstanceClassLoaderKlass)
278269
^ sizeof(TypeArrayKlass)
279-
^ sizeof(ObjArrayKlass);
270+
^ sizeof(ObjArrayKlass)
271+
^ sizeof(JavaThreadIteratorWithHandle);
280272
}
281273

282274
static int referent_offset() {
@@ -347,8 +339,6 @@ OpenJDK_Upcalls mmtk_upcalls = {
347339
mmtk_spawn_gc_thread,
348340
mmtk_block_for_gc,
349341
mmtk_out_of_memory,
350-
mmtk_get_next_mutator,
351-
mmtk_reset_mutator_iterator,
352342
mmtk_scan_object,
353343
mmtk_dump_object,
354344
mmtk_get_object_size,
@@ -379,5 +369,7 @@ OpenJDK_Upcalls mmtk_upcalls = {
379369
mmtk_number_of_mutators,
380370
mmtk_schedule_finalizer,
381371
mmtk_prepare_for_roots_re_scanning,
382-
mmtk_enqueue_references
372+
mmtk_enqueue_references,
373+
mmtk_new_java_thread_iterator,
374+
mmtk_java_thread_iterator_next,
383375
};

0 commit comments

Comments
 (0)