about summary refs log tree commit diff
path: root/library/std/src/sys/thread_local
AgeCommit message (Collapse)AuthorLines
2025-07-21Fix broken TLS destructors on 32-bit win7roblabla-3/+14
On the 32-bit win7 target, we use OS TLS instead of native TLS, due to issues with how the OS handles alignment. Unfortunately, this caused issues due to the TLS destructors not running, causing memory leaks among other problems. On Windows, to support OS TLS, the TlsAlloc family of function is used by Rust. This function does not support TLS destructors at all. However, rust has some code to emulate those destructors, by leveraging the TLS support functionality found in the MSVC CRT (specifically, in tlssup.c of the CRT). Specifically, the CRT provides the ability to register callbacks that are called (among other things) on thread destruction. By registering our own callback, we can run through a list of registered destructors functions to execute. To use this functionality, the user must do two things: 1. They must put the address to their callback in a section between `.CRT$XLB` and `.CRT$XLY`. 2. They must add a reference to `_tls_used` (or `__tls_used` on x86) to make sure the TLS support code in tlssup.c isn't garbage collected by the linker. Prior to this commit, this second bit wasn't being done properly by the Rust TLS support code. Instead of adding a reference to _tls_used, it instead had a reference to its own callback to prevent it from getting GC'd by the linker. While this is _also_ necessary, not having a reference on _tls_used made the entire support non-functional. This commit reworks the code to: 1. Add an unconditional `#[used]` attribute on the CALLBACK, which should be enough to prevent it from getting GC'd by the linker. 2. Add a reference to `_tls_used`, which should pull the TLS support code into the Rust programs and not let it be GC'd by the linker.
2025-06-03Rollup merge of #141455 - joboet:tls_exhaustion_abort, r=tgross35Matthias Krüger-11/+10
std: abort the process on failure to allocate a TLS key The panic machinery uses TLS, so panicking if no TLS keys are left can lead to infinite recursion (see https://github.com/rust-lang/rust/issues/140798#issuecomment-2872307377). Rather than having separate logic for the panic count and the thread name, just always abort the process if a TLS key allocation fails. This also has the benefit of aligning the key-based TLS implementation with the documentation, which does not mention that a panic could also occur because of resource exhaustion.
2025-05-30Address review comments.Orson Peters-0/+7
2025-05-28When replacing an old value we may not drop it in placeOrson Peters-12/+8
2025-05-28Add same unsafe bound on get_or_init_slowOrson Peters-2/+8
2025-05-28Do not panic, maintain old behaviorOrson Peters-25/+17
2025-05-28Improve safety comment, double-drop is not relevant hereOrson Peters-3/+4
2025-05-28Do not move thread-locals before droppingOrson Peters-32/+52
2025-05-23std: abort the process on failure to allocate a TLS keyjoboet-11/+10
The panic machinery uses TLS, so panicking if no TLS keys are left can lead to infinite recursion (see https://github.com/rust-lang/rust/issues/140798#issuecomment-2872307377). Rather than having separate logic for the panic count and the thread name, just always abort the process if a TLS key allocation fails. This also has the benefit of aligning the key-based TLS implementation with the documentation, which does not mention that a panic could also occur because of resource exhaustion.
2025-05-22docs: fix typosDannyyy93-2/+2
2025-04-27use generic Atomic type where possibleChristopher Durham-12/+12
in core/alloc/std only for now, and ignoring test files Co-authored-by: Pavel Grigorenko <GrigorenkoPV@ya.ru>
2025-04-11cfi: Remove #[no_sanitize(cfi)] for extern weak functionsBastian Kersting-3/+0
Previously (https://github.com/rust-lang/rust/pull/115200, https://github.com/rust-lang/rust/pull/138002), we added `#[no_sanitize(cfi)]` to all code paths that call to a weakly linked function. In https://github.com/rust-lang/rust/pull/138349 we fixed the root cause for this issue, which means we can now remove the corresponding attributes.
2025-04-05Rollup merge of #139121 - thaliaarchi:rename-thread_local-statik, r=NoratriebMatthias Krüger-3/+3
Rename internal module from `statik` to `no_threads` This module is named in reference to the keyword, but the term is somewhat overloaded. Rename it to more clearly describe it and avoid the misspelling.
2025-04-04Rename internal module from statik to no_threadsThalia Archibald-3/+3
This module is named in reference to the keyword, but the term is somewhat overloaded. Rename it to more clearly describe it and avoid the misspelling.
2025-03-18fix pthread-based tls on apple targetsAndrei Damian-0/+1
2025-03-10Remove custom TLS implementation for Trusty targetsNicole LeGare-39/+2
2025-03-10Apply rustc-0023-Add-Trusty-OS-support-to-Rust-std.patchNicole LeGare-0/+39
2025-03-06library: Use size_of from the prelude instead of importedThalia Archibald-7/+4
Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the prelude instead of importing or qualifying them. These functions were added to all preludes in Rust 1.80.
2025-02-17Remove obsolete MinGW ThinLTO+TLS workaroundKornel-3/+1
#109797 is fixed
2025-02-13std: Apply unsafe_attr_outside_unsafeEric Huss-2/+2
2025-02-09Mark extern blocks as unsafeMichael Goulet-4/+4
2025-02-09Mark link_section attr with unsafeMichael Goulet-1/+1
2024-12-10Rollup merge of #133472 - rust-wasi-web:master, r=joboetLeón Orell Valerian Liehr-1/+24
Run TLS destructors for wasm32-wasip1-threads The target wasm32-wasip1-threads has support for pthreads and allows registration of TLS destructors. For spawned threads, this registers Rust TLS destructors by creating a pthreads key with an attached destructor function. For the main thread, this registers an `atexit` handler to run the TLS destructors. try-job: test-various
2024-12-05Add libc funcitons only for wasm32-wasip1-threads.Sebastian Urban-1/+1
2024-12-05Fix compilation for wasm32-wasip1 (without threads).Sebastian Urban-2/+4
2024-12-03Use UNIX thread_local implementation for WASI.Sebastian Urban-75/+22
2024-11-27update cfgsBoxy-2/+0
2024-11-25Run TLS destructors for wasm32-wasip1-threadsSebastian Urban-0/+74
The target has support for pthreads and allows registration of TLS destructors.
2024-11-02Remove unintended linkHoutamelo-1/+1
Since `#[link_section]` is enclosed in braces, it was being confused with a link during docs compilation.
2024-10-25Re-do recursive const stability checksRalf Jung-2/+2
Fundamentally, we have *three* disjoint categories of functions: 1. const-stable functions 2. private/unstable functions that are meant to be callable from const-stable functions 3. functions that can make use of unstable const features This PR implements the following system: - `#[rustc_const_stable]` puts functions in the first category. It may only be applied to `#[stable]` functions. - `#[rustc_const_unstable]` by default puts functions in the third category. The new attribute `#[rustc_const_stable_indirect]` can be added to such a function to move it into the second category. - `const fn` without a const stability marker are in the second category if they are still unstable. They automatically inherit the feature gate for regular calls, it can now also be used for const-calls. Also, several holes in recursive const stability checking are being closed. There's still one potential hole that is hard to avoid, which is when MIR building automatically inserts calls to a particular function in stable functions -- which happens in the panic machinery. Those need to *not* be `rustc_const_unstable` (or manually get a `rustc_const_stable_indirect`) to be sure they follow recursive const stability. But that's a fairly rare and special case so IMO it's fine. The net effect of this is that a `#[unstable]` or unmarked function can be constified simply by marking it as `const fn`, and it will then be const-callable from stable `const fn` and subject to recursive const stability requirements. If it is publicly reachable (which implies it cannot be unmarked), it will be const-unstable under the same feature gate. Only if the function ever becomes `#[stable]` does it need a `#[rustc_const_unstable]` or `#[rustc_const_stable]` marker to decide if this should also imply const-stability. Adding `#[rustc_const_unstable]` is only needed for (a) functions that need to use unstable const lang features (including intrinsics), or (b) `#[stable]` functions that are not yet intended to be const-stable. Adding `#[rustc_const_stable]` is only needed for functions that are actually meant to be directly callable from stable const code. `#[rustc_const_stable_indirect]` is used to mark intrinsics as const-callable and for `#[rustc_const_unstable]` functions that are actually called from other, exposed-on-stable `const fn`. No other attributes are required.
2024-10-25Avoid use imports in thread_local_inner! in statikJeong YunWon-5/+4
Fixes #131863 for wasm targets All other macros were done in #131866, but this sub module is missed.
2024-10-18Avoid shadowing user provided types or type aliases in `thread_local!`许杰友 Jieyou Xu (Joe)-22/+26
By using qualified imports, i.e. `$crate::...::LocalKey`.
2024-10-02std: make `thread::current` available in all `thread_local!` destructorsjoboet-26/+175
2024-09-25Use `&raw` in the standard libraryJosh Stone-1/+1
Since the stabilization in #127679 has reached stage0, 1.82-beta, we can start using `&raw` freely, and even the soft-deprecated `ptr::addr_of!` and `ptr::addr_of_mut!` can stop allowing the unstable feature. I intentionally did not change any documentation or tests, but the rest of those macro uses are all now using `&raw const` or `&raw mut` in the standard library.
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-4/+4
2024-07-29Reformat `use` declarations.Nicholas Nethercote-15/+12
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-26Fix doc nitsJohn Arundel-5/+5
Many tiny changes to stdlib doc comments to make them consistent (for example "Returns foo", rather than "Return foo", per RFC1574), adding missing periods, paragraph breaks, backticks for monospace style, and other minor nits. https://github.com/rust-lang/rfcs/blob/master/text/1574-more-api-documentation-conventions.md#appendix-a-full-conventions-text
2024-07-20Remove _tls_used hackChris Denton-13/+0
2024-07-15Remove DWORDChris Denton-4/+4
2024-07-15Remove LPVOIDChris Denton-2/+3
2024-06-28std: add safety commentsjoboet-4/+16
2024-06-25std: separate TLS key creation from TLS accessjoboet-125/+100
Currently, `std` performs an atomic load to get the OS key on every access to `StaticKey` even when the key is already known. This PR thus replaces `StaticKey` with the platform-specific `get` and `set` function and a new `LazyKey` type that acts as a `LazyLock<Key>`, allowing the reuse of the retreived key for multiple accesses.
2024-06-24std: fix wasm buildsjoboet-2/+16
2024-06-17std: rename module for clarityjoboet-2/+2
2024-06-17std: update TLS module documentationjoboet-16/+22
2024-06-17std: use the `c_int` from `core::ffi` instead of `libc`joboet-1/+1
2024-06-17std: simplify `#[cfg]`s for TLSjoboet-15/+10
2024-06-15std: refactor the TLS implementationjoboet-26/+1065
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
2024-06-04Auto merge of #125525 - joboet:tls_accessor, r=cuviperbors-172/+103
Make TLS accessors closures that return pointers The current TLS macros generate a function that returns an `Option<&'static T>`. This is both risky as we lie about lifetimes, and necessitates that those functions are `unsafe`. By returning a `*const T` instead, the accessor function do not have safety requirements any longer and can be made closures without hassle. This PR does exactly that! For native TLS, the closure approach makes it trivial to select the right accessor function at compile-time, which could result in a slight speed-up (I have the hope that the accessors are now simple enough for the MIR-inliner to kick in).
2024-06-02typo: depending from -> onRalf Jung-1/+1