Skip to content

Commit c1608c1

Browse files
Auto merge of #150131 - sardok:realstd_thread_variables, r=<try>
Use realstd current thread static variables in tests try-job: aarch64-gnu-llvm-20-1
2 parents 3fda0e4 + abbbd77 commit c1608c1

9 files changed

Lines changed: 125 additions & 27 deletions

File tree

library/std/src/io/stdio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1287,4 +1287,4 @@ pub fn _eprint(args: fmt::Arguments<'_>) {
12871287
}
12881288

12891289
#[cfg(test)]
1290-
pub use realstd::io::{_eprint, _print};
1290+
pub use realstd::test_internals::{_eprint, _print};

library/std/src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,18 @@
255255
#![allow(unused_features)]
256256
//
257257
// Features:
258-
#![cfg_attr(test, feature(internal_output_capture, print_internals, update_panic_count, rt))]
258+
#![cfg_attr(
259+
test,
260+
feature(
261+
internal_output_capture,
262+
print_internals,
263+
update_panic_count,
264+
rt,
265+
thread_current_internals,
266+
thread_local_internals,
267+
thread_local_internal_pointer
268+
)
269+
)]
259270
#![cfg_attr(
260271
all(target_vendor = "fortanix", target_env = "sgx"),
261272
feature(slice_index_methods, coerce_unsized, sgx_platform)
@@ -361,6 +372,7 @@
361372
#![feature(str_internals)]
362373
#![feature(sync_unsafe_cell)]
363374
#![feature(temporary_niche_types)]
375+
#![feature(test_internals)]
364376
#![feature(ub_checks)]
365377
#![feature(used_with_arg)]
366378
// tidy-alphabetical-end
@@ -758,3 +770,8 @@ mod sealed {
758770
#[cfg(test)]
759771
#[allow(dead_code)] // Not used in all configurations.
760772
pub(crate) mod test_helpers;
773+
774+
#[doc(hidden)]
775+
#[unstable(feature = "test_internals", issue = "none")]
776+
#[cfg(not(test))]
777+
pub mod test_internals;

library/std/src/panicking.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::panic::{Location, PanicPayload};
1414
// make sure to use the stderr output configured
1515
// by libtest in the real copy of std
1616
#[cfg(test)]
17-
use realstd::io::try_set_output_capture;
17+
use realstd::test_internals::try_set_output_capture;
1818

1919
use crate::any::Any;
2020
#[cfg(not(test))]
@@ -487,7 +487,7 @@ pub mod panic_count {
487487
}
488488

489489
#[cfg(test)]
490-
pub use realstd::rt::panic_count;
490+
pub use realstd::test_internals::panic_count;
491491

492492
/// Invoke a closure, capturing the cause of an unwinding panic if one occurs.
493493
#[cfg(panic = "immediate-abort")]

library/std/src/sys/thread_local/native/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,17 @@ pub macro thread_local_inner {
114114
pub(crate) macro local_pointer {
115115
() => {},
116116
($vis:vis static $name:ident; $($rest:tt)*) => {
117+
118+
#[doc(hidden)]
119+
#[unstable(feature = "thread_local_internal_pointer", issue = "none")]
117120
#[thread_local]
118121
$vis static $name: $crate::sys::thread_local::LocalPointer = $crate::sys::thread_local::LocalPointer::__new();
119122
$crate::sys::thread_local::local_pointer! { $($rest)* }
120123
},
121124
}
122125

123-
pub(crate) struct LocalPointer {
126+
#[allow(missing_debug_implementations)]
127+
pub struct LocalPointer {
124128
p: Cell<*mut ()>,
125129
}
126130

library/std/src/sys/thread_local/no_threads.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,15 @@ unsafe impl<T> Sync for LazyStorage<T> {}
123123
pub(crate) macro local_pointer {
124124
() => {},
125125
($vis:vis static $name:ident; $($rest:tt)*) => {
126+
#[doc(hidden)]
127+
#[unstable(feature = "thread_local_internal_pointer", issue = "none")]
126128
$vis static $name: $crate::sys::thread_local::LocalPointer = $crate::sys::thread_local::LocalPointer::__new();
127129
$crate::sys::thread_local::local_pointer! { $($rest)* }
128130
},
129131
}
130132

131-
pub(crate) struct LocalPointer {
133+
#[allow(missing_debug_implementations)]
134+
pub struct LocalPointer {
132135
p: Cell<*mut ()>,
133136
}
134137

library/std/src/sys/thread_local/os.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,15 @@ unsafe extern "C" fn destroy_value<T: 'static, const ALIGN: usize>(ptr: *mut u8)
265265
pub(crate) macro local_pointer {
266266
() => {},
267267
($vis:vis static $name:ident; $($rest:tt)*) => {
268+
#[doc(hidden)]
269+
#[unstable(feature = "thread_local_internal_pointer", issue = "none")]
268270
$vis static $name: $crate::sys::thread_local::LocalPointer = $crate::sys::thread_local::LocalPointer::__new();
269271
$crate::sys::thread_local::local_pointer! { $($rest)* }
270272
},
271273
}
272274

273-
pub(crate) struct LocalPointer {
275+
#[allow(missing_debug_implementations)]
276+
pub struct LocalPointer {
274277
key: LazyKey,
275278
}
276279

library/std/src/test_internals.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// A collection of common re-exports to be used by the test version of this crate.
2+
pub use crate::io::{_eprint, _print, try_set_output_capture};
3+
pub use crate::rt::panic_count;
4+
pub use crate::thread::current::CURRENT as CURRENT_THREAD;
5+
6+
cfg_select! {
7+
target_thread_local => {
8+
pub use crate::thread::current::id::ID as CURRENT_THREAD_ID;
9+
}
10+
target_pointer_width = "16" => {
11+
pub use crate::thread::current::id::ID0 as CURRENT_THREAD_ID0;
12+
pub use crate::thread::current::id::ID16 as CURRENT_THREAD_ID16;
13+
pub use crate::thread::current::id::ID32 as CURRENT_THREAD_ID32;
14+
pub use crate::thread::current::id::ID48 as CURRENT_THREAD_ID48;
15+
}
16+
target_pointer_width = "32" => {
17+
pub use crate::thread::current::id::ID0 as CURRENT_THREAD_ID0;
18+
pub use crate::thread::current::id::ID32 as CURRENT_THREAD_ID32;
19+
}
20+
_ => {
21+
pub use crate::thread::current::id::ID as CURRENT_THREAD_ID;
22+
}
23+
}

library/std/src/thread/current.rs

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,73 @@ use super::thread::Thread;
44
use crate::mem::ManuallyDrop;
55
use crate::ptr;
66
use crate::sys::thread as imp;
7-
use crate::sys::thread_local::local_pointer;
87

98
const NONE: *mut () = ptr::null_mut();
109
const BUSY: *mut () = ptr::without_provenance_mut(1);
1110
const DESTROYED: *mut () = ptr::without_provenance_mut(2);
1211

13-
local_pointer! {
14-
static CURRENT;
12+
cfg_select! {
13+
test => {
14+
use realstd::test_internals::CURRENT_THREAD as CURRENT;
15+
}
16+
_ => {
17+
use crate::sys::thread_local::local_pointer;
18+
19+
local_pointer! {
20+
pub static CURRENT;
21+
}
22+
}
1523
}
1624

1725
/// Persistent storage for the thread ID.
1826
///
1927
/// We store the thread ID so that it never gets destroyed during the lifetime
2028
/// of a thread, either using `#[thread_local]` or multiple `local_pointer!`s.
21-
pub(super) mod id {
29+
pub mod id {
2230
use super::*;
2331

2432
cfg_select! {
2533
target_thread_local => {
26-
use crate::cell::Cell;
34+
cfg_select! {
35+
test => {
36+
use realstd::test_internals::CURRENT_THREAD_ID as ID;
37+
}
38+
_ => {
39+
use crate::cell::Cell;
2740

28-
#[thread_local]
29-
static ID: Cell<Option<ThreadId>> = Cell::new(None);
41+
#[thread_local]
42+
pub static ID: Cell<Option<u64>> = Cell::new(None);
3043

44+
}
45+
}
3146
pub(super) const CHEAP: bool = true;
3247

3348
pub(crate) fn get() -> Option<ThreadId> {
34-
ID.get()
49+
ID.get().and_then(ThreadId::from_u64)
3550
}
3651

3752
pub(super) fn set(id: ThreadId) {
38-
ID.set(Some(id))
53+
ID.set(Some(id.as_u64().get()))
3954
}
4055
}
4156
target_pointer_width = "16" => {
42-
local_pointer! {
43-
static ID0;
44-
static ID16;
45-
static ID32;
46-
static ID48;
57+
cfg_select! {
58+
test => {
59+
use realstd::test_internals::CURRENT_THREAD_ID0 as ID0;
60+
use realstd::test_internals::CURRENT_THREAD_ID16 as ID16;
61+
use realstd::test_internals::CURRENT_THREAD_ID32 as ID32;
62+
use realstd::test_internals::CURRENT_THREAD_ID48 as ID48;
63+
}
64+
_ => {
65+
use crate::sys::thread_local::local_pointer;
66+
67+
local_pointer! {
68+
pub static ID0;
69+
pub static ID16;
70+
pub static ID32;
71+
pub static ID48;
72+
}
73+
}
4774
}
4875

4976
pub(super) const CHEAP: bool = false;
@@ -65,9 +92,19 @@ pub(super) mod id {
6592
}
6693
}
6794
target_pointer_width = "32" => {
68-
local_pointer! {
69-
static ID0;
70-
static ID32;
95+
cfg_select! {
96+
test => {
97+
use realstd::test_internals::CURRENT_THREAD_ID0 as ID0;
98+
use realstd::test_internals::CURRENT_THREAD_ID32 as ID32;
99+
}
100+
_ => {
101+
use crate::sys::thread_local::local_pointer;
102+
103+
local_pointer! {
104+
pub static ID0;
105+
pub static ID32;
106+
}
107+
}
71108
}
72109

73110
pub(super) const CHEAP: bool = false;
@@ -85,8 +122,17 @@ pub(super) mod id {
85122
}
86123
}
87124
_ => {
88-
local_pointer! {
89-
static ID;
125+
cfg_select! {
126+
test => {
127+
use realstd::test_internals::CURRENT_THREAD_ID as ID;
128+
}
129+
_ => {
130+
use crate::sys::thread_local::local_pointer;
131+
132+
local_pointer! {
133+
pub static ID;
134+
}
135+
}
90136
}
91137

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

library/std/src/thread/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ use crate::any::Any;
161161
#[macro_use]
162162
mod local;
163163
mod builder;
164-
mod current;
164+
#[doc(hidden)]
165+
#[unstable(feature = "thread_current_internals", issue = "none")]
166+
pub(crate) mod current;
165167
mod functions;
166168
mod id;
167169
mod join_handle;

0 commit comments

Comments
 (0)