Skip to content

Commit

Permalink
Auto merge of #102783 - RalfJung:tls, r=thomcc
Browse files Browse the repository at this point in the history
sync thread_local key conditions exactly with what the macro uses

This makes the `cfg` in `mod.rs` syntactically the same as those in `local.rs`.

I don't think this should actually change anything, but seems better to be consistent?
I looked into this due to #102549, but this PR would make it *less* likely that `__OsLocalKeyInner` is going to get provided, so this cannot help with that issue.

r? `@thomcc`
  • Loading branch information
bors committed Oct 14, 2022
2 parents ee1c3b3 + 5218e24 commit 5819f41
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions library/std/src/sys/unix/thread_local_dtor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
target_os = "redox",
target_os = "emscripten"
))]
#[cfg_attr(target_family = "wasm", allow(unused))] // might remain unused depending on target details (e.g. wasm32-unknown-emscripten)
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
use crate::mem;
use crate::sys_common::thread_local_dtor::register_dtor_fallback;
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/unsupported/thread_local_dtor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![unstable(feature = "thread_local_internals", issue = "none")]

#[cfg_attr(target_family = "wasm", allow(unused))] // unused on wasm32-unknown-unknown
pub unsafe fn register_dtor(_t: *mut u8, _dtor: unsafe extern "C" fn(*mut u8)) {
// FIXME: right now there is no concept of "thread exit", but this is likely
// going to show up at some point in the form of an exported symbol that the
Expand Down
7 changes: 5 additions & 2 deletions library/std/src/thread/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ pub mod statik {
}

#[doc(hidden)]
#[cfg(target_thread_local)]
#[cfg(all(target_thread_local, not(all(target_family = "wasm", not(target_feature = "atomics"))),))]
pub mod fast {
use super::lazy::LazyKeyInner;
use crate::cell::Cell;
Expand Down Expand Up @@ -1037,7 +1037,10 @@ pub mod fast {
}

#[doc(hidden)]
#[cfg(not(target_thread_local))]
#[cfg(all(
not(target_thread_local),
not(all(target_family = "wasm", not(target_feature = "atomics"))),
))]
pub mod os {
use super::lazy::LazyKeyInner;
use crate::cell::Cell;
Expand Down
30 changes: 20 additions & 10 deletions library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@

#![stable(feature = "rust1", since = "1.0.0")]
#![deny(unsafe_op_in_unsafe_fn)]
// Under `test`, `__FastLocalKeyInner` seems unused.
#![cfg_attr(test, allow(dead_code))]

#[cfg(all(test, not(target_os = "emscripten")))]
mod tests;
Expand Down Expand Up @@ -192,32 +194,40 @@ pub use scoped::{scope, Scope, ScopedJoinHandle};
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::local::{AccessError, LocalKey};

// Select the type used by the thread_local! macro to access TLS keys. There
// are three types: "static", "fast", "OS". The "OS" thread local key
// 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.

#[unstable(feature = "libstd_thread_internals", issue = "none")]
#[cfg(target_thread_local)]
#[cfg(not(test))]
#[cfg(all(
target_thread_local,
not(all(target_family = "wasm", not(target_feature = "atomics"))),
))]
#[doc(hidden)]
pub use self::local::fast::Key as __FastLocalKeyInner;
#[unstable(feature = "libstd_thread_internals", issue = "none")]
#[cfg(target_thread_local)]
#[cfg(test)] // when building for tests, use real std's key
pub use realstd::thread::__FastLocalKeyInner;

// when building for tests, use real std's type
#[unstable(feature = "libstd_thread_internals", issue = "none")]
#[cfg(target_thread_local)]
#[cfg(test)]
pub use self::local::fast::Key as __FastLocalKeyInnerUnused; // we import this anyway to silence 'unused' warnings
#[cfg(all(
target_thread_local,
not(all(target_family = "wasm", not(target_feature = "atomics"))),
))]
pub use realstd::thread::__FastLocalKeyInner;

#[unstable(feature = "libstd_thread_internals", issue = "none")]
#[cfg(all(
not(target_thread_local),
not(all(target_family = "wasm", not(target_feature = "atomics"))),
))]
#[doc(hidden)]
#[cfg(not(target_thread_local))]
pub use self::local::os::Key as __OsLocalKeyInner;

#[unstable(feature = "libstd_thread_internals", issue = "none")]
#[cfg(all(target_family = "wasm", not(target_feature = "atomics")))]
#[doc(hidden)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/threads-sendsync/issue-43733-2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ignore-wasm32
// dont-check-compiler-stderr

#![feature(cfg_target_thread_local, thread_local_internals)]

// On platforms *without* `#[thread_local]`, use
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/threads-sendsync/issue-43733.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ignore-wasm32
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck
// normalize-stderr-test: "__FastLocalKeyInner::<T>::get" -> "$$LOCALKEYINNER::<T>::get"
// normalize-stderr-test: "__OsLocalKeyInner::<T>::get" -> "$$LOCALKEYINNER::<T>::get"

#![feature(thread_local)]
#![feature(cfg_target_thread_local, thread_local_internals)]

Expand Down

0 comments on commit 5819f41

Please sign in to comment.