Commit Graph

372 Commits

Author SHA1 Message Date
Trevor Gross 289fe36d37 Print thread ID in panic message if thread name is unknown
`panic!` does not print any identifying information for threads that are
unnamed. However, in many cases, the thread ID can be determined.

This changes the panic message from something like this:

    thread '<unnamed>' panicked at src/main.rs:3:5:
    explicit panic

To something like this:

    thread '<unnamed>' (0xff9bf) panicked at src/main.rs:3:5:
    explicit panic

Stack overflow messages are updated as well.

This change applies to both named and unnamed threads. The ID printed is
the OS integer thread ID rather than the Rust thread ID, which should
also be what debuggers print.
2025-08-06 23:59:47 +00:00
Stuart Cook e65201ccb8 Rollup merge of #144188 - joshtriplett:available-parallelism, r=Mark-Simulacrum
`available_parallelism`: Add documentation for why we don't look at `ulimit`
2025-08-04 11:24:36 +10:00
Stuart Cook 4bfbd80bab Rollup merge of #144500 - joboet:thread-name-stack-overflow, r=ChrisDenton
thread name in stack overflow message

Fixes rust-lang/rust#144481, which is caused by the thread name not being initialised yet when setting up the stack overflow information. Unfortunately, the stack overflow UI test did not test for the correct thread name being present, and testing this separately didn't occur to me when writing https://github.com/rust-lang/rust/pull/140628.

This PR contains the smallest possible fix I could think of: passing the thread name explicitly to the platform thread creation function. In the future I'd very much like to explore some possibilities around merging the thread packet and thread handle into one structure and using that in the platform code instead – but that's best left for another PR.

This PR also amends the stack overflow test to check for thread names, so we don't run into this again.

``@rustbot`` label +beta-nominated
2025-07-29 20:19:51 +10:00
joboet 73751a0491 thread name in stack overflow message 2025-07-28 18:09:35 +02:00
Trevor Gross 2fcea9fb68 Rollup merge of #143859 - orlp:thread-into-raw-align, r=jhpratt
Guarantee 8 bytes of alignment in Thread::into_raw

When using `AtomicPtr` for synchronization it's incredibly useful when you've got a couple bits you can stuff metadata in. By guaranteeing that `Thread`'s `Inner` struct is aligned to 8 bytes everyone can use the bottom 3 bits to signal other things, such as a critical section, etc.

This guarantee is thus very useful and costs us nothing.
2025-07-26 02:19:29 -05:00
Josh Triplett 41199f39d8 available_parallelism: Add documentation for why we don't look at ulimit 2025-07-19 11:17:38 -07:00
Cameron Steffen 1120cb2fe5 Add LocalKey<Cell>::update 2025-07-15 16:17:57 -05:00
Orson Peters 6f4d0bdde8 Tidy 2025-07-13 01:20:29 +02:00
Orson Peters 69b9bae57d Guarantee 8 bytes of alignment in Thread::into_raw 2025-07-13 00:58:46 +02:00
bors ca98d4d4b3 Auto merge of #141829 - dvdsk:sleep_until_linux, r=cuviper,RalfJung
Specialize sleep_until implementation for unix (except mac)

related tracking issue: https://github.com/rust-lang/rust/issues/113752
Supersedes https://github.com/rust-lang/rust/pull/118480 for the reasons see: https://github.com/rust-lang/rust/issues/113752#issuecomment-2902594469

Replaces the generic catch all implementation with target_os specific ones for: linux/netbsd/freebsd/android/solaris/illumos etc. Other platforms like wasi, macos/ios/tvos/watchos and windows will follow in later separate PR's (once this is merged).
2025-07-06 23:00:51 +00:00
dvdsk f24ee2c9b1 sleep_until: use clock_nanosleep where possible
Using clock nanosleep leads to more accurate sleep times on platforms
where it is supported.

To enable using clock_nanosleep this makes `sleep_until` platform
specific. That unfortunatly requires identical placeholder
implementations for the other platforms (windows/mac/wasm etc).

we will land platform specific implementations for those later. See the
`sleep_until` tracking issue.

This requires an accessors for the Instant type. As that accessor is only
used on the platforms that have clock_nanosleep it is marked as allow_unused.

