@@ -16,6 +16,27 @@ use crate::{BLOCK_FOR_GC, STW_COND, WORLD_HAS_STOPPED};
16
16
17
17
pub static GC_START : AtomicU64 = AtomicU64 :: new ( 0 ) ;
18
18
19
+ use std:: collections:: HashSet ;
20
+ use std:: sync:: RwLock ;
21
+ use std:: thread:: ThreadId ;
22
+
23
+ lazy_static ! {
24
+ static ref GC_THREADS : RwLock <HashSet <ThreadId >> = RwLock :: new( HashSet :: new( ) ) ;
25
+ }
26
+
27
+ pub ( crate ) fn register_gc_thread ( ) {
28
+ let id = std:: thread:: current ( ) . id ( ) ;
29
+ GC_THREADS . write ( ) . unwrap ( ) . insert ( id) ;
30
+ }
31
+ pub ( crate ) fn unregister_gc_thread ( ) {
32
+ let id = std:: thread:: current ( ) . id ( ) ;
33
+ GC_THREADS . write ( ) . unwrap ( ) . remove ( & id) ;
34
+ }
35
+ pub ( crate ) fn is_gc_thread ( ) -> bool {
36
+ let id = std:: thread:: current ( ) . id ( ) ;
37
+ GC_THREADS . read ( ) . unwrap ( ) . contains ( & id)
38
+ }
39
+
19
40
pub struct VMCollection { }
20
41
21
42
impl Collection < JuliaVM > for VMCollection {
@@ -84,18 +105,28 @@ impl Collection<JuliaVM> for VMCollection {
84
105
85
106
fn spawn_gc_thread ( _tls : VMThread , ctx : GCThreadContext < JuliaVM > ) {
86
107
// Just drop the join handle. The thread will run until the process quits.
87
- let _ = std:: thread:: spawn ( move || {
88
- use mmtk:: util:: opaque_pointer:: * ;
89
- use mmtk:: util:: Address ;
90
- let worker_tls = VMWorkerThread ( VMThread ( OpaquePointer :: from_address ( unsafe {
91
- Address :: from_usize ( thread_id:: get ( ) )
92
- } ) ) ) ;
93
- match ctx {
94
- GCThreadContext :: Worker ( w) => {
95
- mmtk:: memory_manager:: start_worker ( & SINGLETON , worker_tls, w)
108
+ let _ = std:: thread:: Builder :: new ( )
109
+ . name ( "MMTk Worker" . to_string ( ) )
110
+ . spawn ( move || {
111
+ use mmtk:: util:: opaque_pointer:: * ;
112
+ use mmtk:: util:: Address ;
113
+
114
+ // Remember this GC thread
115
+ register_gc_thread ( ) ;
116
+
117
+ // Start the worker loop
118
+ let worker_tls = VMWorkerThread ( VMThread ( OpaquePointer :: from_address ( unsafe {
119
+ Address :: from_usize ( thread_id:: get ( ) )
120
+ } ) ) ) ;
121
+ match ctx {
122
+ GCThreadContext :: Worker ( w) => {
123
+ mmtk:: memory_manager:: start_worker ( & SINGLETON , worker_tls, w)
124
+ }
96
125
}
97
- }
98
- } ) ;
126
+
127
+ // The GC thread quits somehow. Unresgister this GC thread
128
+ unregister_gc_thread ( ) ;
129
+ } ) ;
99
130
}
100
131
101
132
fn schedule_finalization ( _tls : VMWorkerThread ) { }
0 commit comments