about summary refs log tree commit diff
path: root/library/std/src/thread
AgeCommit message (Collapse)AuthorLines
2022-03-19Stabilize thread::is_finishedJubilee Young-2/+2
2022-03-10Rollup merge of #93950 - T-O-R-U-S:use-modern-formatting-for-format!-macros, ↵Dylan DPC-4/+4
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-10Rollup merge of #94644 - m-ou-se:scoped-threads-drop-soundness, r=joshtriplettMatthias Krüger-7/+47
Fix soundness issue in scoped threads. This was discovered in https://github.com/rust-lang/rust/pull/94559#discussion_r820116323 The `scope()` function returns when all threads are finished, but I accidentally considered a thread 'finished' before dropping their panic payload or ignored return value. So if a thread returned (or panics with) something that in its `Drop` implementation still uses borrowed stuff, it goes wrong. https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=2a1f19ac4676cdabe43e24e536ff9358
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-4/+4
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-09Add documentation about lifetimes to thread::scope.Mara Bos-0/+18
2022-03-09Add soundness test for dropping scoped thread results before joining.Mara Bos-1/+24
2022-03-09Remove outdated comment.Mara Bos-2/+0
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-08Rollup merge of #94559 - m-ou-se:thread-scope-spawn-closure-without-arg, ↵Matthias Krüger-21/+23
r=Mark-Simulacrum Remove argument from closure in thread::Scope::spawn. This implements ```@danielhenrymantilla's``` [suggestion](https://github.com/rust-lang/rust/issues/93203#issuecomment-1040798286) for improving the scoped threads interface. Summary: The `Scope` type gets an extra lifetime argument, which represents basically its own lifetime that will be used in `&'scope Scope<'scope, 'env>`: ```diff - pub struct Scope<'env> { .. }; + pub struct Scope<'scope, 'env: 'scope> { .. } pub fn scope<'env, F, T>(f: F) -> T where - F: FnOnce(&Scope<'env>) -> T; + F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> T; ``` This simplifies the `spawn` function, which now no longer passes an argument to the closure you give it, and now uses the `'scope` lifetime for everything: ```diff - pub fn spawn<'scope, F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T> + pub fn spawn<F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T> where - F: FnOnce(&Scope<'env>) -> T + Send + 'env, + F: FnOnce() -> T + Send + 'scope, - T: Send + 'env; + T: Send + 'scope; ``` The only difference the user will notice, is that their closure now takes no arguments anymore, even when spawning threads from spawned threads: ```diff thread::scope(|s| { - s.spawn(|_| { + s.spawn(|| { ... }); - s.spawn(|s| { + s.spawn(|| { ... - s.spawn(|_| ...); + s.spawn(|| ...); }); }); ``` <details><summary>And, as a bonus, errors get <em>slightly</em> better because now any lifetime issues point to the outermost <code>s</code> (since there is only one <code>s</code>), rather than the innermost <code>s</code>, making it clear that the lifetime lasts for the entire <code>thread::scope</code>. </summary> ```diff error[E0373]: closure may outlive the current function, but it borrows `a`, which is owned by the current function --> src/main.rs:9:21 | - 7 | s.spawn(|s| { - | - has type `&Scope<'1>` + 6 | thread::scope(|s| { + | - lifetime `'1` appears in the type of `s` 9 | s.spawn(|| println!("{:?}", a)); // might run after `a` is dropped | ^^ - `a` is borrowed here | | | may outlive borrowed value `a` | note: function requires argument type to outlive `'1` --> src/main.rs:9:13 | 9 | s.spawn(|| println!("{:?}", a)); // might run after `a` is dropped | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: to force the closure to take ownership of `a` (and any other referenced variables), use the `move` keyword | 9 | s.spawn(move || println!("{:?}", a)); // might run after `a` is dropped | ++++ " ``` </details> The downside is that the signature of `scope` and `Scope` gets slightly more complex, but in most cases the user wouldn't need to write those, as they just use the argument provided by `thread::scope` without having to name its type. Another downside is that this does not work nicely in Rust 2015 and Rust 2018, since in those editions, `s` would be captured by reference and not by copy. In those editions, the user would need to use `move ||` to capture `s` by copy. (Which is what the compiler suggests in the error.)
2022-03-07Use `f` instead of `|| f()`.Mara Bos-1/+1
Co-authored-by: Mark Rousskov <mark.simulacrum@gmail.com>
2022-03-05Small fixes in thread local code.Mara Bos-2/+2
2022-03-05Update documentation in thread/local.rs.Mara Bos-6/+26
2022-03-05Add debug asserts in thread local cell set methods.Mara Bos-0/+4
2022-03-05Add tracking issue number for local_key_cell_methods.Mara Bos-9/+9
2022-03-05Rename LocalKey's with_{ref,mut} to with_borrow{,_mut}.Mara Bos-9/+9
2022-03-05Implement RFC 3184 - thread local cell methods.Mara Bos-10/+347
2022-03-04Rollup merge of #94549 - m-ou-se:thread-is-finished, r=yaahcMatthias Krüger-14/+22
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-04Use '_ for irrelevant lifetimes in Debug impl.Mara Bos-1/+1
Co-authored-by: Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>
2022-03-03Remove argument from closure in thread::Scope::spawn.Mara Bos-21/+23
2022-03-03Remove unnecessary #![feature]s from doctest.Mara Bos-2/+0
2022-03-03Update test.Mara Bos-4/+4
2022-03-03Rename JoinHandle::is_running to is_finished and update docs.Mara Bos-8/+18
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-25Make TLS __getit #[inline(always)] on non-WindowsErik Desjardins-2/+2
This may improve perf.
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/+382
Implement RFC 3151: Scoped threads. This implements https://github.com/rust-lang/rfcs/pull/3151 r? `@Amanieu`
2022-01-22Add test for thread::Scope invariance.Mara Bos-0/+13
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-16Rollup merge of #92619 - Alexendoo:macro-diagnostic-items, r=matthewjasperMatthias Krüger-0/+1
Add diagnostic items for macros For use in Clippy, it adds diagnostic items to all the stable public macros Clippy has lints that look for almost all of these (currently by name or path), but there are a few that aren't currently part of any lint, I could remove those if it's preferred to add them as needed rather than ahead of time
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-06Add diagnostic items for macrosAlex Macleod-0/+1
2022-01-05Mention *scoped* thread in panic message.Mara Bos-1/+1
Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
2022-01-05Note the invariance over 'env in Scope<'env>.Mara Bos-0/+2
2022-01-05Fix missing .load() in Scope's Debug impl.Mara Bos-1/+1
2022-01-05Rename n_running_threads to num_running_threads.Mara Bos-11/+11
2022-01-04Fix typo in Scope::spawn docs.Mara Bos-1/+1
Co-authored-by: deltragon <m@dafert.at>
2022-01-04Fix typo in is_running() docs.Mara Bos-2/+2
Co-authored-by: Mattias Buelens <649348+MattiasBuelens@users.noreply.github.com>
2022-01-04Fix typo in documentation.Mara Bos-1/+1
2022-01-04Use > rather than == for overflow check in scoped threads.Mara Bos-1/+1
Co-authored-by: Jacob Lifshay <programmerjake@gmail.com>
2022-01-04Fix variance of thread::Scope.Mara Bos-1/+1
2022-01-04Formatting.Mara Bos-1/+3
2022-01-04Simplify panicking mechanism of thread::scope.Mara Bos-31/+22
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-04Add documentation for scoped threads.Mara Bos-11/+175
2022-01-04Add ScopedJoinHandle::is_running().Mara Bos-1/+10
2022-01-04Implement RFC 3151: Scoped threads.Mara Bos-26/+202
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.