Skip to content

Commit

Permalink
Auto merge of rust-lang#116123 - joboet:rewrite_native_tls, r=<try>
Browse files Browse the repository at this point in the history
Rewrite native thread-local storage

(part of rust-lang#110897)

The current native thread-local storage implementation has become quite messy, uses indescriptive names and unnecessarily adds code to the macro expansion. This PR tries to fix that by using a new implementation that also allows more layout optimizations and potentially increases performance by eliminating unnecessary TLS accesses.

This does not change the recursive initialization behaviour I described in [this comment](rust-lang#110897 (comment)), so it should be a library-only change. Changing that behaviour should be quite easy now, however.

r? `@m-ou-se`
`@rustbot` label +T-libs
  • Loading branch information
bors committed Mar 16, 2024
2 parents 774ae59 + a087a37 commit 503706f
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 355 deletions.
432 changes: 244 additions & 188 deletions library/std/src/sys/thread_local/fast_local.rs

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion library/std/src/sys/thread_local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cfg_if::cfg_if! {
#[doc(hidden)]
mod fast_local;
#[doc(hidden)]
pub use fast_local::{Key, thread_local_inner};
pub use fast_local::{Storage, take_or_call, thread_local_inner};
} else {
#[doc(hidden)]
mod os_local;
Expand All @@ -24,6 +24,9 @@ cfg_if::cfg_if! {
}
}

// Not used by the fast-local TLS anymore.
// FIXME(#110897): remove this.
#[allow(unused)]
mod lazy {
use crate::cell::UnsafeCell;
use crate::hint;
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ cfg_if::cfg_if! {
#[doc(hidden)]
#[unstable(feature = "thread_local_internals", issue = "none")]
pub mod local_impl {
pub use crate::sys::thread_local::{thread_local_inner, Key, abort_on_dtor_unwind};
pub use crate::sys::thread_local::*;
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/tools/tidy/src/issues.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4009,8 +4009,6 @@
"ui/test-attrs/issue-53675-a-test-called-panic.rs",
"ui/threads-sendsync/issue-24313.rs",
"ui/threads-sendsync/issue-29488.rs",
"ui/threads-sendsync/issue-43733-2.rs",
"ui/threads-sendsync/issue-43733.rs",
"ui/threads-sendsync/issue-4446.rs",
"ui/threads-sendsync/issue-4448.rs",
"ui/threads-sendsync/issue-8827.rs",
Expand Down
31 changes: 18 additions & 13 deletions tests/ui/suggestions/missing-lifetime-specifier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// different number of duplicated diagnostics on different targets
//@ only-x86_64
//@ only-linux
// The specific errors produced depend the thread-local implementation.
// Run only on platforms with "fast" TLS.
//@ ignore-windows FIXME(#84933)
//@ ignore-wasm globals are used instead of thread locals
//@ ignore-emscripten globals are used instead of thread locals
//@ ignore-android does not use #[thread_local]
//@ ignore-nto does not use #[thread_local]
// Different number of duplicated diagnostics on different targets
//@ compile-flags: -Zdeduplicate-diagnostics=yes

#![allow(bare_trait_objects)]
Expand All @@ -20,31 +25,31 @@ pub union Qux<'t, 'k, I> {
trait Tar<'t, 'k, I> {}

thread_local! {
//~^ ERROR lifetime may not live long enough
//~| ERROR lifetime may not live long enough
//~^ ERROR borrowed data escapes outside of function
//~| ERROR borrowed data escapes outside of function
static a: RefCell<HashMap<i32, Vec<Vec<Foo>>>> = RefCell::new(HashMap::new());
//~^ ERROR missing lifetime specifiers
//~| ERROR missing lifetime specifiers
}
thread_local! {
//~^ ERROR lifetime may not live long enough
//~| ERROR lifetime may not live long enough
//~| ERROR lifetime may not live long enough
//~^ ERROR borrowed data escapes outside of function
//~| ERROR borrowed data escapes outside of function
//~| ERROR borrowed data escapes outside of function
static b: RefCell<HashMap<i32, Vec<Vec<&Bar>>>> = RefCell::new(HashMap::new());
//~^ ERROR missing lifetime specifiers
//~| ERROR missing lifetime specifiers
}
thread_local! {
//~^ ERROR lifetime may not live long enough
//~| ERROR lifetime may not live long enough
//~^ ERROR borrowed data escapes outside of function
//~| ERROR borrowed data escapes outside of function
static c: RefCell<HashMap<i32, Vec<Vec<Qux<i32>>>>> = RefCell::new(HashMap::new());
//~^ ERROR missing lifetime specifiers
//~| ERROR missing lifetime specifiers
}
thread_local! {
//~^ ERROR lifetime may not live long enough
//~| ERROR lifetime may not live long enough
//~| ERROR lifetime may not live long enough
//~^ ERROR borrowed data escapes outside of function
//~| ERROR borrowed data escapes outside of function
//~| ERROR borrowed data escapes outside of function
static d: RefCell<HashMap<i32, Vec<Vec<&Tar<i32>>>>> = RefCell::new(HashMap::new());
//~^ ERROR missing lifetime specifiers
//~| ERROR missing lifetime specifiers
Expand Down
Loading

0 comments on commit 503706f

Please sign in to comment.