From 9b889e919807c7f5c377c9c6b17d55276c8e3351 Mon Sep 17 00:00:00 2001 From: Thalia Archibald Date: Sat, 29 Mar 2025 14:43:32 -0700 Subject: Rename internal module from statik to no_threads This module is named in reference to the keyword, but the term is somewhat overloaded. Rename it to more clearly describe it and avoid the misspelling. --- library/std/src/sys/thread_local/mod.rs | 6 +- library/std/src/sys/thread_local/no_threads.rs | 125 +++++++++++++++++++++++++ library/std/src/sys/thread_local/statik.rs | 125 ------------------------- 3 files changed, 128 insertions(+), 128 deletions(-) create mode 100644 library/std/src/sys/thread_local/no_threads.rs delete mode 100644 library/std/src/sys/thread_local/statik.rs (limited to 'library/std/src') diff --git a/library/std/src/sys/thread_local/mod.rs b/library/std/src/sys/thread_local/mod.rs index 1ff13154b7b..fb7cb40625d 100644 --- a/library/std/src/sys/thread_local/mod.rs +++ b/library/std/src/sys/thread_local/mod.rs @@ -30,9 +30,9 @@ cfg_if::cfg_if! { target_os = "zkvm", target_os = "trusty", ))] { - mod statik; - pub use statik::{EagerStorage, LazyStorage, thread_local_inner}; - pub(crate) use statik::{LocalPointer, local_pointer}; + mod no_threads; + pub use no_threads::{EagerStorage, LazyStorage, thread_local_inner}; + pub(crate) use no_threads::{LocalPointer, local_pointer}; } else if #[cfg(target_thread_local)] { mod native; pub use native::{EagerStorage, LazyStorage, thread_local_inner}; diff --git a/library/std/src/sys/thread_local/no_threads.rs b/library/std/src/sys/thread_local/no_threads.rs new file mode 100644 index 00000000000..4da01a84acf --- /dev/null +++ b/library/std/src/sys/thread_local/no_threads.rs @@ -0,0 +1,125 @@ +//! On some targets like wasm there's no threads, so no need to generate +//! thread locals and we can instead just use plain statics! + +use crate::cell::{Cell, UnsafeCell}; +use crate::ptr; + +#[doc(hidden)] +#[allow_internal_unstable(thread_local_internals)] +#[allow_internal_unsafe] +#[unstable(feature = "thread_local_internals", issue = "none")] +#[rustc_macro_transparency = "semitransparent"] +pub macro thread_local_inner { + // used to generate the `LocalKey` value for const-initialized thread locals + (@key $t:ty, const $init:expr) => {{ + const __INIT: $t = $init; + + // NOTE: Please update the shadowing test in `tests/thread.rs` if these types are renamed. + unsafe { + $crate::thread::LocalKey::new(|_| { + static VAL: $crate::thread::local_impl::EagerStorage<$t> = + $crate::thread::local_impl::EagerStorage { value: __INIT }; + &VAL.value + }) + } + }}, + + // used to generate the `LocalKey` value for `thread_local!` + (@key $t:ty, $init:expr) => {{ + #[inline] + fn __init() -> $t { $init } + + unsafe { + use $crate::thread::LocalKey; + use $crate::thread::local_impl::LazyStorage; + + LocalKey::new(|init| { + static VAL: LazyStorage<$t> = LazyStorage::new(); + VAL.get(init, __init) + }) + } + }}, + ($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $($init:tt)*) => { + $(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> = + $crate::thread::local_impl::thread_local_inner!(@key $t, $($init)*); + }, +} + +#[allow(missing_debug_implementations)] +pub struct EagerStorage { + pub value: T, +} + +// SAFETY: the target doesn't have threads. +unsafe impl Sync for EagerStorage {} + +#[allow(missing_debug_implementations)] +pub struct LazyStorage { + value: UnsafeCell>, +} + +impl LazyStorage { + pub const fn new() -> LazyStorage { + LazyStorage { value: UnsafeCell::new(None) } + } + + /// Gets a pointer to the TLS value, potentially initializing it with the + /// provided parameters. + /// + /// The resulting pointer may not be used after reentrant inialialization + /// has occurred. + #[inline] + pub fn get(&'static self, i: Option<&mut Option>, f: impl FnOnce() -> T) -> *const T { + let value = unsafe { &*self.value.get() }; + match value { + Some(v) => v, + None => self.initialize(i, f), + } + } + + #[cold] + fn initialize(&'static self, i: Option<&mut Option>, f: impl FnOnce() -> T) -> *const T { + let value = i.and_then(Option::take).unwrap_or_else(f); + // Destroy the old value, after updating the TLS variable as the + // destructor might reference it. + // FIXME(#110897): maybe panic on recursive initialization. + unsafe { + self.value.get().replace(Some(value)); + } + // SAFETY: we just set this to `Some`. + unsafe { (*self.value.get()).as_ref().unwrap_unchecked() } + } +} + +// SAFETY: the target doesn't have threads. +unsafe impl Sync for LazyStorage {} + +#[rustc_macro_transparency = "semitransparent"] +pub(crate) macro local_pointer { + () => {}, + ($vis:vis static $name:ident; $($rest:tt)*) => { + $vis static $name: $crate::sys::thread_local::LocalPointer = $crate::sys::thread_local::LocalPointer::__new(); + $crate::sys::thread_local::local_pointer! { $($rest)* } + }, +} + +pub(crate) struct LocalPointer { + p: Cell<*mut ()>, +} + +impl LocalPointer { + pub const fn __new() -> LocalPointer { + LocalPointer { p: Cell::new(ptr::null_mut()) } + } + + pub fn get(&self) -> *mut () { + self.p.get() + } + + pub fn set(&self, p: *mut ()) { + self.p.set(p) + } +} + +// SAFETY: the target doesn't have threads. +unsafe impl Sync for LocalPointer {} diff --git a/library/std/src/sys/thread_local/statik.rs b/library/std/src/sys/thread_local/statik.rs deleted file mode 100644 index 4da01a84acf..00000000000 --- a/library/std/src/sys/thread_local/statik.rs +++ /dev/null @@ -1,125 +0,0 @@ -//! On some targets like wasm there's no threads, so no need to generate -//! thread locals and we can instead just use plain statics! - -use crate::cell::{Cell, UnsafeCell}; -use crate::ptr; - -#[doc(hidden)] -#[allow_internal_unstable(thread_local_internals)] -#[allow_internal_unsafe] -#[unstable(feature = "thread_local_internals", issue = "none")] -#[rustc_macro_transparency = "semitransparent"] -pub macro thread_local_inner { - // used to generate the `LocalKey` value for const-initialized thread locals - (@key $t:ty, const $init:expr) => {{ - const __INIT: $t = $init; - - // NOTE: Please update the shadowing test in `tests/thread.rs` if these types are renamed. - unsafe { - $crate::thread::LocalKey::new(|_| { - static VAL: $crate::thread::local_impl::EagerStorage<$t> = - $crate::thread::local_impl::EagerStorage { value: __INIT }; - &VAL.value - }) - } - }}, - - // used to generate the `LocalKey` value for `thread_local!` - (@key $t:ty, $init:expr) => {{ - #[inline] - fn __init() -> $t { $init } - - unsafe { - use $crate::thread::LocalKey; - use $crate::thread::local_impl::LazyStorage; - - LocalKey::new(|init| { - static VAL: LazyStorage<$t> = LazyStorage::new(); - VAL.get(init, __init) - }) - } - }}, - ($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $($init:tt)*) => { - $(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> = - $crate::thread::local_impl::thread_local_inner!(@key $t, $($init)*); - }, -} - -#[allow(missing_debug_implementations)] -pub struct EagerStorage { - pub value: T, -} - -// SAFETY: the target doesn't have threads. -unsafe impl Sync for EagerStorage {} - -#[allow(missing_debug_implementations)] -pub struct LazyStorage { - value: UnsafeCell>, -} - -impl LazyStorage { - pub const fn new() -> LazyStorage { - LazyStorage { value: UnsafeCell::new(None) } - } - - /// Gets a pointer to the TLS value, potentially initializing it with the - /// provided parameters. - /// - /// The resulting pointer may not be used after reentrant inialialization - /// has occurred. - #[inline] - pub fn get(&'static self, i: Option<&mut Option>, f: impl FnOnce() -> T) -> *const T { - let value = unsafe { &*self.value.get() }; - match value { - Some(v) => v, - None => self.initialize(i, f), - } - } - - #[cold] - fn initialize(&'static self, i: Option<&mut Option>, f: impl FnOnce() -> T) -> *const T { - let value = i.and_then(Option::take).unwrap_or_else(f); - // Destroy the old value, after updating the TLS variable as the - // destructor might reference it. - // FIXME(#110897): maybe panic on recursive initialization. - unsafe { - self.value.get().replace(Some(value)); - } - // SAFETY: we just set this to `Some`. - unsafe { (*self.value.get()).as_ref().unwrap_unchecked() } - } -} - -// SAFETY: the target doesn't have threads. -unsafe impl Sync for LazyStorage {} - -#[rustc_macro_transparency = "semitransparent"] -pub(crate) macro local_pointer { - () => {}, - ($vis:vis static $name:ident; $($rest:tt)*) => { - $vis static $name: $crate::sys::thread_local::LocalPointer = $crate::sys::thread_local::LocalPointer::__new(); - $crate::sys::thread_local::local_pointer! { $($rest)* } - }, -} - -pub(crate) struct LocalPointer { - p: Cell<*mut ()>, -} - -impl LocalPointer { - pub const fn __new() -> LocalPointer { - LocalPointer { p: Cell::new(ptr::null_mut()) } - } - - pub fn get(&self) -> *mut () { - self.p.get() - } - - pub fn set(&self, p: *mut ()) { - self.p.set(p) - } -} - -// SAFETY: the target doesn't have threads. -unsafe impl Sync for LocalPointer {} -- cgit 1.4.1-3-g733a5