@@ -33,8 +33,9 @@ pub struct TlsData<'tcx> {
33
33
/// pthreads-style thread-local storage.
34
34
keys : BTreeMap < TlsKey , TlsEntry < ' tcx > > ,
35
35
36
- /// A single global per thread dtor (that's how things work on macOS) with a data argument.
37
- global_dtors : BTreeMap < ThreadId , ( ty:: Instance < ' tcx > , Scalar < Tag > ) > ,
36
+ /// A single per thread destructor of the thread local storage (that's how
37
+ /// things work on macOS) with a data argument.
38
+ thread_dtors : BTreeMap < ThreadId , ( ty:: Instance < ' tcx > , Scalar < Tag > ) > ,
38
39
39
40
/// Whether we are in the "destruct" phase, during which some operations are UB.
40
41
dtors_running : HashSet < ThreadId > ,
@@ -48,7 +49,7 @@ impl<'tcx> Default for TlsData<'tcx> {
48
49
TlsData {
49
50
next_key : 1 , // start with 1 as we must not use 0 on Windows
50
51
keys : Default :: default ( ) ,
51
- global_dtors : Default :: default ( ) ,
52
+ thread_dtors : Default :: default ( ) ,
52
53
dtors_running : Default :: default ( ) ,
53
54
last_dtor_key : Default :: default ( ) ,
54
55
}
@@ -117,27 +118,27 @@ impl<'tcx> TlsData<'tcx> {
117
118
}
118
119
}
119
120
120
- /// Set global dtor for the given thread. This function is used to implement
121
- /// `_tlv_atexit` shim on MacOS.
121
+ /// Set the thread wide destructor of the thread local storage for the given
122
+ /// thread. This function is used to implement `_tlv_atexit` shim on MacOS.
122
123
///
123
- /// Global destructors are available only on MacOS and (potentially
124
- /// confusingly) they seem to be still per thread as can be guessed from the
125
- /// following comment in the [`_tlv_atexit`
124
+ /// Thread wide dtors are available only on MacOS. There is one destructor
125
+ /// per thread as can be guessed from the following comment in the
126
+ /// [`_tlv_atexit`
126
127
/// implementation](https://github.com/opensource-apple/dyld/blob/195030646877261f0c8c7ad8b001f52d6a26f514/src/threadLocalVariables.c#L389):
127
128
///
128
129
/// // NOTE: this does not need locks because it only operates on current thread data
129
- pub fn set_thread_global_dtor (
130
+ pub fn set_thread_dtor (
130
131
& mut self ,
131
132
thread : ThreadId ,
132
133
dtor : ty:: Instance < ' tcx > ,
133
134
data : Scalar < Tag >
134
135
) -> InterpResult < ' tcx > {
135
136
if self . dtors_running . contains ( & thread) {
136
137
// UB, according to libstd docs.
137
- throw_ub_format ! ( "setting global destructor while destructors are already running" ) ;
138
+ throw_ub_format ! ( "setting thread's local storage destructor while destructors are already running" ) ;
138
139
}
139
- if self . global_dtors . insert ( thread, ( dtor, data) ) . is_some ( ) {
140
- throw_unsup_format ! ( "setting more than one global destructor for the same thread is not supported" ) ;
140
+ if self . thread_dtors . insert ( thread, ( dtor, data) ) . is_some ( ) {
141
+ throw_unsup_format ! ( "setting more than one thread local storage destructor for the same thread is not supported" ) ;
141
142
}
142
143
Ok ( ( ) )
143
144
}
@@ -223,15 +224,15 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
223
224
Ok ( ( ) )
224
225
}
225
226
226
- /// Schedule the MacOS global dtor to be executed.
227
+ /// Schedule the MacOS thread destructor of the thread local storage to be
228
+ /// executed.
227
229
///
228
230
/// Note: It is safe to call this function also on other Unixes.
229
- fn schedule_macos_global_tls_dtors ( & mut self ) -> InterpResult < ' tcx > {
231
+ fn schedule_macos_tls_dtor ( & mut self ) -> InterpResult < ' tcx > {
230
232
let this = self . eval_context_mut ( ) ;
231
233
let thread_id = this. get_active_thread ( ) ?;
232
- // The macOS global dtor runs "before any TLS slots get freed", so do that first.
233
- if let Some ( ( instance, data) ) = this. machine . tls . global_dtors . remove ( & thread_id) {
234
- trace ! ( "Running global dtor {:?} on {:?} at {:?}" , instance, data, thread_id) ;
234
+ if let Some ( ( instance, data) ) = this. machine . tls . thread_dtors . remove ( & thread_id) {
235
+ trace ! ( "Running macos dtor {:?} on {:?} at {:?}" , instance, data, thread_id) ;
235
236
236
237
let ret_place = MPlaceTy :: dangling ( this. machine . layouts . unit , this) . into ( ) ;
237
238
this. call_function (
@@ -306,7 +307,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
306
307
}
307
308
} else {
308
309
this. machine . tls . dtors_running . insert ( active_thread) ;
309
- this. schedule_macos_global_tls_dtors ( ) ?;
310
+ // The macOS thread wide destructor runs "before any TLS slots get
311
+ // freed", so do that first.
312
+ this. schedule_macos_tls_dtor ( ) ?;
310
313
this. schedule_pthread_tls_dtors ( ) ?;
311
314
}
312
315
0 commit comments