Restructure and rename thread local things in std.

This commit is contained in:
Mara Bos 2023-04-26 21:02:29 +02:00
parent 8763965a2c
commit fba5cfe482
6 changed files with 303 additions and 331 deletions

View file

@ -1,13 +1,14 @@
use super::lazy::LazyKeyInner;
use crate::cell::Cell;
use crate::sys::thread_local_dtor::register_dtor;
use crate::{fmt, mem, panic};
#[doc(hidden)] #[doc(hidden)]
#[macro_export] #[allow_internal_unstable(thread_local_internals, cfg_target_thread_local, thread_local)]
#[allow_internal_unstable(
thread_local_internals,
cfg_target_thread_local,
thread_local,
libstd_thread_internals
)]
#[allow_internal_unsafe] #[allow_internal_unsafe]
macro_rules! __thread_local_inner { #[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 // used to generate the `LocalKey` value for const-initialized thread locals
(@key $t:ty, const $init:expr) => {{ (@key $t:ty, const $init:expr) => {{
#[cfg_attr(not(bootstrap), inline)] #[cfg_attr(not(bootstrap), inline)]
@ -49,7 +50,7 @@ macro_rules! __thread_local_inner {
// 0 == we haven't registered a destructor, so do // 0 == we haven't registered a destructor, so do
// so now. // so now.
0 => { 0 => {
$crate::thread::__LocalKeyInner::<$t>::register_dtor( $crate::thread::local_impl::Key::<$t>::register_dtor(
$crate::ptr::addr_of_mut!(VAL) as *mut $crate::primitive::u8, $crate::ptr::addr_of_mut!(VAL) as *mut $crate::primitive::u8,
destroy, destroy,
); );
@ -69,7 +70,7 @@ macro_rules! __thread_local_inner {
unsafe { unsafe {
$crate::thread::LocalKey::new(__getit) $crate::thread::LocalKey::new(__getit)
} }
}}; }},
// used to generate the `LocalKey` value for `thread_local!` // used to generate the `LocalKey` value for `thread_local!`
(@key $t:ty, $init:expr) => { (@key $t:ty, $init:expr) => {
@ -82,8 +83,8 @@ macro_rules! __thread_local_inner {
init: $crate::option::Option<&mut $crate::option::Option<$t>>, init: $crate::option::Option<&mut $crate::option::Option<$t>>,
) -> $crate::option::Option<&'static $t> { ) -> $crate::option::Option<&'static $t> {
#[thread_local] #[thread_local]
static __KEY: $crate::thread::__LocalKeyInner<$t> = static __KEY: $crate::thread::local_impl::Key<$t> =
$crate::thread::__LocalKeyInner::<$t>::new(); $crate::thread::local_impl::Key::<$t>::new();
// FIXME: remove the #[allow(...)] marker when macros don't // FIXME: remove the #[allow(...)] marker when macros don't
// raise warning for missing/extraneous unsafe blocks anymore. // raise warning for missing/extraneous unsafe blocks anymore.
@ -107,19 +108,12 @@ macro_rules! __thread_local_inner {
$crate::thread::LocalKey::new(__getit) $crate::thread::LocalKey::new(__getit)
} }
} }
}; },
($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $($init:tt)*) => { ($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $($init:tt)*) => {
$(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> = $(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> =
$crate::__thread_local_inner!(@key $t, $($init)*); $crate::thread::local_impl::thread_local_inner!(@key $t, $($init)*);
},
} }
}
#[doc(hidden)]
pub mod fast {
use super::super::lazy::LazyKeyInner;
use crate::cell::Cell;
use crate::sys::thread_local_dtor::register_dtor;
use crate::{fmt, mem, panic};
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
enum DtorState { enum DtorState {
@ -251,4 +245,3 @@ pub mod fast {
rtabort!("thread local panicked on drop"); rtabort!("thread local panicked on drop");
} }
} }
}

View file

@ -1,35 +1,24 @@
//! The following module declarations are outside cfg_if because the internal
//! `__thread_local_internal` macro does not seem to be exported properly when using cfg_if
#![unstable(feature = "thread_local_internals", reason = "should not be necessary", issue = "none")] #![unstable(feature = "thread_local_internals", reason = "should not be necessary", issue = "none")]
#[cfg(all(target_thread_local, not(all(target_family = "wasm", not(target_feature = "atomics")))))]
mod fast_local;
#[cfg(all(
not(target_thread_local),
not(all(target_family = "wasm", not(target_feature = "atomics")))
))]
mod os_local;
#[cfg(all(target_family = "wasm", not(target_feature = "atomics")))]
mod static_local;
#[cfg(not(test))]
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(all(target_family = "wasm", not(target_feature = "atomics")))] { if #[cfg(all(target_family = "wasm", not(target_feature = "atomics")))] {
#[doc(hidden)] #[doc(hidden)]
pub use static_local::statik::Key; mod static_local;
} else if #[cfg(all(target_thread_local, not(all(target_family = "wasm", not(target_feature = "atomics")))))] {
#[doc(hidden)] #[doc(hidden)]
pub use fast_local::fast::Key; pub use static_local::{Key, thread_local_inner};
} else if #[cfg(all(not(target_thread_local), not(all(target_family = "wasm", not(target_feature = "atomics")))))] { } else if #[cfg(all(target_thread_local))] {
#[doc(hidden)] #[doc(hidden)]
pub use os_local::os::Key; mod fast_local;
#[doc(hidden)]
pub use fast_local::{Key, thread_local_inner};
} else {
#[doc(hidden)]
mod os_local;
#[doc(hidden)]
pub use os_local::{Key, thread_local_inner};
} }
} }
#[doc(hidden)]
#[cfg(test)]
pub use realstd::thread::__LocalKeyInner as Key;
mod lazy { mod lazy {
use crate::cell::UnsafeCell; use crate::cell::UnsafeCell;
use crate::hint; use crate::hint;

View file

@ -1,13 +1,14 @@
use super::lazy::LazyKeyInner;
use crate::cell::Cell;
use crate::sys_common::thread_local_key::StaticKey as OsStaticKey;
use crate::{fmt, marker, panic, ptr};
#[doc(hidden)] #[doc(hidden)]
#[macro_export] #[allow_internal_unstable(thread_local_internals)]
#[allow_internal_unstable(
thread_local_internals,
cfg_target_thread_local,
thread_local,
libstd_thread_internals
)]
#[allow_internal_unsafe] #[allow_internal_unsafe]
macro_rules! __thread_local_inner { #[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 // used to generate the `LocalKey` value for const-initialized thread locals
(@key $t:ty, const $init:expr) => {{ (@key $t:ty, const $init:expr) => {{
#[cfg_attr(not(bootstrap), inline)] #[cfg_attr(not(bootstrap), inline)]
@ -21,8 +22,8 @@ macro_rules! __thread_local_inner {
// same implementation as below for os thread locals. // same implementation as below for os thread locals.
#[inline] #[inline]
const fn __init() -> $t { INIT_EXPR } const fn __init() -> $t { INIT_EXPR }
static __KEY: $crate::thread::__LocalKeyInner<$t> = static __KEY: $crate::thread::local_impl::Key<$t> =
$crate::thread::__LocalKeyInner::new(); $crate::thread::local_impl::Key::new();
#[allow(unused_unsafe)] #[allow(unused_unsafe)]
unsafe { unsafe {
__KEY.get(move || { __KEY.get(move || {
@ -41,7 +42,7 @@ macro_rules! __thread_local_inner {
unsafe { unsafe {
$crate::thread::LocalKey::new(__getit) $crate::thread::LocalKey::new(__getit)
} }
}}; }},
// used to generate the `LocalKey` value for `thread_local!` // used to generate the `LocalKey` value for `thread_local!`
(@key $t:ty, $init:expr) => { (@key $t:ty, $init:expr) => {
@ -55,8 +56,8 @@ macro_rules! __thread_local_inner {
unsafe fn __getit( unsafe fn __getit(
init: $crate::option::Option<&mut $crate::option::Option<$t>>, init: $crate::option::Option<&mut $crate::option::Option<$t>>,
) -> $crate::option::Option<&'static $t> { ) -> $crate::option::Option<&'static $t> {
static __KEY: $crate::thread::__LocalKeyInner<$t> = static __KEY: $crate::thread::local_impl::Key<$t> =
$crate::thread::__LocalKeyInner::new(); $crate::thread::local_impl::Key::new();
// FIXME: remove the #[allow(...)] marker when macros don't // FIXME: remove the #[allow(...)] marker when macros don't
// raise warning for missing/extraneous unsafe blocks anymore. // raise warning for missing/extraneous unsafe blocks anymore.
@ -80,19 +81,12 @@ macro_rules! __thread_local_inner {
$crate::thread::LocalKey::new(__getit) $crate::thread::LocalKey::new(__getit)
} }
} }
}; },
($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $($init:tt)*) => { ($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $($init:tt)*) => {
$(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> = $(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> =
$crate::__thread_local_inner!(@key $t, $($init)*); $crate::thread::local_impl::thread_local_inner!(@key $t, $($init)*);
},
} }
}
#[doc(hidden)]
pub mod os {
use super::super::lazy::LazyKeyInner;
use crate::cell::Cell;
use crate::sys_common::thread_local_key::StaticKey as OsStaticKey;
use crate::{fmt, marker, panic, ptr};
/// Use a regular global static to store this key; the state provided will then be /// Use a regular global static to store this key; the state provided will then be
/// thread-local. /// thread-local.
@ -194,4 +188,3 @@ pub mod os {
rtabort!("thread local panicked on drop"); rtabort!("thread local panicked on drop");
} }
} }
}

View file

@ -1,13 +1,12 @@
use super::lazy::LazyKeyInner;
use crate::fmt;
#[doc(hidden)] #[doc(hidden)]
#[macro_export] #[allow_internal_unstable(thread_local_internals)]
#[allow_internal_unstable(
thread_local_internals,
cfg_target_thread_local,
thread_local,
libstd_thread_internals
)]
#[allow_internal_unsafe] #[allow_internal_unsafe]
macro_rules! __thread_local_inner { #[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 // used to generate the `LocalKey` value for const-initialized thread locals
(@key $t:ty, const $init:expr) => {{ (@key $t:ty, const $init:expr) => {{
#[inline] // see comments below #[inline] // see comments below
@ -30,7 +29,7 @@ macro_rules! __thread_local_inner {
unsafe { unsafe {
$crate::thread::LocalKey::new(__getit) $crate::thread::LocalKey::new(__getit)
} }
}}; }},
// used to generate the `LocalKey` value for `thread_local!` // used to generate the `LocalKey` value for `thread_local!`
(@key $t:ty, $init:expr) => { (@key $t:ty, $init:expr) => {
@ -41,8 +40,8 @@ macro_rules! __thread_local_inner {
unsafe fn __getit( unsafe fn __getit(
init: $crate::option::Option<&mut $crate::option::Option<$t>>, init: $crate::option::Option<&mut $crate::option::Option<$t>>,
) -> $crate::option::Option<&'static $t> { ) -> $crate::option::Option<&'static $t> {
static __KEY: $crate::thread::__LocalKeyInner<$t> = static __KEY: $crate::thread::local_impl::Key<$t> =
$crate::thread::__LocalKeyInner::new(); $crate::thread::local_impl::Key::new();
// FIXME: remove the #[allow(...)] marker when macros don't // FIXME: remove the #[allow(...)] marker when macros don't
// raise warning for missing/extraneous unsafe blocks anymore. // raise warning for missing/extraneous unsafe blocks anymore.
@ -66,19 +65,15 @@ macro_rules! __thread_local_inner {
$crate::thread::LocalKey::new(__getit) $crate::thread::LocalKey::new(__getit)
} }
} }
}; },
($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $($init:tt)*) => { ($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $($init:tt)*) => {
$(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> = $(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> =
$crate::__thread_local_inner!(@key $t, $($init)*); $crate::thread::local_impl::thread_local_inner!(@key $t, $($init)*);
} },
} }
/// On some targets like wasm there's no threads, so no need to generate /// On some targets like wasm there's no threads, so no need to generate
/// thread locals and we can instead just use plain statics! /// thread locals and we can instead just use plain statics!
#[doc(hidden)]
pub mod statik {
use super::super::lazy::LazyKeyInner;
use crate::fmt;
pub struct Key<T> { pub struct Key<T> {
inner: LazyKeyInner<T>, inner: LazyKeyInner<T>,
@ -112,4 +107,3 @@ pub mod statik {
Some(value) Some(value)
} }
} }
}

View file

@ -153,23 +153,23 @@ macro_rules! thread_local {
() => {}; () => {};
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }; $($rest:tt)*) => ( ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }; $($rest:tt)*) => (
$crate::__thread_local_inner!($(#[$attr])* $vis $name, $t, const $init); $crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, const $init);
$crate::thread_local!($($rest)*); $crate::thread_local!($($rest)*);
); );
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }) => ( ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }) => (
$crate::__thread_local_inner!($(#[$attr])* $vis $name, $t, const $init); $crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, const $init);
); );
// process multiple declarations // process multiple declarations
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => ( ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
$crate::__thread_local_inner!($(#[$attr])* $vis $name, $t, $init); $crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, $init);
$crate::thread_local!($($rest)*); $crate::thread_local!($($rest)*);
); );
// handle a single declaration // handle a single declaration
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => ( ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => (
$crate::__thread_local_inner!($(#[$attr])* $vis $name, $t, $init); $crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, $init);
); );
} }

View file

@ -204,9 +204,12 @@ pub use self::local::{AccessError, LocalKey};
// by the elf linker. "static" is for single-threaded platforms where a global // by the elf linker. "static" is for single-threaded platforms where a global
// static is sufficient. // static is sufficient.
// Implementation details used by the thread_local!{} macro.
#[doc(hidden)] #[doc(hidden)]
#[unstable(feature = "libstd_thread_internals", issue = "none")] #[unstable(feature = "thread_local_internals", issue = "none")]
pub use crate::sys::common::thread_local::Key as __LocalKeyInner; pub mod local_impl {
pub use crate::sys::common::thread_local::{thread_local_inner, Key};
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Builder // Builder