Skip to content

Commit de909c1

Browse files
committed
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 7de42ba commit de909c1

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

tests/pass-dep/concurrency/tls_pthread_drop_order.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//@ignore-target-windows: No pthreads on Windows
22
//! Test that pthread_key destructors are run in the right order.
33
//! Note that these are *not* used by actual `thread_local!` on Linux! Those use
4-
//! `thread_local_dtor::register_dtor` from the stdlib instead. In Miri this hits the fallback path
5-
//! in `register_dtor_fallback`, which uses a *single* pthread_key to manage a thread-local list of
6-
//! dtors to call.
4+
//! `destructors::register` from the stdlib instead. In Miri this ends up hitting
5+
//! the fallback path in `guard::key::enable`, which uses a *single* pthread_key
6+
//! to manage a thread-local list of dtors to call.
77
88
use std::mem;
99
use std::ptr;

0 commit comments

Comments
 (0)