Skip to content

Commit f3facf1

Browse files
committedJun 15, 2024
std: refactor the TLS implementation
As discovered by Mara in #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 #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
1 parent d2ad293 commit f3facf1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+720
-930
lines changed
 

‎library/std/src/sys/pal/hermit/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ pub mod pipe;
3333
pub mod process;
3434
pub mod stdio;
3535
pub mod thread;
36-
pub mod thread_local_dtor;
37-
#[path = "../unsupported/thread_local_key.rs"]
38-
pub mod thread_local_key;
3936
pub mod time;
4037

4138
use crate::io::ErrorKind;
@@ -98,7 +95,6 @@ pub unsafe extern "C" fn runtime_entry(
9895
argv: *const *const c_char,
9996
env: *const *const c_char,
10097
) -> ! {
101-
use thread_local_dtor::run_dtors;
10298
extern "C" {
10399
fn main(argc: isize, argv: *const *const c_char) -> i32;
104100
}
@@ -108,7 +104,7 @@ pub unsafe extern "C" fn runtime_entry(
108104

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

111-
run_dtors();
107+
crate::sys::thread_local::destructors::run();
112108
hermit_abi::exit(result);
113109
}
114110

‎library/std/src/sys/pal/hermit/thread.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![allow(dead_code)]
22

33
use super::hermit_abi;
4-
use super::thread_local_dtor::run_dtors;
54
use crate::ffi::CStr;
65
use crate::io;
76
use crate::mem;
@@ -50,7 +49,7 @@ impl Thread {
5049
Box::from_raw(ptr::with_exposed_provenance::<Box<dyn FnOnce()>>(main).cast_mut())();
5150

5251
// run all destructors
53-
run_dtors();
52+
crate::sys::thread_local::destructors::run();
5453
}
5554
}
5655
}

0 commit comments

Comments
 (0)