diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index 2d80fe49e80a7..36aadd348ea04 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -1287,4 +1287,4 @@ pub fn _eprint(args: fmt::Arguments<'_>) { } #[cfg(test)] -pub use realstd::io::{_eprint, _print}; +pub use realstd::test_internals::{_eprint, _print}; diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 8fb1b1b05d20c..5060529001501 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -255,7 +255,18 @@ #![allow(unused_features)] // // Features: -#![cfg_attr(test, feature(internal_output_capture, print_internals, update_panic_count, rt))] +#![cfg_attr( + test, + feature( + internal_output_capture, + print_internals, + update_panic_count, + rt, + thread_current_internals, + thread_local_internals, + thread_local_internal_pointer + ) +)] #![cfg_attr( all(target_vendor = "fortanix", target_env = "sgx"), feature(slice_index_methods, coerce_unsized, sgx_platform) @@ -361,6 +372,7 @@ #![feature(str_internals)] #![feature(sync_unsafe_cell)] #![feature(temporary_niche_types)] +#![feature(test_internals)] #![feature(ub_checks)] #![feature(used_with_arg)] // tidy-alphabetical-end @@ -760,3 +772,8 @@ mod sealed { #[cfg(test)] #[allow(dead_code)] // Not used in all configurations. pub(crate) mod test_helpers; + +#[doc(hidden)] +#[unstable(feature = "test_internals", issue = "none")] +#[cfg(not(test))] +pub mod test_internals; diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index a4a974d0447b8..3f0cbb7136e4b 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -14,7 +14,7 @@ use core::panic::{Location, PanicPayload}; // make sure to use the stderr output configured // by libtest in the real copy of std #[cfg(test)] -use realstd::io::try_set_output_capture; +use realstd::test_internals::try_set_output_capture; use crate::any::Any; #[cfg(not(test))] @@ -487,7 +487,7 @@ pub mod panic_count { } #[cfg(test)] -pub use realstd::rt::panic_count; +pub use realstd::test_internals::panic_count; /// Invoke a closure, capturing the cause of an unwinding panic if one occurs. #[cfg(panic = "immediate-abort")] diff --git a/library/std/src/sys/thread_local/native/mod.rs b/library/std/src/sys/thread_local/native/mod.rs index 38b373be56c9d..999c7f080662f 100644 --- a/library/std/src/sys/thread_local/native/mod.rs +++ b/library/std/src/sys/thread_local/native/mod.rs @@ -114,13 +114,17 @@ pub macro thread_local_inner { pub(crate) macro local_pointer { () => {}, ($vis:vis static $name:ident; $($rest:tt)*) => { + + #[doc(hidden)] + #[unstable(feature = "thread_local_internal_pointer", issue = "none")] #[thread_local] $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 { +#[allow(missing_debug_implementations)] +pub struct LocalPointer { p: Cell<*mut ()>, } diff --git a/library/std/src/sys/thread_local/no_threads.rs b/library/std/src/sys/thread_local/no_threads.rs index 936d464be9f1c..323b8f1d229dd 100644 --- a/library/std/src/sys/thread_local/no_threads.rs +++ b/library/std/src/sys/thread_local/no_threads.rs @@ -123,12 +123,15 @@ unsafe impl Sync for LazyStorage {} pub(crate) macro local_pointer { () => {}, ($vis:vis static $name:ident; $($rest:tt)*) => { + #[doc(hidden)] + #[unstable(feature = "thread_local_internal_pointer", issue = "none")] $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 { +#[allow(missing_debug_implementations)] +pub struct LocalPointer { p: Cell<*mut ()>, } diff --git a/library/std/src/sys/thread_local/os.rs b/library/std/src/sys/thread_local/os.rs index 07b93a2cbbc34..e11770f48fb14 100644 --- a/library/std/src/sys/thread_local/os.rs +++ b/library/std/src/sys/thread_local/os.rs @@ -265,12 +265,15 @@ unsafe extern "C" fn destroy_value(ptr: *mut u8) pub(crate) macro local_pointer { () => {}, ($vis:vis static $name:ident; $($rest:tt)*) => { + #[doc(hidden)] + #[unstable(feature = "thread_local_internal_pointer", issue = "none")] $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 { +#[allow(missing_debug_implementations)] +pub struct LocalPointer { key: LazyKey, } diff --git a/library/std/src/test_internals.rs b/library/std/src/test_internals.rs new file mode 100644 index 0000000000000..073f33768d062 --- /dev/null +++ b/library/std/src/test_internals.rs @@ -0,0 +1,23 @@ +/// A collection of common re-exports to be used by the test version of this crate. +pub use crate::io::{_eprint, _print, try_set_output_capture}; +pub use crate::rt::panic_count; +pub use crate::thread::current::CURRENT as CURRENT_THREAD; + +cfg_select! { + target_thread_local => { + pub use crate::thread::current::id::ID as CURRENT_THREAD_ID; + } + target_pointer_width = "16" => { + pub use crate::thread::current::id::ID0 as CURRENT_THREAD_ID0; + pub use crate::thread::current::id::ID16 as CURRENT_THREAD_ID16; + pub use crate::thread::current::id::ID32 as CURRENT_THREAD_ID32; + pub use crate::thread::current::id::ID48 as CURRENT_THREAD_ID48; + } + target_pointer_width = "32" => { + pub use crate::thread::current::id::ID0 as CURRENT_THREAD_ID0; + pub use crate::thread::current::id::ID32 as CURRENT_THREAD_ID32; + } + _ => { + pub use crate::thread::current::id::ID as CURRENT_THREAD_ID; + } +} diff --git a/library/std/src/thread/current.rs b/library/std/src/thread/current.rs index 508e35cefe88f..8a2603f2e36f8 100644 --- a/library/std/src/thread/current.rs +++ b/library/std/src/thread/current.rs @@ -4,46 +4,73 @@ use super::thread::Thread; use crate::mem::ManuallyDrop; use crate::ptr; use crate::sys::thread as imp; -use crate::sys::thread_local::local_pointer; const NONE: *mut () = ptr::null_mut(); const BUSY: *mut () = ptr::without_provenance_mut(1); const DESTROYED: *mut () = ptr::without_provenance_mut(2); -local_pointer! { - static CURRENT; +cfg_select! { + test => { + use realstd::test_internals::CURRENT_THREAD as CURRENT; + } + _ => { + use crate::sys::thread_local::local_pointer; + + local_pointer! { + pub static CURRENT; + } + } } /// Persistent storage for the thread ID. /// /// We store the thread ID so that it never gets destroyed during the lifetime /// of a thread, either using `#[thread_local]` or multiple `local_pointer!`s. -pub(super) mod id { +pub mod id { use super::*; cfg_select! { target_thread_local => { - use crate::cell::Cell; + cfg_select! { + test => { + use realstd::test_internals::CURRENT_THREAD_ID as ID; + } + _ => { + use crate::cell::Cell; - #[thread_local] - static ID: Cell> = Cell::new(None); + #[thread_local] + pub static ID: Cell> = Cell::new(None); + } + } pub(super) const CHEAP: bool = true; pub(crate) fn get() -> Option { - ID.get() + ID.get().and_then(ThreadId::from_u64) } pub(super) fn set(id: ThreadId) { - ID.set(Some(id)) + ID.set(Some(id.as_u64().get())) } } target_pointer_width = "16" => { - local_pointer! { - static ID0; - static ID16; - static ID32; - static ID48; + cfg_select! { + test => { + use realstd::test_internals::CURRENT_THREAD_ID0 as ID0; + use realstd::test_internals::CURRENT_THREAD_ID16 as ID16; + use realstd::test_internals::CURRENT_THREAD_ID32 as ID32; + use realstd::test_internals::CURRENT_THREAD_ID48 as ID48; + } + _ => { + use crate::sys::thread_local::local_pointer; + + local_pointer! { + pub static ID0; + pub static ID16; + pub static ID32; + pub static ID48; + } + } } pub(super) const CHEAP: bool = false; @@ -65,9 +92,19 @@ pub(super) mod id { } } target_pointer_width = "32" => { - local_pointer! { - static ID0; - static ID32; + cfg_select! { + test => { + use realstd::test_internals::CURRENT_THREAD_ID0 as ID0; + use realstd::test_internals::CURRENT_THREAD_ID32 as ID32; + } + _ => { + use crate::sys::thread_local::local_pointer; + + local_pointer! { + pub static ID0; + pub static ID32; + } + } } pub(super) const CHEAP: bool = false; @@ -85,8 +122,17 @@ pub(super) mod id { } } _ => { - local_pointer! { - static ID; + cfg_select! { + test => { + use realstd::test_internals::CURRENT_THREAD_ID as ID; + } + _ => { + use crate::sys::thread_local::local_pointer; + + local_pointer! { + pub static ID; + } + } } pub(super) const CHEAP: bool = true; diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index 00aeb70e6e076..c146f2e945340 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -161,7 +161,9 @@ use crate::any::Any; #[macro_use] mod local; mod builder; -mod current; +#[doc(hidden)] +#[unstable(feature = "thread_current_internals", issue = "none")] +pub(crate) mod current; mod functions; mod id; mod join_handle;