summary refs log tree commit diff
path: root/library/std/src/thread/mod.rs
AgeCommit message (Collapse)AuthorLines
2023-04-28avoid duplicating TLS state between test std and realstdRalf Jung-16/+16
2023-04-26Auto merge of #110861 - m-ou-se:thread-local-restructure, r=workingjubileebors-2/+5
Restructure and rename std thread_local internals to make it less of a maze Every time I try to work on std's thread local internals, it feels like I'm trying to navigate a confusing maze made of macros, deeply nested modules, and types with multiple names/aliases. Time to clean it up a bit. This PR: - Exports `Key` with its own name (`Key`), instead of `__LocalKeyInner` - Uses `pub macro` to put `__thread_local_inner` into a (unstable, hidden) module, removing `#[macro_export]`, removing it from the crate root. - Removes the `__` from `__thread_local_inner`. - Removes a few unnecessary `allow_internal_unstable` features from the macros - Removes the `libstd_thread_internals` feature. (Merged with `thread_local_internals`.) - And removes it from the unstable book - Gets rid of the deeply nested modules for the `Key` definitions (`mod fast` / `mod os` / `mod statik`). - Turns a `#[cfg]` mess into a single `cfg_if`, now that there's no `#[macro_export]` anymore that breaks with `cfg_if`. - Simplifies the `cfg_if` conditions to not repeat the conditions. - Removes useless `normalize-stderr-test`, which were left over from when the `Key` types had different names on different platforms. - Removes a seemingly unnecessary `realstd` re-export on `cfg(test)`. This PR changes nothing about the thread local implementation. That's for a later PR. (Which should hopefully be easier once all this stuff is a bit cleaned up.)
2023-04-26Restructure and rename thread local things in std.Mara Bos-2/+5
2023-04-26Spelling library/Josh Soref-2/+2
* advance * aligned * borrowed * calculate * debugable * debuggable * declarations * desugaring * documentation * enclave * ignorable * initialized * iterator * kaboom * monomorphization * nonexistent * optimizer * panicking * process * reentrant * rustonomicon * the * uninitialized Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2023-03-17Update mod.rsSean Linsley-2/+2
2023-03-16Clarify that RUST_MIN_STACK is internally cachedSean Linsley-1/+2
For larger applications it's important that users set `RUST_MIN_STACK` at the start of their program because `min_stack` caches the value. Not doing so can lead to their `env::set_var` call surprisingly not having any effect.
2023-03-10Moved thread_local implementation to sys::commonAyush Singh-36/+1
This allows removing all the platform-dependent code from `library/std/src/thread/local.rs` and `library/std/src/thread/mod.rs` Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
2023-02-19Rollup merge of #104659 - tshepang:reflow, r=workingjubileeDylan DPC-2/+4
reflow the stack size story
2023-01-09std tests: use __OsLocalKeyInner from realstdRalf Jung-7/+15
2022-12-30std: rename `Parker::new` to `Parker::new_in_place`, add safe `Parker::new` ↵joboet-1/+1
constructor for SGX
2022-12-29std: unify id-based thread parking implementationsjoboet-1/+1
2022-11-21reflow the stack size storyTshepang Mbambo-2/+4
2022-11-20Improve documentation of Stack sizeFabian Hintringer-3/+2
2022-10-15Auto merge of #100579 - joboet:sync_mutex_everywhere, r=thomccbors-15/+12
std: use `sync::Mutex` for internal statics Since `sync::Mutex` is now `const`-constructible, it can be used for internal statics, removing the need for `sys_common::StaticMutex`. This adds some extra allocations on platforms which need to box their mutexes (currently SGX and some UNIX), but these will become unnecessary with the lock improvements tracked in #93740. I changed the program argument implementation on Hermit, it does not need `Mutex` but can use atomics like some UNIX systems (ping `@mkroening` `@stlankes).`
2022-10-14Auto merge of #102783 - RalfJung:tls, r=thomccbors-10/+20
sync thread_local key conditions exactly with what the macro uses This makes the `cfg` in `mod.rs` syntactically the same as those in `local.rs`. I don't think this should actually change anything, but seems better to be consistent? I looked into this due to https://github.com/rust-lang/rust/issues/102549, but this PR would make it *less* likely that `__OsLocalKeyInner` is going to get provided, so this cannot help with that issue. r? `@thomcc`
2022-10-13smarter way to avoid 'unused' warning when building for testsRalf Jung-9/+2
2022-10-13sync thread_local key conditions exactly with what the macro usesRalf Jung-8/+25
2022-10-13std: use `sync::Mutex` for internal staticsjoboet-15/+12
2022-10-11Rollup merge of #102589 - RalfJung:scoped-threads-dangling, r=m-ou-seYuki Okushi-0/+33
scoped threads: pass closure through MaybeUninit to avoid invalid dangling references The `main` function defined here looks roughly like this, if it were written as a more explicit stand-alone function: ```rust // Not showing all the `'lifetime` tracking, the point is that // this closure might live shorter than `thread`. fn thread(control: ..., closure: impl FnOnce() + 'lifetime) { closure(); control.signal_done(); // A lot of time can pass here. } ``` Note that `thread` continues to run even after `signal_done`! Now consider what happens if the `closure` captures a reference of lifetime `'lifetime`: - The type of `closure` is a struct (the implicit unnameable closure type) with a `&'lifetime mut T` field. References passed to a function are marked with `dereferenceable`, which is LLVM speak for *this reference will remain live for the entire duration of this function*. - The closure runs, `signal_done` runs. Then -- potentially -- this thread gets scheduled away and the main thread runs, seeing the signal and returning to the user. Now `'lifetime` ends and the memory the reference points to might be deallocated. - Now we have UB! The reference that as passed to `thread` with the promise of remaining live for the entire duration of the function, actually got deallocated while the function still runs. Oops. Long-term I think we should be able to use `ManuallyDrop` to fix this without `unsafe`, or maybe a new `MaybeDangling` type. I am working on an RFC for that. But in the mean time it'd be nice to fix this so that Miri with `-Zmiri-retag-fields` (which is needed for "full enforcement" of all the LLVM flags we generate) stops erroring on scoped threads. Fixes https://github.com/rust-lang/rust/issues/101983 r? `@m-ou-se`
2022-10-11Rollup merge of #102412 - joboet:dont_panic, r=m-ou-seYuki Okushi-2/+20
Never panic in `thread::park` and `thread::park_timeout` fixes #102398 `@rustbot` label +T-libs +T-libs-api
2022-10-03scoped threads: pass closure through MaybeUninit to avoid invalid dangling ↵Ralf Jung-0/+33
references
2022-09-28std: never panic in `thread::park` and `thread::park_timeout`joboet-2/+20
2022-09-26Update docs so that deprecated method points to relevant methodAnirudh-0/+2
2022-09-09doc: fix minor typoAkhilesh Singhania-1/+1
2022-08-22update and extend some comments, and cfg-out some unused codeRalf Jung-7/+7
2022-08-22std: use realstd fast key when building testsRalf Jung-0/+10
2022-08-10std: optimize thread ID generationjoboet-18/+41
2022-07-23Auto merge of #97925 - the8472:cgroupv1, r=joshtriplettbors-1/+6
Add cgroupv1 support to available_parallelism Fixes #97549 My dev machine uses cgroup v2 so I was only able to test that code path. So the v1 code path is written only based on documentation. I could use some help testing that it works on a machine with cgroups v1: ``` $ x.py build --stage 1 # quota.rs fn main() { println!("{:?}", std::thread::available_parallelism()); } # assuming stage1 is linked in rustup $ rust +stage1 quota.rs # spawn a new cgroup scope for the current user $ sudo systemd-run -p CPUQuota="300%" --uid=$(id -u) -tdS # should print Ok(3) $ ./quota ``` If it doesn't work as expected an strace, the contents of `/proc/self/cgroups` and the structure of `/sys/fs/cgroups` would help.
2022-07-22[review] mention that runtime may scale with # of mountpointsthe8472-0/+5
Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2022-07-15Fix typo in mod.rsIkko Ashimine-1/+1
constuct -> construct
2022-06-27fix data race in thread::scopeRalf Jung-6/+11
2022-06-20Improve docs for `is_running` to explain use caseJosh Triplett-3/+4
2022-06-11Stabilize scoped threads.Mara Bos-2/+2
2022-06-09add cgroupv1 support to available_parallelismThe 8472-1/+1
2022-05-09Auto merge of #95960 - jhpratt:remove-rustc_deprecated, r=compiler-errorsbors-2/+2
Remove `#[rustc_deprecated]` This removes `#[rustc_deprecated]` and introduces diagnostics to help users to the right direction (that being `#[deprecated]`). All uses of `#[rustc_deprecated]` have been converted. CI is expected to fail initially; this requires #95958, which includes converting `stdarch`. I plan on following up in a short while (maybe a bootstrap cycle?) removing the diagnostics, as they're only intended to be short-term.
2022-05-01Fix some links in the standard libraryVadim Petrochenkov-0/+1
2022-04-25std: directly use pthread in UNIX parker implementationjoboet-5/+27
Mutex and Condvar are being replaced by more efficient implementations, which need thread parking themselves (see #93740). Therefore use the pthread synchronization primitives directly. Also, avoid allocating because the Parker struct is being placed in an Arc anyways.
2022-04-14Remove use of `#[rustc_deprecated]`Jacob Pratt-2/+2
2022-03-19Stabilize thread::is_finishedJubilee Young-1/+1
2022-03-10Rollup merge of #93950 - T-O-R-U-S:use-modern-formatting-for-format!-macros, ↵Dylan DPC-3/+3
r=Mark-Simulacrum Use modern formatting for format! macros This updates the standard library's documentation to use the new format_args syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored). `eprintln!("{}", e)` becomes `eprintln!("{e}")`, but `eprintln!("{}", e.kind())` remains untouched.
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-3/+3
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-03-09Properly abort when thread result panics on drop.Mara Bos-19/+21
2022-03-09Fix soundness issue in scoped threads.Mara Bos-0/+17
2022-03-04Rollup merge of #94549 - m-ou-se:thread-is-finished, r=yaahcMatthias Krüger-4/+9
Rename JoinHandle::is_running to is_finished. This is renaming `is_running` to `is_finished` as discussed on the tracking issue here: https://github.com/rust-lang/rust/issues/90470#issuecomment-1050188499 Taking some of the docs suggestions from https://github.com/rust-lang/rust/pull/94033
2022-03-03Rename JoinHandle::is_running to is_finished and update docs.Mara Bos-4/+9
Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2022-03-03update available_parallelism docs since cgroups and sched_getaffinity are ↵The 8472-1/+4
now taken into account
2022-02-22Fix typo.NyantasticUwU-1/+1
Yeah just a typo (probably some breaking changes in here be careful) :)
2022-01-23Rollup merge of #92555 - m-ou-se:scoped-threads, r=AmanieuMatthias Krüger-27/+66
Implement RFC 3151: Scoped threads. This implements https://github.com/rust-lang/rfcs/pull/3151 r? `@Amanieu`
2022-01-22Add tracking issue number for scoped_threads.Mara Bos-2/+2
2022-01-22Simplify Send/Sync of std::thread::Packet.Mara Bos-7/+5