Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/std/src/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
19 changes: 18 additions & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
4 changes: 2 additions & 2 deletions library/std/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -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")]
Expand Down
6 changes: 5 additions & 1 deletion library/std/src/sys/thread_local/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()>,
}

Expand Down
5 changes: 4 additions & 1 deletion library/std/src/sys/thread_local/no_threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,15 @@ unsafe impl<T> Sync for LazyStorage<T> {}
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 ()>,
}

Expand Down
5 changes: 4 additions & 1 deletion library/std/src/sys/thread_local/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,15 @@ unsafe extern "C" fn destroy_value<T: 'static, const ALIGN: usize>(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,
}

Expand Down
23 changes: 23 additions & 0 deletions library/std/src/test_internals.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}
84 changes: 65 additions & 19 deletions library/std/src/thread/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<ThreadId>> = Cell::new(None);
#[thread_local]
pub static ID: Cell<Option<u64>> = Cell::new(None);

}
}
pub(super) const CHEAP: bool = true;

pub(crate) fn get() -> Option<ThreadId> {
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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading