Skip to content

Commit

Permalink
Rollup merge of rust-lang#126523 - joboet:the_great_big_tls_refactor,…
Browse files Browse the repository at this point in the history
… r=Mark-Simulacrum

std: refactor the TLS implementation

As discovered by Mara in rust-lang#110897, our TLS implementation is a total mess. In the past months, I have simplified the actual macros and their expansions, but the majority of the complexity comes from the platform-specific support code needed to create keys and register destructors. In keeping with rust-lang#117276, I have therefore moved all of the `thread_local_key`/`thread_local_dtor` modules to the `thread_local` module in `sys` and merged them into a new structure, so that future porters of `std` can simply mix-and-match the existing code instead of having to copy the same (bad) implementation everywhere. The new structure should become obvious when looking at `sys/thread_local/mod.rs`.

Unfortunately, the documentation changes associated with the refactoring have made this PR rather large. That said, this contains no functional changes except for two small ones:
* the key-based destructor fallback now, by virtue of sharing the implementation used by macOS and others, stores its list in a `#[thread_local]` static instead of in the key, eliminating one indirection layer and drastically simplifying its code.
* I've switched over ZKVM (tier 3) to use the same implementation as WebAssembly, as the implementation was just a way worse version of that

Please let me know if I can make this easier to review! I know these large PRs aren't optimal, but I couldn't think of any good intermediate steps.

`@rustbot` label +A-thread-locals
  • Loading branch information
compiler-errors authored Jun 23, 2024
2 parents ed0ba52 + 32f9b8b commit 18c2de0
Show file tree
Hide file tree
Showing 50 changed files with 722 additions and 931 deletions.
6 changes: 1 addition & 5 deletions library/std/src/sys/pal/hermit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ pub mod pipe;
pub mod process;
pub mod stdio;
pub mod thread;
pub mod thread_local_dtor;
#[path = "../unsupported/thread_local_key.rs"]
pub mod thread_local_key;
pub mod time;

use crate::io::ErrorKind;
Expand Down Expand Up @@ -97,7 +94,6 @@ pub unsafe extern "C" fn runtime_entry(
argv: *const *const c_char,
env: *const *const c_char,
) -> ! {
use thread_local_dtor::run_dtors;
extern "C" {
fn main(argc: isize, argv: *const *const c_char) -> i32;
}
Expand All @@ -107,7 +103,7 @@ pub unsafe extern "C" fn runtime_entry(

let result = main(argc as isize, argv);

run_dtors();
crate::sys::thread_local::destructors::run();
hermit_abi::exit(result);
}

Expand Down
3 changes: 1 addition & 2 deletions library/std/src/sys/pal/hermit/thread.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(dead_code)]

use super::hermit_abi;
use super::thread_local_dtor::run_dtors;
use crate::ffi::CStr;
use crate::io;
use crate::mem;
Expand Down Expand Up @@ -50,7 +49,7 @@ impl Thread {
Box::from_raw(ptr::with_exposed_provenance::<Box<dyn FnOnce()>>(main).cast_mut())();

// run all destructors
run_dtors();
crate::sys::thread_local::destructors::run();
}
}
}
Expand Down
29 changes: 0 additions & 29 deletions library/std/src/sys/pal/hermit/thread_local_dtor.rs

This file was deleted.

3 changes: 1 addition & 2 deletions library/std/src/sys/pal/itron/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::{
num::NonZero,
ptr::NonNull,
sync::atomic::{AtomicUsize, Ordering},
sys::thread_local_dtor::run_dtors,
time::Duration,
};

Expand Down Expand Up @@ -117,7 +116,7 @@ impl Thread {

// Run TLS destructors now because they are not
// called automatically for terminated tasks.
unsafe { run_dtors() };
unsafe { crate::sys::thread_local::destructors::run() };

let old_lifecycle = inner
.lifecycle
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/sgx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub mod pipe;
pub mod process;
pub mod stdio;
pub mod thread;
pub mod thread_local_key;
pub mod thread_parking;
pub mod time;
pub mod waitqueue;
Expand Down
2 changes: 0 additions & 2 deletions library/std/src/sys/pal/solid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ pub mod pipe;
pub mod process;
pub mod stdio;
pub use self::itron::thread;
pub mod thread_local_dtor;
pub mod thread_local_key;
pub use self::itron::thread_parking;
pub mod time;

Expand Down
43 changes: 0 additions & 43 deletions library/std/src/sys/pal/solid/thread_local_dtor.rs

This file was deleted.

21 changes: 0 additions & 21 deletions library/std/src/sys/pal/solid/thread_local_key.rs

This file was deleted.

3 changes: 0 additions & 3 deletions library/std/src/sys/pal/teeos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ pub mod process;
mod rand;
pub mod stdio;
pub mod thread;
pub mod thread_local_dtor;
#[path = "../unix/thread_local_key.rs"]
pub mod thread_local_key;
#[allow(non_upper_case_globals)]
#[path = "../unix/time.rs"]
pub mod time;
Expand Down
4 changes: 0 additions & 4 deletions library/std/src/sys/pal/teeos/thread_local_dtor.rs

This file was deleted.

2 changes: 0 additions & 2 deletions library/std/src/sys/pal/uefi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ pub mod pipe;
pub mod process;
pub mod stdio;
pub mod thread;
#[path = "../unsupported/thread_local_key.rs"]
pub mod thread_local_key;
pub mod time;

mod helpers;
Expand Down
2 changes: 0 additions & 2 deletions library/std/src/sys/pal/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ pub mod rand;
pub mod stack_overflow;
pub mod stdio;
pub mod thread;
pub mod thread_local_dtor;
pub mod thread_local_key;
pub mod thread_parking;
pub mod time;

Expand Down
126 changes: 0 additions & 126 deletions library/std/src/sys/pal/unix/thread_local_dtor.rs

This file was deleted.

29 changes: 0 additions & 29 deletions library/std/src/sys/pal/unix/thread_local_key.rs

This file was deleted.

3 changes: 0 additions & 3 deletions library/std/src/sys/pal/unsupported/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ pub mod pipe;
pub mod process;
pub mod stdio;
pub mod thread;
#[cfg(target_thread_local)]
pub mod thread_local_dtor;
pub mod thread_local_key;
pub mod time;

mod common;
Expand Down
10 changes: 0 additions & 10 deletions library/std/src/sys/pal/unsupported/thread_local_dtor.rs

This file was deleted.

Loading

0 comments on commit 18c2de0

Please sign in to comment.