Skip to content

Commit d98e174

Browse files
authored
Rollup merge of rust-lang#110946 - RalfJung:tls-realstd, r=m-ou-se
avoid duplicating TLS state between test std and realstd This basically re-lands rust-lang#100201 and rust-lang#106638, which got reverted by rust-lang#110861. This works around 2 Miri limitations: - Miri doesn't support the magic linker section that our Windows TLS support relies on, and instead knows where in std to find the symbol that stores the thread callback. - For macOS, Miri only supports at most one destructor to be registered per thread. The 2nd would not be very hard to fix (though the intended destructor order is unclear); the first would be a lot of work to fix. Neither of these is a problem for regular Rust code, but in the std test suite we have essentially 2 copies of the std code and then these both become issues. To avoid that we have the std test crate import the TLS code from the real std instead of having its own copy. r? ``````@m-ou-se``````
2 parents 74c4821 + d5e7ac5 commit d98e174

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#![unstable(feature = "thread_local_internals", reason = "should not be necessary", issue = "none")]
22

3+
// There are three thread-local implementations: "static", "fast", "OS".
4+
// The "OS" thread local key type is accessed via platform-specific API calls and is slow, while the
5+
// "fast" key type is accessed via code generated via LLVM, where TLS keys are set up by the linker.
6+
// "static" is for single-threaded platforms where a global static is sufficient.
7+
38
cfg_if::cfg_if! {
49
if #[cfg(all(target_family = "wasm", not(target_feature = "atomics")))] {
510
#[doc(hidden)]

library/std/src/sys/common/thread_local/os_local.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub macro thread_local_inner {
1818
) -> $crate::option::Option<&'static $t> {
1919
const INIT_EXPR: $t = $init;
2020

21-
// On platforms without `#[thread_local]` we fall back to the
21+
// On platforms without `#[thread_local]` we fall back to the
2222
// same implementation as below for os thread locals.
2323
#[inline]
2424
const fn __init() -> $t { INIT_EXPR }

library/std/src/thread/mod.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -193,22 +193,22 @@ pub use scoped::{scope, Scope, ScopedJoinHandle};
193193
#[macro_use]
194194
mod local;
195195

196-
#[stable(feature = "rust1", since = "1.0.0")]
197-
pub use self::local::{AccessError, LocalKey};
198-
199-
// Provide the type used by the thread_local! macro to access TLS keys. This
200-
// needs to be kept in sync with the macro itself (in `local.rs`).
201-
// There are three types: "static", "fast", "OS". The "OS" thread local key
202-
// type is accessed via platform-specific API calls and is slow, while the "fast"
203-
// key type is accessed via code generated via LLVM, where TLS keys are set up
204-
// by the elf linker. "static" is for single-threaded platforms where a global
205-
// static is sufficient.
206-
207-
// Implementation details used by the thread_local!{} macro.
208-
#[doc(hidden)]
209-
#[unstable(feature = "thread_local_internals", issue = "none")]
210-
pub mod local_impl {
211-
pub use crate::sys::common::thread_local::{thread_local_inner, Key};
196+
cfg_if::cfg_if! {
197+
if #[cfg(test)] {
198+
// Avoid duplicating the global state assoicated with thread-locals between this crate and
199+
// realstd. Miri relies on this.
200+
pub use realstd::thread::{local_impl, AccessError, LocalKey};
201+
} else {
202+
#[stable(feature = "rust1", since = "1.0.0")]
203+
pub use self::local::{AccessError, LocalKey};
204+
205+
// Implementation details used by the thread_local!{} macro.
206+
#[doc(hidden)]
207+
#[unstable(feature = "thread_local_internals", issue = "none")]
208+
pub mod local_impl {
209+
pub use crate::sys::common::thread_local::{thread_local_inner, Key};
210+
}
211+
}
212212
}
213213

214214
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)