1//! Implementation of the `thread_local` macro.
2//!
3//! There are three different thread-local implementations:
4//! * Some targets lack threading support, and hence have only one thread, so
5//! the TLS data is stored in a normal `static`.
6//! * Some targets support TLS natively via the dynamic linker and C runtime.
7//! * On some targets, the OS provides a library-based TLS implementation. The
8//! TLS data is heap-allocated and referenced using a TLS key.
9//!
10//! Each implementation provides a macro which generates the `LocalKey` `const`
11//! used to reference the TLS variable, along with the necessary helper structs
12//! to track the initialization/destruction state of the variable.
13//!
14//! Additionally, this module contains abstractions for the OS interfaces used
15//! for these implementations.
1617#![cfg_attr(test, allow(unused))]
18#![doc(hidden)]
19#![forbid(unsafe_op_in_unsafe_fn)]
20#![unstable(
21 feature = "thread_local_internals",
22 reason = "internal details of the thread_local macro",
23 issue = "none"
24)]
25#![deny(
26 clippy::arithmetic_side_effects,
27 clippy::expect_used,
28 clippy::unwrap_used,
29 clippy::indexing_slicing,
30 clippy::panic,
31 clippy::unreachable,
32 clippy::unimplemented,
33 reason = "TLS accesses must not call the global allocator, including via panic (#160930)"
34)]
3536cfg_select! {
37 any(
38 all(target_family = "wasm", not(target_feature = "atomics"), not(target_os = "wasi")),
39 target_os = "uefi",
40 target_os = "zkvm",
41 target_os = "trusty",
42 target_os = "vexos",
43 ) => {
44mod no_threads;
45pub use no_threads::{EagerStorage, LazyStorage, thread_local_inner};
46pub(crate) use no_threads::{LocalPointer, local_pointer};
47 }
48 target_thread_local => {
49mod native;
50pub use native::{EagerStorage, LazyStorage, thread_local_inner};
51pub(crate) use native::{LocalPointer, local_pointer};
52 }
53_ => {
54mod os;
55pub(crate) use os::{LocalPointer, local_pointer};
56pub use os::{Storage, thread_local_inner, value_align};
57 }
58}
5960/// The native TLS implementation needs a way to register destructors for its data.
61/// This module contains platform-specific implementations of that register.
62///
63/// It turns out however that most platforms don't have a way to register a
64/// destructor for each variable. On these platforms, we keep track of the
65/// destructors ourselves and register (through the [`guard`] module) only a
66/// single callback that runs all of the destructors in the list.
67#[cfg(all(
68 target_thread_local,
69 not(all(target_family = "wasm", not(target_feature = "atomics"), not(target_os = "wasi")))
70))]
71pub(crate) mod destructors {
72cfg_select! {
73 any(
74 target_os = "linux",
75 target_os = "android",
76 target_os = "fuchsia",
77 target_os = "redox",
78 target_os = "hurd",
79 target_os = "netbsd",
80 target_os = "dragonfly"
81) => {
82mod linux_like;
83mod list;
84pub(super) use linux_like::register;
85pub(super) use list::run;
86 }
87_ => {
88mod list;
89pub(super) use list::register;
90pub(crate) use list::run;
91 }
92 }
93}
9495/// This module provides a way to schedule the execution of the destructor list
96/// and the [runtime cleanup](crate::rt::thread_cleanup) function. Calling `enable`
97/// sets up the current thread to ensure that these functions are called at the right times.
98pub(crate) mod guard {
99cfg_select! {
100 all(target_thread_local, target_vendor = "apple") => {
101mod apple;
102pub(crate) use apple::enable;
103 }
104 all(target_thread_local, target_os = "aix") => {
105mod aix;
106pub(crate) use aix::enable;
107 }
108 target_os = "windows" => {
109mod windows;
110pub(crate) use windows::enable;
111#[cfg(not(target_thread_local))]
112mod retained;
113#[cfg(not(target_thread_local))]
114pub(crate) use retained::{LOCAL_POINTER_NEEDS_DTOR, cleanup_pending};
115 }
116 any(
117 all(target_family = "wasm", not(target_os = "wasi")),
118 target_os = "uefi",
119 target_os = "zkvm",
120 target_os = "trusty",
121 target_os = "vexos",
122 ) => {
123pub(crate) fn enable() {
124// FIXME: Right now there is no concept of "thread exit" on
125 // wasm, but this is likely going to show up at some point in
126 // the form of an exported symbol that the wasm runtime is going
127 // to be expected to call. For now we just leak everything, but
128 // if such a function starts to exist it will probably need to
129 // iterate the destructor list with these functions:
130#[cfg(all(target_family = "wasm", target_feature = "atomics"))]
131 #[allow(unused)]
132use super::destructors::run;
133#[allow(unused)]
134use crate::rt::thread_cleanup;
135 }
136 }
137 any(target_os = "hermit", target_os = "xous") => {
138// `std` is the only runtime, so it just calls the destructor functions
139 // itself when the time comes.
140pub(crate) fn enable() {}
141#[cfg(not(target_thread_local))]
142mod retained;
143#[cfg(not(target_thread_local))]
144pub(crate) use retained::{LOCAL_POINTER_NEEDS_DTOR, cleanup_pending};
145 }
146 target_os = "solid_asp3" => {
147mod solid;
148pub(crate) use solid::enable;
149 }
150_ => {
151mod key;
152pub(crate) use key::enable;
153#[cfg(not(target_thread_local))]
154pub(crate) use key::{LOCAL_POINTER_NEEDS_DTOR, cleanup_pending};
155 }
156 }
157}
158159/// `const`-creatable TLS keys.
160///
161/// Most OSs without native TLS will provide a library-based way to create TLS
162/// storage. For each TLS variable, we create a key, which can then be used to
163/// reference an entry in a thread-local table. This then associates each key
164/// with a pointer which we can get and set to store our data.
165pub(crate) mod key {
166cfg_select! {
167 any(
168 all(not(target_vendor = "apple"), not(target_family = "wasm"), target_family = "unix"),
169 all(not(target_thread_local), target_vendor = "apple"),
170 target_os = "teeos",
171 target_os = "wasi",
172 ) => {
173mod racy;
174mod unix;
175#[cfg(test)]
176mod tests;
177pub(super) use racy::LazyKey;
178#[cfg(any(not(target_thread_local), test))]
179pub(super) use unix::get;
180pub(super) use unix::{Key, set};
181use unix::{create, destroy};
182 }
183 all(not(target_thread_local), target_os = "windows") => {
184#[cfg(test)]
185mod tests;
186mod windows;
187pub(super) use windows::{Key, LazyKey, get, run_dtors, set};
188 }
189 all(target_vendor = "fortanix", target_env = "sgx") => {
190mod racy;
191mod sgx;
192#[cfg(test)]
193mod tests;
194pub(super) use racy::LazyKey;
195pub(super) use sgx::{Key, get, set};
196use sgx::{create, destroy};
197 }
198 target_os = "xous" => {
199mod racy;
200#[cfg(test)]
201mod tests;
202mod xous;
203pub(super) use racy::LazyKey;
204pub(crate) use xous::destroy_tls;
205pub(super) use xous::{Key, get, set};
206use xous::{create, destroy};
207 }
208 target_os = "motor" => {
209mod racy;
210#[cfg(test)]
211mod tests;
212pub(super) use moto_rt::tls::{Key, get, set};
213use moto_rt::tls::{create, destroy};
214pub(super) use racy::LazyKey;
215 }
216_ => {}
217 }
218}
219220/// Run a callback in a scenario which must not unwind (such as a `extern "C"
221/// fn` declared in a user crate). If the callback unwinds anyway, then
222/// `rtabort` with a message about thread local panicking on drop.
223#[inline]
224#[allow(dead_code)]
225fn abort_on_dtor_unwind(f: impl FnOnce()) {
226// Using a guard like this is lower cost.
227let guard = DtorUnwindGuard;
228f();
229 core::mem::forget(guard);
230231struct DtorUnwindGuard;
232impl Dropfor DtorUnwindGuard {
233#[inline]
234fn drop(&mut self) {
235// This is not terribly descriptive, but it doesn't need to be as we'll
236 // already have printed a panic message at this point.
237{
if let Some(mut out) = crate::sys::stdio::panic_output() {
let _ =
crate::io::Write::write_fmt(&mut out,
format_args!("fatal runtime error: {0}, aborting\n",
format_args!("thread local panicked on drop")));
};
crate::process::abort();
};rtabort!("thread local panicked on drop");
238 }
239 }
240}