Skip to content

Commit 6842eb2

Browse files
author
Vytautas Astrauskas
committed
Rename global tls dtor to thread dtor.
1 parent 39efdf3 commit 6842eb2

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

src/shims/foreign_items/posix/macos.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8383
let dtor = this.memory.get_fn(dtor)?.as_instance()?;
8484
let data = this.read_scalar(args[1])?.not_undef()?;
8585
let active_thread = this.get_active_thread()?;
86-
this.machine.tls.set_thread_global_dtor(active_thread, dtor, data)?;
86+
this.machine.tls.set_thread_dtor(active_thread, dtor, data)?;
8787
}
8888

8989
// Querying system information

src/shims/tls.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ pub struct TlsData<'tcx> {
3333
/// pthreads-style thread-local storage.
3434
keys: BTreeMap<TlsKey, TlsEntry<'tcx>>,
3535

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>)>,
3839

3940
/// Whether we are in the "destruct" phase, during which some operations are UB.
4041
dtors_running: HashSet<ThreadId>,
@@ -48,7 +49,7 @@ impl<'tcx> Default for TlsData<'tcx> {
4849
TlsData {
4950
next_key: 1, // start with 1 as we must not use 0 on Windows
5051
keys: Default::default(),
51-
global_dtors: Default::default(),
52+
thread_dtors: Default::default(),
5253
dtors_running: Default::default(),
5354
last_dtor_key: Default::default(),
5455
}
@@ -117,27 +118,27 @@ impl<'tcx> TlsData<'tcx> {
117118
}
118119
}
119120

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.
122123
///
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`
126127
/// implementation](https://github.com/opensource-apple/dyld/blob/195030646877261f0c8c7ad8b001f52d6a26f514/src/threadLocalVariables.c#L389):
127128
///
128129
/// // 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(
130131
&mut self,
131132
thread: ThreadId,
132133
dtor: ty::Instance<'tcx>,
133134
data: Scalar<Tag>
134135
) -> InterpResult<'tcx> {
135136
if self.dtors_running.contains(&thread) {
136137
// 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");
138139
}
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");
141142
}
142143
Ok(())
143144
}
@@ -223,15 +224,15 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
223224
Ok(())
224225
}
225226

226-
/// Schedule the MacOS global dtor to be executed.
227+
/// Schedule the MacOS thread destructor of the thread local storage to be
228+
/// executed.
227229
///
228230
/// 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> {
230232
let this = self.eval_context_mut();
231233
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);
235236

236237
let ret_place = MPlaceTy::dangling(this.machine.layouts.unit, this).into();
237238
this.call_function(
@@ -306,7 +307,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
306307
}
307308
} else {
308309
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()?;
310313
this.schedule_pthread_tls_dtors()?;
311314
}
312315

0 commit comments

Comments
 (0)