32bit time_t targets do not use clock_nanosleep atm, they instead rely
on the same placeholder as the other platforms. We could make them
use clock_nanosleep too in the future using `__clock_nanosleep_time64`.

__clock_nanosleep_time64 is documented at:
https://www.gnu.org/software/libc/manual/html_node/64_002dbit-time-symbol-handling.html
2025-07-06 17:36:49 +02:00
klensy c76d032f01 setup CI and tidy to use typos for spellchecking and fix few typos 2025-07-03 10:51:06 +03:00
Pietro Albini 2ce08ca5d6 update cfg(bootstrap) 2025-05-12 15:33:37 +02:00
Christopher Durham 4d93f60568 use generic Atomic type where possible
in core/alloc/std only for now, and ignoring test files

Co-authored-by: Pavel Grigorenko <GrigorenkoPV@ya.ru>
2025-04-27 02:18:08 +03:00
Lieselotte 0d56e3eed1 LocalKey<T>: document that the dtor should not panic 2025-04-18 14:11:32 +02:00
Michael Howell e013cf8afc rustdoc-search: add unbox flag to Result aliases
Fixes #139665
2025-04-11 11:36:40 -07:00
bors 7586a9f99a Auto merge of #138702 - m-ou-se:spawn-in-atexit, r=Mark-Simulacrum
Allow spawning threads after TLS destruction

Fixes #138696
2025-03-27 21:46:58 +00:00
Mara Bos f23e76e0d2 Allow spawning threads after TLS destruction. 2025-03-19 12:49:18 +01:00
Paul Menage 9e5c942286 Mark some std tests as requiring panic = "unwind"
This allows these test modules to pass on builds/targets without
unwinding support, where `panic = "abort"` - the ignored tests are for
functionality that's not supported on those targets.
2025-03-10 08:31:06 -07:00
Thalia Archibald 988eb19970 library: Use size_of from the prelude instead of imported
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-03-06 20:20:38 -08:00
Matthias Krüger f19d4b5f97 Rollup merge of #137480 - fuzzypixelz:fix/124466, r=workingjubilee
Return unexpected termination error instead of panicing in `Thread::join`

There is a time window during which the OS can terminate a thread before stdlib can retreive its `Packet`. Currently the `Thread::join` panics with no message in such an event, which makes debugging difficult; fixes #124466.
2025-02-27 08:56:39 +01:00
Mahmoud Mazouz 7058f62d0a Use .expect(..) instead 2025-02-25 13:11:03 +01:00
Mahmoud Mazouz 1ccdc06136 Remove speculation on cause of error
Co-authored-by: Jubilee <workingjubilee@gmail.com>
2025-02-24 09:50:46 +01:00
Mahmoud Mazouz db1f0d0458 Return error on unexpected termination in Thread::join.
There is a time window during which the OS can terminate a thread before stdlib
can retreive its `Packet`. Currently the `Thread::join` panics with no message
in such an event, which makes debugging difficult; fixes #124466.
2025-02-23 12:26:16 +01:00
Kornel b94162078d Highlight thread_local! const init in docs 2025-02-21 13:02:42 +00:00
bjorn3 332fb7e6f1 Move std::thread_local unit tests to integration tests 2025-01-26 10:28:05 +00:00
bors b605c65b6e Auto merge of #135224 - wyfo:tls-panic-outline, r=cuviper
Outline panicking code for `LocalKey::with`

See https://github.com/rust-lang/rust/pull/115491 for prior related modifications.

https://godbolt.org/z/MTsz87jGj shows a reduction of the code size for TLS accesses.
2025-01-21 02:23:15 +00:00
joboet 2c28cc45c8 add comments explaining main thread identification 2025-01-14 13:37:28 +01:00
joboet 14f7f4b7bf std: lazily allocate the main thread handle
Thereby, we also allow accessing thread::current before main: as the runtime no longer tries to install its own handle, this will no longer trigger an abort. Rather, the name returned from name will only be "main" after the runtime initialization code has run, but I think that is acceptable.

