summaryrefslogtreecommitdiff
path: root/gc/mmtk/src/collection.rs
blob: 83d046aef43092713e61fb8987e44e2acea25500 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use crate::abi::GCThreadTLS;

use crate::api::RubyMutator;
use crate::heap::RubyHeapTrigger;
use crate::mmtk;
use crate::upcalls;
use crate::Ruby;
use mmtk::memory_manager;
use mmtk::scheduler::*;
use mmtk::util::heap::GCTriggerPolicy;
use mmtk::util::VMMutatorThread;
use mmtk::util::VMThread;
use mmtk::util::VMWorkerThread;
use mmtk::vm::Collection;
use mmtk::vm::GCThreadContext;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::thread;

static CURRENT_GC_MAY_MOVE: AtomicBool = AtomicBool::new(false);

pub struct VMCollection {}

impl Collection<Ruby> for VMCollection {
    fn is_collection_enabled() -> bool {
        crate::CONFIGURATION.gc_enabled.load(Ordering::Relaxed)
    }

    fn stop_all_mutators<F>(tls: VMWorkerThread, mut mutator_visitor: F)
    where
        F: FnMut(&'static mut mmtk::Mutator<Ruby>),
    {
        (upcalls().stop_the_world)();

        if crate::mmtk().get_plan().current_gc_may_move_object() {
            CURRENT_GC_MAY_MOVE.store(true, Ordering::Relaxed);
            (upcalls().before_updating_jit_code)();
        } else {
            CURRENT_GC_MAY_MOVE.store(false, Ordering::Relaxed);
        }

        crate::binding().pinning_registry.pin_children(tls);

        (upcalls().get_mutators)(
            Self::notify_mutator_ready::<F>,
            &mut mutator_visitor as *mut F as *mut _,
        );
    }

    fn resume_mutators(_tls: VMWorkerThread) {
        if CURRENT_GC_MAY_MOVE.load(Ordering::Relaxed) {
            (upcalls().after_updating_jit_code)();
        }

        (upcalls().resume_mutators)();
    }

    fn block_for_gc(tls: VMMutatorThread) {
        (upcalls().block_for_gc)(tls);
    }

    fn spawn_gc_thread(_tls: VMThread, ctx: GCThreadContext<Ruby>) {
        let join_handle = match ctx {
            GCThreadContext::Worker(mut worker) => thread::Builder::new()
                .name("MMTk Worker Thread".to_string())
                .spawn(move || {
                    let ordinal = worker.ordinal;
                    debug!("Hello! This is MMTk Worker Thread running! ordinal: {ordinal}");
                    crate::register_gc_thread(thread::current().id());
                    let ptr_worker = &mut *worker as *mut GCWorker<Ruby>;
                    let gc_thread_tls =
                        Box::into_raw(Box::new(GCThreadTLS::for_worker(ptr_worker)));
                    (upcalls().init_gc_worker_thread)(gc_thread_tls);
                    memory_manager::start_worker(
                        mmtk(),
                        GCThreadTLS::to_vwt(gc_thread_tls),
                        worker,
                    );
                    debug!("An MMTk Worker Thread is quitting. Good bye! ordinal: {ordinal}");
                    crate::unregister_gc_thread(thread::current().id());
                })
                .unwrap(),
        };

        {
            let mut handles = crate::binding().gc_thread_join_handles.lock().unwrap();
            handles.push(join_handle);
        }
    }

    fn vm_live_bytes() -> usize {
        (upcalls().vm_live_bytes)()
    }

    fn create_gc_trigger() -> Box<dyn GCTriggerPolicy<Ruby>> {
        Box::new(RubyHeapTrigger::default())
    }
}

impl VMCollection {
    extern "C" fn notify_mutator_ready<F>(mutator_ptr: *mut RubyMutator, data: *mut libc::c_void)
    where
        F: FnMut(&'static mut mmtk::Mutator<Ruby>),
    {
        let mutator = unsafe { &mut *mutator_ptr };
        let mutator_visitor = unsafe { &mut *(data as *mut F) };
        mutator_visitor(mutator);
    }
}