Skip to content

Commit 5a3e2a4

Browse files
committed
Auto merge of #126523 - joboet:the_great_big_tls_refactor, r=Mark-Simulacrum
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
2 parents d371d17 + 50a02ed commit 5a3e2a4

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

+736
-931
lines changed

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

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

4037
use crate::io::ErrorKind;
@@ -97,7 +94,6 @@ pub unsafe extern "C" fn runtime_entry(
9794
argv: *const *const c_char,
9895
env: *const *const c_char,
9996
) -> ! {
100-
use thread_local_dtor::run_dtors;
10197
extern "C" {
10298
fn main(argc: isize, argv: *const *const c_char) -> i32;
10399
}
@@ -107,7 +103,7 @@ pub unsafe extern "C" fn runtime_entry(
107103

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

110-
run_dtors();
106+
crate::sys::thread_local::destructors::run();
111107
hermit_abi::exit(result);
112108
}
113109

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
}

library/std/src/sys/pal/hermit/thread_local_dtor.rs

-29
This file was deleted.

library/std/src/sys/pal/itron/thread.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::{
1515
num::NonZero,
1616
ptr::NonNull,
1717
sync::atomic::{AtomicUsize, Ordering},
18-
sys::thread_local_dtor::run_dtors,
1918
time::Duration,
2019
};
2120

@@ -117,7 +116,7 @@ impl Thread {
117116

118117
// Run TLS destructors now because they are not
119118
// called automatically for terminated tasks.
120-
unsafe { run_dtors() };
119+
unsafe { crate::sys::thread_local::destructors::run() };
121120

122121
let old_lifecycle = inner
123122
.lifecycle

library/std/src/sys/pal/sgx/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub mod pipe;
2626
pub mod process;
2727
pub mod stdio;
2828
pub mod thread;
29-
pub mod thread_local_key;
3029
pub mod thread_parking;
3130
pub mod time;
3231
pub mod waitqueue;

library/std/src/sys/pal/solid/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ pub mod pipe;
3333
pub mod process;
3434
pub mod stdio;
3535
pub use self::itron::thread;
36-
pub mod thread_local_dtor;
37-
pub mod thread_local_key;
3836
pub use self::itron::thread_parking;
3937
pub mod time;
4038

library/std/src/sys/pal/solid/thread_local_dtor.rs

-43
This file was deleted.

library/std/src/sys/pal/solid/thread_local_key.rs

-21
This file was deleted.

library/std/src/sys/pal/teeos/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ pub mod process;
2727
mod rand;
2828
pub mod stdio;
2929
pub mod thread;
30-
pub mod thread_local_dtor;
31-
#[path = "../unix/thread_local_key.rs"]
32-
pub mod thread_local_key;
3330
#[allow(non_upper_case_globals)]
3431
#[path = "../unix/time.rs"]
3532
pub mod time;

library/std/src/sys/pal/teeos/thread_local_dtor.rs

-4
This file was deleted.

library/std/src/sys/pal/uefi/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ pub mod pipe;
2828
pub mod process;
2929
pub mod stdio;
3030
pub mod thread;
31-
#[path = "../unsupported/thread_local_key.rs"]
32-
pub mod thread_local_key;
3331
pub mod time;
3432

3533
mod helpers;

library/std/src/sys/pal/unix/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ pub mod rand;
3333
pub mod stack_overflow;
3434
pub mod stdio;
3535
pub mod thread;
36-
pub mod thread_local_dtor;
37-
pub mod thread_local_key;
3836
pub mod thread_parking;
3937
pub mod time;
4038

library/std/src/sys/pal/unix/thread_local_dtor.rs

-126
This file was deleted.

library/std/src/sys/pal/unix/thread_local_key.rs

-29
This file was deleted.

library/std/src/sys/pal/unsupported/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ pub mod pipe;
1111
pub mod process;
1212
pub mod stdio;
1313
pub mod thread;
14-
#[cfg(target_thread_local)]
15-
pub mod thread_local_dtor;
16-
pub mod thread_local_key;
1714
pub mod time;
1815

1916
mod common;

library/std/src/sys/pal/unsupported/thread_local_dtor.rs

-10
This file was deleted.

0 commit comments

Comments
 (0)