This new approach also requires some changes to the signal handling code, as calling `thread::current` would now allocate when called on the main thread, which is not acceptable. I fixed this by adding a new function (`with_current_name`) that performs all the naming logic without allocation or without initializing the thread ID (which could allocate on some platforms).
2025-01-14 13:37:28 +01:00
joboet 0e5ee891b2 Revert "Remove the Arc rt::init allocation for thread info"
This reverts commit 0747f2898e.
2025-01-14 13:37:25 +01:00
Esteban Küber eb917ea24d Remove some unnecessary .into() calls 2025-01-08 21:19:28 +00:00
Joseph Perez 8ec7bae57b Outline panicking code for LocalKey::with
See https://github.com/rust-lang/rust/pull/115491 for prior related
modifications.

https://godbolt.org/z/MTsz87jGj shows a reduction of the code size
for TLS accesses.
2025-01-08 00:29:20 +01:00
calciumbe 4f8bebd6b5 fix: typos
Signed-off-by: calciumbe <192480234+calciumbe@users.noreply.github.com>
2024-12-29 21:35:02 +08:00
Ralf Jung 8b2b6359f9 mri: add track_caller to thread spawning methods for better backtraces 2024-12-20 15:03:51 +01:00
Will-Low 5d8233edbf Define acronym for thread local storage
There are multiple references in this module's documentation to the acronym "TLS", without defining it. This is confusing for the reader.

I propose that this acronym be defined during the first use of the term.
2024-12-06 15:33:05 -08:00
Matthias Krüger 58fac8fe72 Rollup merge of #132937 - xmh0511:master, r=m-ou-se
a release operation synchronizes with an acquire operation

Change:
1. `Calls to park _synchronize-with_ calls to unpark` to `Calls to unpark _synchronize-with_ calls to park`
2. `park synchronizes-with _all_ prior unpark operations` to `_all_ prior unpark operations synchronize-with park`
2024-12-03 21:55:26 +01:00
bors 1fc691e6dd Auto merge of #133533 - BoxyUwU:bump-boostrap, r=jieyouxu,Mark-Simulacrum
Bump boostrap compiler to new beta

Currently failing due to something about the const stability checks and `panic!`. I'm not sure why though since I wasn't able to see any PRs merged in the past few days that would result in a `cfg(bootstrap)` that shouldn't be removed. cc `@RalfJung` #131349
2024-11-29 22:39:10 +00:00
Matthias Krüger a8b690fada Rollup merge of #133498 - GuillaumeGomez:missing-examples, r=joboet
Add missing code examples on `LocalKey`

r? ``@Amanieu``
2024-11-27 22:23:25 +01:00
Boxy 22998f0785 update cfgs 2024-11-27 15:14:54 +00:00
Guillaume Gomez 02c3e6d087 Add missing code examples on LocalKey 2024-11-26 15:29:47 +01:00
Ralf Jung 8bc8adb8dc std:🧵 avoid leading whitespace in some panic messages 2024-11-25 18:31:49 +01:00
许杰友 Jieyou Xu (Joe) 31b4023e24 Rollup merge of #132730 - joboet:after_main_sync, r=Noratrieb
std: allow after-main use of synchronization primitives

By creating an unnamed thread handle when the actual one has already been destroyed, synchronization primitives using thread parking can be used even outside the Rust runtime.

This also fixes an inefficiency in the queue-based `RwLock`: if `thread::current` was not initialized yet, it will create a new handle on every parking attempt without initializing `thread::current`. The private `current_or_unnamed` function introduced here fixes this.
2024-11-25 00:39:03 +08:00
Colin Finck d0384f3ba5 Fix typo in std::thread::Scope::spawn documentation. 2024-11-22 11:51:38 +01:00
Mara Bos 691796be03 Update doc comments for spawn hook. 2024-11-19 18:55:52 +01:00
Mara Bos b96f023dbd Address review comments.
Co-authored-by: waffle <waffle.lapkin@gmail.com>
2024-11-19 18:55:52 +01:00
Mara Bos 38b9a448c9 Fix tracking issue. 2024-11-19 18:54:21 +01:00
Mara Bos 24fec0d896 Add tracking issue. 2024-11-19 18:54:21 +01:00
Mara Bos 5a80b48fe1 Use Send + Sync for spawn hooks. 2024-11-19 18:54:20 +01:00
Mara Bos f2bf9e198e Add thread Builder::no_hooks(). 2024-11-19 18:54:20 +01:00
Mara Bos 947354fbec Update thread spawn hooks.
1. Make the effect thread local.
2. Don't return a io::Result from hooks.
2024-11-19 18:54:20 +01:00