Skip to content

Commit e750dfe

Browse files
authored
Refactor TLS (#306)
* Use VMThread/VMMutatorThread/VMWorkerThread instead of OpaquePointer
1 parent de79b1e commit e750dfe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+251
-216
lines changed

docs/tutorial/code/mygc_semispace/gc_work.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use crate::policy::space::Space;
55
use crate::scheduler::gc_work::*;
66
use crate::util::alloc::{Allocator, BumpAllocator};
77
use crate::util::forwarding_word;
8-
use crate::util::{Address, ObjectReference, OpaquePointer};
8+
use crate::util::{Address, ObjectReference};
9+
use crate::util::opaque_pointer::*;
910
use crate::vm::VMBinding;
1011
use crate::MMTK;
1112
use crate::plan::PlanConstraints;
@@ -28,8 +29,8 @@ impl<VM: VMBinding> CopyContext for MyGCCopyContext<VM> {
2829
fn constraints(&self) -> &'static PlanConstraints {
2930
&super::global::MYGC_CONSTRAINTS
3031
}
31-
fn init(&mut self, tls:OpaquePointer) {
32-
self.mygc.tls = tls;
32+
fn init(&mut self, tls: VMWorkerThread) {
33+
self.mygc.tls = tls.0;
3334
}
3435
// ANCHOR_END: copycontext_constraints_init
3536
// ANCHOR: copycontext_prepare
@@ -72,13 +73,13 @@ impl<VM: VMBinding> MyGCCopyContext<VM> {
7273
let plan = &mmtk.plan.downcast_ref::<MyGC<VM>>().unwrap();
7374
Self {
7475
plan,
75-
mygc: BumpAllocator::new(OpaquePointer::UNINITIALIZED, plan.tospace(), &*mmtk.plan),
76+
mygc: BumpAllocator::new(VMThread::UNINITIALIZED, plan.tospace(), &*mmtk.plan),
7677
}
7778
}
7879
}
7980

8081
impl<VM: VMBinding> WorkerLocal for MyGCCopyContext<VM> {
81-
fn init(&mut self, tls: OpaquePointer) {
82+
fn init(&mut self, tls: VMWorkerThread) {
8283
CopyContext::init(self, tls);
8384
}
8485
}

docs/tutorial/code/mygc_semispace/global.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::util::heap::HeapMeta;
2222
use crate::util::heap::VMRequest;
2323
use crate::util::side_metadata::SideMetadataContext;
2424
use crate::util::options::UnsafeOptionsWrapper;
25-
use crate::util::OpaquePointer;
25+
use crate::util::opaque_pointer::*;
2626
use crate::vm::VMBinding;
2727
use enum_map::EnumMap;
2828
use std::sync::atomic::{AtomicBool, Ordering}; // Add
@@ -65,7 +65,7 @@ impl<VM: VMBinding> Plan for MyGC<VM> {
6565
// ANCHOR: create_worker_local
6666
fn create_worker_local(
6767
&self,
68-
tls: OpaquePointer,
68+
tls: VMWorkerThread,
6969
mmtk: &'static MMTK<Self::VM>,
7070
) -> GCWorkerLocalPtr {
7171
let mut c = MyGCCopyContext::new(mmtk);
@@ -115,7 +115,7 @@ impl<VM: VMBinding> Plan for MyGC<VM> {
115115

116116
// Modify
117117
// ANCHOR: prepare
118-
fn prepare(&self, tls: OpaquePointer) {
118+
fn prepare(&self, tls: VMWorkerThread) {
119119
self.common.prepare(tls, true);
120120

121121
self.hi
@@ -129,7 +129,7 @@ impl<VM: VMBinding> Plan for MyGC<VM> {
129129

130130
// Modify
131131
// ANCHOR: release
132-
fn release(&self, tls: OpaquePointer) {
132+
fn release(&self, tls: VMWorkerThread) {
133133
self.common.release(tls, true);
134134
self.fromspace().release();
135135
}

docs/tutorial/code/mygc_semispace/mutator.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::plan::mutator_context::MutatorConfig;
66
use crate::plan::AllocationSemantics as AllocationType;
77
use crate::util::alloc::allocators::{AllocatorSelector, Allocators};
88
use crate::util::alloc::BumpAllocator;
9-
use crate::util::OpaquePointer;
9+
use crate::util::opaque_pointer::*;
1010
use crate::vm::VMBinding;
1111
use enum_map::enum_map;
1212
use enum_map::EnumMap;
@@ -17,7 +17,7 @@ use enum_map::EnumMap;
1717
// Add
1818
pub fn mygc_mutator_prepare<VM: VMBinding>(
1919
_mutator: &mut Mutator<VM>,
20-
_tls: OpaquePointer,
20+
_tls: VMWorkerThread,
2121
) {
2222
// Do nothing
2323
}
@@ -26,7 +26,7 @@ pub fn mygc_mutator_prepare<VM: VMBinding>(
2626
// ANCHOR: release
2727
pub fn mygc_mutator_release<VM: VMBinding>(
2828
mutator: &mut Mutator<VM>,
29-
_tls: OpaquePointer,
29+
_tls: VMWorkerThread,
3030
) {
3131
// rebind the allocation bump pointer to the appropriate semispace
3232
let bump_allocator = unsafe {
@@ -58,7 +58,7 @@ lazy_static! {
5858
// ANCHOR_END: allocator_mapping
5959

6060
pub fn create_mygc_mutator<VM: VMBinding>(
61-
mutator_tls: OpaquePointer,
61+
mutator_tls: VMMutatorThread,
6262
plan: &'static MyGC<VM>,
6363
) -> Mutator<VM> {
6464
let config = MutatorConfig {

src/mm/memory_manager.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::util::alloc::allocators::AllocatorSelector;
1919
use crate::util::constants::LOG_BYTES_IN_PAGE;
2020
use crate::util::heap::layout::vm_layout_constants::HEAP_END;
2121
use crate::util::heap::layout::vm_layout_constants::HEAP_START;
22-
use crate::util::OpaquePointer;
22+
use crate::util::opaque_pointer::*;
2323
use crate::util::{Address, ObjectReference};
2424
use crate::vm::Collection;
2525
use crate::vm::VMBinding;
@@ -30,7 +30,7 @@ use std::sync::atomic::Ordering;
3030
/// Arguments:
3131
/// * `mmtk`: A reference to an MMTk instance.
3232
/// * `tls`: The thread that will be used as the GC controller.
33-
pub fn start_control_collector<VM: VMBinding>(mmtk: &MMTK<VM>, tls: OpaquePointer) {
33+
pub fn start_control_collector<VM: VMBinding>(mmtk: &MMTK<VM>, tls: VMWorkerThread) {
3434
mmtk.plan.base().control_collector_context.run(tls);
3535
}
3636

@@ -61,7 +61,7 @@ pub fn gc_init<VM: VMBinding>(mmtk: &'static mut MMTK<VM>, heap_size: usize) {
6161
/// * `tls`: The thread that will be associated with the mutator.
6262
pub fn bind_mutator<VM: VMBinding>(
6363
mmtk: &'static MMTK<VM>,
64-
tls: OpaquePointer,
64+
tls: VMMutatorThread,
6565
) -> Box<Mutator<VM>> {
6666
crate::plan::global::create_mutator(tls, mmtk)
6767
}
@@ -148,7 +148,7 @@ pub fn get_allocator_mapping<VM: VMBinding>(
148148
/// * `worker`: A reference to the GC worker.
149149
/// * `mmtk`: A reference to an MMTk instance.
150150
pub fn start_worker<VM: VMBinding>(
151-
tls: OpaquePointer,
151+
tls: VMWorkerThread,
152152
worker: &mut GCWorker<VM>,
153153
mmtk: &'static MMTK<VM>,
154154
) {
@@ -165,7 +165,7 @@ pub fn start_worker<VM: VMBinding>(
165165
/// * `mmtk`: A reference to an MMTk instance.
166166
/// * `tls`: The thread that wants to enable the collection. This value will be passed back to the VM in
167167
/// Collection::spawn_worker_thread() so that the VM knows the context.
168-
pub fn enable_collection<VM: VMBinding>(mmtk: &'static MMTK<VM>, tls: OpaquePointer) {
168+
pub fn enable_collection<VM: VMBinding>(mmtk: &'static MMTK<VM>, tls: VMThread) {
169169
mmtk.scheduler.initialize(mmtk.options.threads, mmtk, tls);
170170
VM::VMCollection::spawn_worker_thread(tls, None); // spawn controller thread
171171
mmtk.plan.base().initialized.store(true, Ordering::SeqCst);
@@ -228,7 +228,7 @@ pub fn total_bytes<VM: VMBinding>(mmtk: &MMTK<VM>) -> usize {
228228
/// Arguments:
229229
/// * `mmtk`: A reference to an MMTk instance.
230230
/// * `tls`: The thread that triggers this collection request.
231-
pub fn handle_user_collection_request<VM: VMBinding>(mmtk: &MMTK<VM>, tls: OpaquePointer) {
231+
pub fn handle_user_collection_request<VM: VMBinding>(mmtk: &MMTK<VM>, tls: VMMutatorThread) {
232232
mmtk.plan.handle_user_collection_request(tls, false);
233233
}
234234

@@ -326,7 +326,7 @@ pub fn add_phantom_candidate<VM: VMBinding>(
326326
/// Arguments:
327327
/// * `mmtk`: A reference to an MMTk instance.
328328
/// * `tls`: The thread that calls the function (and triggers a collection).
329-
pub fn harness_begin<VM: VMBinding>(mmtk: &MMTK<VM>, tls: OpaquePointer) {
329+
pub fn harness_begin<VM: VMBinding>(mmtk: &MMTK<VM>, tls: VMMutatorThread) {
330330
mmtk.harness_begin(tls);
331331
}
332332

src/mmtk.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use crate::util::finalizable_processor::FinalizableProcessor;
55
use crate::util::heap::layout::heap_layout::Mmapper;
66
use crate::util::heap::layout::heap_layout::VMMap;
77
use crate::util::heap::layout::map::Map;
8+
use crate::util::opaque_pointer::*;
89
use crate::util::options::{Options, UnsafeOptionsWrapper};
910
use crate::util::reference_processor::ReferenceProcessors;
1011
#[cfg(feature = "sanity")]
1112
use crate::util::sanity::sanity_checker::SanityChecker;
12-
use crate::util::OpaquePointer;
1313
use crate::vm::VMBinding;
1414
use std::default::Default;
1515
use std::sync::atomic::{AtomicBool, Ordering};
@@ -66,7 +66,7 @@ impl<VM: VMBinding> MMTK<VM> {
6666
}
6767
}
6868

69-
pub fn harness_begin(&self, tls: OpaquePointer) {
69+
pub fn harness_begin(&self, tls: VMMutatorThread) {
7070
// FIXME Do a full heap GC if we have generational GC
7171
self.plan.handle_user_collection_request(tls, true);
7272
self.inside_harness.store(true, Ordering::SeqCst);

src/plan/controller_collector_context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
33
use crate::scheduler::gc_work::ScheduleCollection;
44
use crate::scheduler::*;
5-
use crate::util::OpaquePointer;
5+
use crate::util::opaque_pointer::*;
66
use crate::vm::VMBinding;
77
use std::marker::PhantomData;
88
use std::sync::atomic::{AtomicBool, Ordering};
99
use std::sync::RwLock;
1010
use std::sync::{Arc, Condvar, Mutex};
1111

1212
struct RequestSync {
13-
tls: OpaquePointer,
13+
tls: VMWorkerThread,
1414
request_count: isize,
1515
last_request_count: isize,
1616
}
@@ -34,7 +34,7 @@ impl<VM: VMBinding> ControllerCollectorContext<VM> {
3434
pub fn new() -> Self {
3535
ControllerCollectorContext {
3636
request_sync: Mutex::new(RequestSync {
37-
tls: OpaquePointer::UNINITIALIZED,
37+
tls: VMWorkerThread(VMThread::UNINITIALIZED),
3838
request_count: 0,
3939
last_request_count: -1,
4040
}),
@@ -51,7 +51,7 @@ impl<VM: VMBinding> ControllerCollectorContext<VM> {
5151
*scheduler_guard = Some(scheduler.clone());
5252
}
5353

54-
pub fn run(&self, tls: OpaquePointer) {
54+
pub fn run(&self, tls: VMWorkerThread) {
5555
{
5656
self.request_sync.lock().unwrap().tls = tls;
5757
}

src/plan/gencopy/gc_work.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use crate::scheduler::gc_work::*;
66
use crate::scheduler::WorkerLocal;
77
use crate::util::alloc::{Allocator, BumpAllocator};
88
use crate::util::forwarding_word;
9+
use crate::util::opaque_pointer::*;
910
use crate::util::side_metadata::*;
10-
use crate::util::{Address, ObjectReference, OpaquePointer};
11+
use crate::util::{Address, ObjectReference};
1112
use crate::vm::*;
1213
use crate::MMTK;
1314
use std::ops::{Deref, DerefMut};
@@ -23,8 +24,8 @@ impl<VM: VMBinding> CopyContext for GenCopyCopyContext<VM> {
2324
fn constraints(&self) -> &'static PlanConstraints {
2425
&super::global::GENCOPY_CONSTRAINTS
2526
}
26-
fn init(&mut self, tls: OpaquePointer) {
27-
self.ss.tls = tls;
27+
fn init(&mut self, tls: VMWorkerThread) {
28+
self.ss.tls = tls.0;
2829
}
2930
fn prepare(&mut self) {
3031
self.ss.rebind(self.plan.tospace());
@@ -65,13 +66,13 @@ impl<VM: VMBinding> GenCopyCopyContext<VM> {
6566
Self {
6667
plan,
6768
// it doesn't matter which space we bind with the copy allocator. We will rebind to a proper space in prepare().
68-
ss: BumpAllocator::new(OpaquePointer::UNINITIALIZED, plan.tospace(), &*mmtk.plan),
69+
ss: BumpAllocator::new(VMThread::UNINITIALIZED, plan.tospace(), &*mmtk.plan),
6970
}
7071
}
7172
}
7273

7374
impl<VM: VMBinding> WorkerLocal for GenCopyCopyContext<VM> {
74-
fn init(&mut self, tls: OpaquePointer) {
75+
fn init(&mut self, tls: VMWorkerThread) {
7576
CopyContext::init(self, tls);
7677
}
7778
}

src/plan/gencopy/global.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::util::options::UnsafeOptionsWrapper;
2424
#[cfg(feature = "sanity")]
2525
use crate::util::sanity::sanity_checker::*;
2626
use crate::util::side_metadata::SideMetadataContext;
27-
use crate::util::OpaquePointer;
27+
use crate::util::VMWorkerThread;
2828
use crate::vm::*;
2929
use crate::{mmtk::MMTK, plan::barriers::BarrierSelector};
3030
use enum_map::EnumMap;
@@ -61,7 +61,7 @@ impl<VM: VMBinding> Plan for GenCopy<VM> {
6161

6262
fn create_worker_local(
6363
&self,
64-
tls: OpaquePointer,
64+
tls: VMWorkerThread,
6565
mmtk: &'static MMTK<Self::VM>,
6666
) -> GCWorkerLocalPtr {
6767
let mut c = GenCopyCopyContext::new(mmtk);
@@ -128,7 +128,7 @@ impl<VM: VMBinding> Plan for GenCopy<VM> {
128128
&*ALLOCATOR_MAPPING
129129
}
130130

131-
fn prepare(&self, tls: OpaquePointer) {
131+
fn prepare(&self, tls: VMWorkerThread) {
132132
self.common.prepare(tls, true);
133133
self.nursery.prepare(true);
134134
if !self.in_nursery() {
@@ -140,7 +140,7 @@ impl<VM: VMBinding> Plan for GenCopy<VM> {
140140
self.copyspace1.prepare(!hi);
141141
}
142142

143-
fn release(&self, tls: OpaquePointer) {
143+
fn release(&self, tls: VMWorkerThread) {
144144
self.common.release(tls, true);
145145
self.nursery.release();
146146
if !self.in_nursery() {

src/plan/gencopy/mutator.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ use crate::plan::mutator_context::MutatorConfig;
66
use crate::plan::AllocationSemantics as AllocationType;
77
use crate::util::alloc::allocators::{AllocatorSelector, Allocators};
88
use crate::util::alloc::BumpAllocator;
9-
use crate::util::OpaquePointer;
9+
use crate::util::{VMMutatorThread, VMWorkerThread};
1010
use crate::vm::VMBinding;
1111
use crate::MMTK;
1212
use enum_map::enum_map;
1313
use enum_map::EnumMap;
1414

15-
pub fn gencopy_mutator_prepare<VM: VMBinding>(_mutator: &mut Mutator<VM>, _tls: OpaquePointer) {
15+
pub fn gencopy_mutator_prepare<VM: VMBinding>(_mutator: &mut Mutator<VM>, _tls: VMWorkerThread) {
1616
// Do nothing
1717
}
1818

19-
pub fn gencopy_mutator_release<VM: VMBinding>(mutator: &mut Mutator<VM>, _tls: OpaquePointer) {
19+
pub fn gencopy_mutator_release<VM: VMBinding>(mutator: &mut Mutator<VM>, _tls: VMWorkerThread) {
2020
// reset nursery allocator
2121
let bump_allocator = unsafe {
2222
mutator
@@ -37,7 +37,7 @@ lazy_static! {
3737
}
3838

3939
pub fn create_gencopy_mutator<VM: VMBinding>(
40-
mutator_tls: OpaquePointer,
40+
mutator_tls: VMMutatorThread,
4141
mmtk: &'static MMTK<VM>,
4242
) -> Mutator<VM> {
4343
let gencopy = mmtk.plan.downcast_ref::<GenCopy<VM>>().unwrap();

0 commit comments

Comments
 (0)