Rollup merge of #110946 - RalfJung:tls-realstd, r=m-ou-se

avoid duplicating TLS state between test std and realstd

This basically re-lands https://github.com/rust-lang/rust/pull/100201 and https://github.com/rust-lang/rust/pull/106638, which got reverted by https://github.com/rust-lang/rust/pull/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``````
This commit is contained in:
Yuki Okushi 2023-05-05 12:46:25 +09:00 committed by GitHub
commit d98e174497
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 16 deletions

View file

@ -1,5 +1,10 @@
#![unstable(feature = "thread_local_internals", reason = "should not be necessary", issue = "none")]
// There are three thread-local implementations: "static", "fast", "OS".
// The "OS" thread local key type is accessed via platform-specific API calls and is slow, while the
// "fast" key type is accessed via code generated via LLVM, where TLS keys are set up by the linker.
// "static" is for single-threaded platforms where a global static is sufficient.
cfg_if::cfg_if! {
if #[cfg(all(target_family = "wasm", not(target_feature = "atomics")))] {
#[doc(hidden)]

View file

@ -18,7 +18,7 @@ pub macro thread_local_inner {
) -> $crate::option::Option<&'static $t> {
const INIT_EXPR: $t = $init;
// On platforms without `#[thread_local]` we fall back to the
// On platforms without `#[thread_local]` we fall back to the
// same implementation as below for os thread locals.
#[inline]
const fn __init() -> $t { INIT_EXPR }

View file

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