about summary refs log tree commit diff
path: root/library/std/src/thread/mod.rs
AgeCommit message (Collapse)AuthorLines
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-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
2022-01-13Auto merge of #92553 - m-ou-se:thread-join-simplify, r=Mark-Simulacrumbors-9/+9
Simpilfy thread::JoinInner. `JoinInner`'s `native` field was an `Option`, but that's unnecessary. Also, thanks to `Arc::get_mut`, there's no unsafety needed in `JoinInner::join()`.
2022-01-07Stabilize `#[feature(available_parallelism)]`Yoshua Wuyts-2/+1
2022-01-05Rename n_running_threads to num_running_threads.Mara Bos-2/+2
2022-01-04Fix typo in is_running() docs.Mara Bos-1/+1
Co-authored-by: Mattias Buelens <649348+MattiasBuelens@users.noreply.github.com>
2022-01-04Simplify panicking mechanism of thread::scope.Mara Bos-8/+5
It now panic!()s on its own, rather than resume_unwind'ing the panic payload from the thread. Using resume_unwind skips the panic_handler, meaning that the main thread would never have a panic handler run, which can get confusing.
2022-01-04Implement RFC 3151: Scoped threads.Mara Bos-26/+70
2022-01-04Simpilfy thread::JoinInner.Mara Bos-9/+9
2022-01-01Rollup merge of #84083 - ltratt:threadid_doc_tweak, r=dtolnayMatthias Krüger-4/+7
Clarify the guarantees that ThreadId does and doesn't make. The existing documentation does not spell out whether `ThreadId`s are unique during the lifetime of a thread or of a process. I had to examine the source code to realise (pleasingly!) that they're unique for the lifetime of a process. That seems worth documenting clearly, as it's a strong guarantee. Examining the way `ThreadId`s are created also made me realise that the `as_u64` method on `ThreadId` could be a trap for the unwary on those platforms where the platform's notion of a thread identifier is also a 64 bit integer (particularly if they happen to use a similar identifier scheme to `ThreadId`). I therefore think it's worth being even clearer that there's no relationship between the two.
2021-12-25Language tweak.Laurence Tratt-2/+2