Skip to content

Commit 223d16e

Browse files
committed
Auto merge of rust-lang#100201 - RalfJung:thread-local-key, r=thomcc
std: use realstd fast key when building tests Under `cfg(test)`, the `std` crate is not the actual standard library, just any old crate we are testing. It imports the real standard library as `realstd`, and then does some careful `cfg` magic so that the crate built for testing uses the `realstd` global state rather than having its own copy of that. However, this was not done for all global state hidden in std: the 'fast' version of thread-local keys, at least on some platforms, also involves some global state. Specifically its macOS version has this [`static REGISTERED`](https://github.com/rust-lang/rust/blob/bc63d5a26a65752fb105957d3235cc9c8cb0767f/library/std/src/sys/unix/thread_local_dtor.rs#L62) that would get duplicated. So this PR imports the 'fast' key type from `realstd` rather than using the local copy, to ensure its internal state (and that of the functions it calls) does not get duplicated. I also noticed that the `__OsLocalKeyInner` is unused under `cfg(target_thread_local)`, so I removed it for that configuration. There was a comment saying macOS picks between `__OsLocalKeyInner` and `__FastLocalKeyInner` at runtime, but I think that comment is outdated -- I found no trace of such a runtime switching mechanism, and the library still check-builds on apple targets with this PR. (I don't have a Mac so I cannot actually run it.)
2 parents ee285ea + d13699d commit 223d16e

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

library/std/src/sys_common/thread_local_key.rs

+2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ use crate::sys_common::mutex::StaticMutex;
6969
/// ```ignore (cannot-doctest-private-modules)
7070
/// use tls::os::{StaticKey, INIT};
7171
///
72+
/// // Use a regular global static to store the key.
7273
/// static KEY: StaticKey = INIT;
7374
///
75+
/// // The state provided via `get` and `set` is thread-local.
7476
/// unsafe {
7577
/// assert!(KEY.get().is_null());
7678
/// KEY.set(1 as *mut u8);

library/std/src/thread/local.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@ pub mod fast {
10361036
}
10371037

10381038
#[doc(hidden)]
1039+
#[cfg(not(target_thread_local))]
10391040
pub mod os {
10401041
use super::lazy::LazyKeyInner;
10411042
use crate::cell::Cell;
@@ -1044,6 +1045,8 @@ pub mod os {
10441045
use crate::ptr;
10451046
use crate::sys_common::thread_local_key::StaticKey as OsStaticKey;
10461047

1048+
/// Use a regular global static to store this key; the state provided will then be
1049+
/// thread-local.
10471050
pub struct Key<T> {
10481051
// OS-TLS key that we'll use to key off.
10491052
os: OsStaticKey,

library/std/src/thread/mod.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -192,21 +192,31 @@ pub use scoped::{scope, Scope, ScopedJoinHandle};
192192
#[stable(feature = "rust1", since = "1.0.0")]
193193
pub use self::local::{AccessError, LocalKey};
194194

195-
// The types used by the thread_local! macro to access TLS keys. Note that there
196-
// are two types, the "OS" type and the "fast" type. The OS thread local key
197-
// type is accessed via platform-specific API calls and is slow, while the fast
195+
// Select the type used by the thread_local! macro to access TLS keys. There
196+
// are three types: "static", "fast", "OS". The "OS" thread local key
197+
// type is accessed via platform-specific API calls and is slow, while the "fast"
198198
// key type is accessed via code generated via LLVM, where TLS keys are set up
199-
// by the elf linker. Note that the OS TLS type is always available: on macOS
200-
// the standard library is compiled with support for older platform versions
201-
// where fast TLS was not available; end-user code is compiled with fast TLS
202-
// where available, but both are needed.
199+
// by the elf linker. "static" is for single-threaded platforms where a global
200+
// static is sufficient.
203201

204202
#[unstable(feature = "libstd_thread_internals", issue = "none")]
205203
#[cfg(target_thread_local)]
204+
#[cfg(not(test))]
206205
#[doc(hidden)]
207206
pub use self::local::fast::Key as __FastLocalKeyInner;
207+
#[unstable(feature = "libstd_thread_internals", issue = "none")]
208+
#[cfg(target_thread_local)]
209+
#[cfg(test)] // when building for tests, use real std's key
210+
pub use realstd::thread::__FastLocalKeyInner;
211+
212+
#[unstable(feature = "libstd_thread_internals", issue = "none")]
213+
#[cfg(target_thread_local)]
214+
#[cfg(test)]
215+
pub use self::local::fast::Key as __FastLocalKeyInnerUnused; // we import this anyway to silence 'unused' warnings
216+
208217
#[unstable(feature = "libstd_thread_internals", issue = "none")]
209218
#[doc(hidden)]
219+
#[cfg(not(target_thread_local))]
210220
pub use self::local::os::Key as __OsLocalKeyInner;
211221
#[unstable(feature = "libstd_thread_internals", issue = "none")]
212222
#[cfg(all(target_family = "wasm", not(target_feature = "atomics")))]

0 commit comments

Comments
 (0)