std/sys/thread_local/
mod.rs1#![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)]
35
36cfg_select! {
37 any(
38 all(target_family = "wasm", not(target_feature = "atomics"), not(target_env = "p3")),
39 target_os = "uefi",
40 target_os = "zkvm",
41 target_os = "trusty",
42 target_os = "vexos",
43 ) => {
44 mod no_threads;
45 pub use no_threads::{EagerStorage, LazyStorage, thread_local_inner};
46 pub(crate) use no_threads::{LocalPointer, local_pointer};
47 }
48 target_thread_local => {
49 mod native;
50 pub use native::{EagerStorage, LazyStorage, thread_local_inner};
51 pub(crate) use native::{LocalPointer, local_pointer};
52 }
53 _ => {
54 mod os;
55 pub use os::{Storage, thread_local_inner, value_align};
56 pub(crate) use os::{LocalPointer, local_pointer};
57 }
58}
59
60#[cfg(all(
68 target_thread_local,
69 not(all(target_family = "wasm", not(target_feature = "atomics"), not(target_env = "p3")))
70))]
71pub(crate) mod destructors {
72 cfg_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 ) => {
82 mod linux_like;
83 mod list;
84 pub(super) use linux_like::register;
85 pub(super) use list::run;
86 }
87 _ => {
88 mod list;
89 pub(super) use list::register;
90 pub(crate) use list::run;
91 }
92 }
93}
94
95pub(crate) mod guard {
99 cfg_select! {
100 all(target_thread_local, target_vendor = "apple") => {
101 mod apple;
102 pub(crate) use apple::enable;
103 }
104 target_os = "windows" => {
105 mod windows;
106 pub(crate) use windows::enable;
107 }
108 any(
109 all(target_family = "wasm", not(target_env = "p3")),
110 target_os = "uefi",
111 target_os = "zkvm",
112 target_os = "trusty",
113 target_os = "vexos",
114 ) => {
115 pub(crate) fn enable() {
116 #[cfg(all(target_family = "wasm", target_feature = "atomics"))]
123 #[allow(unused)]
124 use super::destructors::run;
125 #[allow(unused)]
126 use crate::rt::thread_cleanup;
127 }
128 }
129 any(
130 target_os = "hermit",
131 target_os = "xous",
132 ) => {
133 pub(crate) fn enable() {}
136 }
137 target_os = "solid_asp3" => {
138 mod solid;
139 pub(crate) use solid::enable;
140 }
141 _ => {
142 mod key;
143 pub(crate) use key::enable;
144 }
145 }
146}
147
148pub(crate) mod key {
155 cfg_select! {
156 any(
157 all(
158 not(target_vendor = "apple"),
159 not(target_family = "wasm"),
160 target_family = "unix",
161 ),
162 all(not(target_thread_local), target_vendor = "apple"),
163 target_os = "teeos",
164 all(target_os = "wasi", target_env = "p3"),
165 ) => {
166 mod racy;
167 mod unix;
168 #[cfg(test)]
169 mod tests;
170 pub(super) use racy::LazyKey;
171 pub(super) use unix::{Key, set};
172 #[cfg(any(not(target_thread_local), test))]
173 pub(super) use unix::get;
174 use unix::{create, destroy};
175 }
176 all(not(target_thread_local), target_os = "windows") => {
177 #[cfg(test)]
178 mod tests;
179 mod windows;
180 pub(super) use windows::{Key, LazyKey, get, run_dtors, set};
181 }
182 all(target_vendor = "fortanix", target_env = "sgx") => {
183 mod racy;
184 mod sgx;
185 #[cfg(test)]
186 mod tests;
187 pub(super) use racy::LazyKey;
188 pub(super) use sgx::{Key, get, set};
189 use sgx::{create, destroy};
190 }
191 target_os = "xous" => {
192 mod racy;
193 #[cfg(test)]
194 mod tests;
195 mod xous;
196 pub(super) use racy::LazyKey;
197 pub(crate) use xous::destroy_tls;
198 pub(super) use xous::{Key, get, set};
199 use xous::{create, destroy};
200 }
201 target_os = "motor" => {
202 mod racy;
203 #[cfg(test)]
204 mod tests;
205 pub(super) use racy::LazyKey;
206 pub(super) use moto_rt::tls::{Key, get, set};
207 use moto_rt::tls::{create, destroy};
208 }
209 _ => {}
210 }
211}
212
213#[inline]
217#[allow(dead_code)]
218fn abort_on_dtor_unwind(f: impl FnOnce()) {
219 let guard = DtorUnwindGuard;
221 f();
222 core::mem::forget(guard);
223
224 struct DtorUnwindGuard;
225 impl Drop for DtorUnwindGuard {
226 #[inline]
227 fn drop(&mut self) {
228 rtabort!("thread local panicked on drop");
231 }
232 }
233}