about summary refs log tree commit diff
path: root/library/std/src/thread
AgeCommit message (Collapse)AuthorLines
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-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
2021-12-20JoinHandle docs: add missing 'the'Ralf Jung-1/+1
2021-12-18Update stdlib to the 2021 editionLucas Kent-7/+8
2021-12-14Fix a bunch of typosFrank Steffahn-2/+2
2021-12-05Rollup merge of #91355 - alexcrichton:stabilize-thread-local-const, r=m-ou-seMatthias Krüger-8/+0
std: Stabilize the `thread_local_const_init` feature This commit is intended to follow the stabilization disposition of the FCP that has now finished in #84223. This stabilizes the ability to flag thread local initializers as `const` expressions which enables the macro to generate more efficient code for accessing it, notably removing runtime checks for initialization. More information can also be found in #84223 as well as the tests where the feature usage was removed in this PR. Closes #84223
2021-12-02Document Windows TLS drop behaviourChris Denton-0/+14
2021-11-29std: Stabilize the `thread_local_const_init` featureAlex Crichton-8/+0
This commit is intended to follow the stabilization disposition of the FCP that has now finished in #84223. This stabilizes the ability to flag thread local initializers as `const` expressions which enables the macro to generate more efficient code for accessing it, notably removing runtime checks for initialization. More information can also be found in #84223 as well as the tests where the feature usage was removed in this PR. Closes #84223
2021-11-19Expand available_parallelism docs in anticipation of cgroup quotasThe8472-3/+6
The "fixed" in "fixed steady state limits" means to exclude load-dependent resource prioritization that would calculate to 100% of capacity on an idle system and less capacity on a loaded system. Additionally I also exclude "system load" since it would be silly to try to identify other, perhaps higher priority, processes hogging some CPU cores that aren't explicitly excluded by masks/quotas/whatever.
2021-11-18Auto merge of #90774 - alexcrichton:tweak-const, r=m-ou-sebors-6/+6
std: Tweak expansion of thread-local const This commit tweaks the expansion of `thread_local!` when combined with a `const { ... }` value to help ensure that the rules which apply to `const { ... }` blocks will be the same as when they're stabilized. Previously with this invocation: thread_local!(static NAME: Type = const { init_expr }); this would generate (on supporting platforms): #[thread_local] static NAME: Type = init_expr; instead the macro now expands to: const INIT_EXPR: Type = init_expr; #[thread_local] static NAME: Type = INIT_EXPR; with the hope that because `init_expr` is defined as a `const` item then it's not accidentally allowing more behavior than if it were put into a `static`. For example on the stabilization issue [this example][ex] now gives the same error both ways. [ex]: https://github.com/rust-lang/rust/issues/84223#issuecomment-953384298
2021-11-10std: Tweak expansion of thread-local constAlex Crichton-6/+6
This commit tweaks the expansion of `thread_local!` when combined with a `const { ... }` value to help ensure that the rules which apply to `const { ... }` blocks will be the same as when they're stabilized. Previously with this invocation: thread_local!(static NAME: Type = const { init_expr }); this would generate (on supporting platforms): #[thread_local] static NAME: Type = init_expr; instead the macro now expands to: const INIT_EXPR: Type = init_expr; #[thread_local] static NAME: Type = INIT_EXPR; with the hope that because `init_expr` is defined as a `const` item then it's not accidentally allowing more behavior than if it were put into a `static`. For example on the stabilization issue [this example][ex] now gives the same error both ways. [ex]: https://github.com/rust-lang/rust/issues/84223#issuecomment-953384298
2021-11-10Review commentsAlex Crichton-1/+1
2021-11-10Use `target_family = "wasm"`Alex Crichton-17/+8
2021-11-10std: Get the standard library compiling for wasm64Alex Crichton-9/+18
This commit goes through and updates various `#[cfg]` as appropriate to get the wasm64-unknown-unknown target behaving similarly to the wasm32-unknown-unknown target. Most of this is just updating various conditions for `target_arch = "wasm32"` to also account for `target_arch = "wasm64"` where appropriate. This commit also lists `wasm64` as an allow-listed architecture to not have the `restricted_std` feature enabled, enabling experimentation with `-Z build-std` externally. The main goal of this commit is to enable playing around with `wasm64-unknown-unknown` externally via `-Z build-std` in a way that's similar to the `wasm32-unknown-unknown` target. These targets are effectively the same and only differ in their pointer size, but wasm64 is much newer and has much less ecosystem/library support so it'll still take time to get wasm64 fully-fledged.
2021-11-02Auto merge of #90439 - m-ou-se:thread-is-running, r=Mark-Simulacrumbors-1/+44
Add JoinHandle::is_running. This adds: ```rust impl<T> JoinHandle<T> { /// Checks if the the associated thread is still running its main function. /// /// This might return `false` for a brief moment after the thread's main /// function has returned, but before the thread itself has stopped running. pub fn is_running(&self) -> bool; } ``` The usual way to check if a background thread is still running is to set some atomic flag at the end of its main function. We already do that, in the form of dropping an Arc which will reduce the reference counter. So we might as well expose that information. This is useful in applications with a main loop (e.g. a game, gui, control system, ..) where you spawn some background task, and check every frame/iteration whether the background task is finished to .join() it in that frame/iteration while keeping the program responsive.
2021-11-01Add tracking issue for thread_is_running.Mara Bos-1/+1
2021-10-31Add test for JoinHandle::is_running.Mara Bos-1/+35
2021-10-31Add JoinHandle::is_running.Mara Bos-0/+9
2021-10-31Rollup merge of #90431 - jkugelman:must-use-std-o-through-z, r=joshtriplettMatthias Krüger-0/+5
Add #[must_use] to remaining std functions (O-Z) I've run out of compelling reasons to group functions together across crates so I'm just going to go module-by-module. This is half of the remaining items from the `std` crate, from O-Z. `panicking::take_hook` has a side effect: it unregisters the current panic hook, returning it. I almost ignored it, but the documentation example shows `let _ = panic::take_hook();`, so following suit I went ahead and added a `#[must_use]`. ```rust std::panicking fn take_hook() -> Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send>; ``` I added these functions that clippy did not flag: ```rust std::path::Path fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool; std::path::Path fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool; std::path::Path fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf; std::path::Path fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf; ``` Parent issue: #89692 r? `@joshtriplett`
2021-10-30Add #[must_use] to remaining std functions (O-Z)John Kugelman-0/+5
2021-10-31Rollup merge of #89789 - jkugelman:must-use-thread-builder, r=joshtriplettMatthias Krüger-0/+1
Add #[must_use] to thread::Builder I copied the wording of the [`fmt::Debug` builders](https://doc.rust-lang.org/src/core/fmt/builders.rs.html#444). Affects: ```rust std/src/thread/mod.rs:289:5 std::thread::Builder fn new() -> Builder; std/src/thread/mod.rs:318:5 std::thread::Builder fn name(mut self, name: String) -> Builder; std/src/thread/mod.rs:341:5 std::thread::Builder fn stack_size(mut self, size: usize) -> Builder; ``` Parent issue: #89692 r? `@joshtriplett`
2021-10-23Add caveat about changing parallelism and function call overheadThe8472-0/+4
2021-10-13Rollup merge of #89670 - yoshuawuyts:available-parallelism-docs, r=joshtriplettMatthias Krüger-20/+57
Improve `std::thread::available_parallelism` docs _Tracking issue: https://github.com/rust-lang/rust/issues/74479_ This PR reworks the documentation of `std::thread::available_parallelism`, as requested [here](https://github.com/rust-lang/rust/pull/89324#issuecomment-934343254). ## Changes The following changes are made: - We've removed prior mentions of "hardware threads" and instead centers the docs around "parallelism" as a resource available to a program. - We now provide examples of when `available_parallelism` may return numbers that differ from the number of CPU cores in the host machine. - We now mention that the amount of available parallelism may change over time. - We make note of which platform components we don't take into account which more advanced users may want to take note of. - The example has been updated, which should be a bit easier to use. - We've added a docs alias to `num-cpus` which provides similar functionality to `available_parallelism`, and is one of the most popular crates on crates.io. --- Thanks! r? `@BurntSushi`
2021-10-13Improve `std::thread::available_parallelism` docsYoshua Wuyts-20/+57
2021-10-12Update library/std/src/thread/mod.rsJohn Kugelman-1/+1
Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2021-10-11Add #[must_use] to thread::BuilderJohn Kugelman-0/+1
2021-10-11Add #[must_use] to as_type conversionsJohn Kugelman-0/+1
2021-10-08Fix minor std::thread documentation typoMarcelo Diop-Gonzalez-3/+3
callers of spawn_unchecked() need to make sure that the thread not outlive references in the passed closure, not the other way around.
2021-10-06Rollup merge of #89324 - yoshuawuyts:hardware-parallelism, r=m-ou-seManish Goregaokar-5/+7
Rename `std::thread::available_conccurrency` to `std::thread::available_parallelism` _Tracking issue: https://github.com/rust-lang/rust/issues/74479_ This PR renames `std::thread::available_conccurrency` to `std::thread::available_parallelism`. ## Rationale The API was initially named `std::thread::hardware_concurrency`, mirroring the [C++ API of the same name](https://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency). We eventually decided to omit any reference to the word "hardware" after [this comment](https://github.com/rust-lang/rust/pull/74480#issuecomment-662045841). And so we ended up with `available_concurrency` instead. --- For a talk I was preparing this week I was reading through ["Understanding and expressing scalable concurrency" (A. Turon, 2013)](http://aturon.github.io/academic/turon-thesis.pdf), and the following passage stood out to me (emphasis mine): > __Concurrency is a system-structuring mechanism.__ An interactive system that deals with disparate asynchronous events is naturally structured by division into concurrent threads with disparate responsibilities. Doing so creates a better fit between problem and solution, and can also decrease the average latency of the system by preventing long-running computations from obstructing quicker ones. > __Parallelism is a resource.__ A given machine provides a certain capacity for parallelism, i.e., a bound on the number of computations it can perform simultaneously. The goal is to maximize throughput by intelligently using this resource. For interactive systems, parallelism can decrease latency as well. _Chapter 2.1: Concurrency is not Parallelism. Page 30._ --- _"Concurrency is a system-structuring mechanism. Parallelism is a resource."_ — It feels like this accurately captures the way we should be thinking about these APIs. What this API returns is not "the amount of concurrency available to the program" which is a property of the program, and thus even with just a single thread is effectively unbounded. But instead it returns "the amount of _parallelism_ available to the program", which is a resource hard-constrained by the machine's capacity (and can be further restricted by e.g. operating systems). That's why I'd like to propose we rename this API from `available_concurrency` to `available_parallelism`. This still meets the criteria we previously established of not attempting to define what exactly we mean by "hardware", "threads", and other such words. Instead we only talk about "concurrency" as an abstract resource available to our program. r? `@joshtriplett`
2021-10-04Add doc aliases to `std::thread::available_parallelism`Yoshua Wuyts-0/+2
2021-09-28Rename `std::thread::available_onccurrency` to ↵Yoshua Wuyts-5/+5
`std::thread::available_parallelism`
2021-09-16Remove an allocation from rt::initbjorn3-7/+5
Previously the thread name would first be heap allocated and then re-allocated to add a nul terminator. Now it will be heap allocated only once with nul terminator added form the start.
2021-08-07removed references to parent/child from std::thread documentationGodmar Back-31/+40
- also clarifies how thread.join and detaching of threads works - the previous prose implied that there is a relationship between a spawning thread and the thread being spawned, and that "child" threads couldn't outlive their parents unless detached, which is incorrect.
2021-07-29Fix may not to appropriate might not or must notAli Malik-2/+2
2021-07-10rename variableRalf Jung-2/+2
2021-07-10avoid reentrant lock acquire when ThreadIds run outRalf Jung-0/+1
2021-07-06rewrote documentation for thread::yield_now()Godmar Back-16/+17
The old documentation suggested the use of yield_now for repeated polling instead of discouraging it; it also made the false claim that channels are implementing using yield_now. (They are not, except for a corner case).
2021-06-24Use `#[non_exhaustive]` where appropriateJacob Pratt-4/+3
Due to the std/alloc split, it is not possible to make `alloc::collections::TryReserveError::AllocError` non-exhaustive without having an unstable, doc-hidden method to construct (which negates the benefits from `#[non_exhaustive]`.
2021-06-22Auto merge of #86527 - JohnTitor:rollup-cbu78g4, r=JohnTitorbors-162/+37
Rollup of 11 pull requests Successful merges: - #85054 (Revert SGX inline asm syntax) - #85182 (Move `available_concurrency` implementation to `sys`) - #86037 (Add `io::Cursor::{remaining, remaining_slice, is_empty}`) - #86114 (Reopen #79692 (Format symbols under shared frames)) - #86297 (Allow to pass arguments to rustdoc-gui tool) - #86334 (Resolve type aliases to the type they point to in intra-doc links) - #86367 (Fix comment about rustc_inherit_overflow_checks in abs().) - #86381 (Add regression test for issue #39161) - #86387 (Remove `#[allow(unused_lifetimes)]` which is now unnecessary) - #86398 (Add regression test for issue #54685) - #86493 (Say "this enum variant takes"/"this struct takes" instead of "this function takes") Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2021-06-21Move `available_concurrency` implementation to `sys`Christiaan Dirkx-162/+37
2021-06-20Squashed implementation of the passAlex Vlasov-1/+1
2021-05-20std: Don't inline TLS accessor on MinGWAlex Crichton-13/+13
This is causing [issues] on Cargo's own CI for MinGW and given the original investigation there's no reason that MinGW should work when MSVC doesn't, this this tweaks the MSVC exception to being a Windows exception. [issues]: https://github.com/rust-lang/cargo/runs/2626676503?check_suite_focus=true#step:9:2453
2021-05-19Auto merge of #84876 - alexcrichton:inline-thread-locals-cross-crate, ↵bors-0/+24
r=Mark-Simulacrum std: Attempt again to inline thread-local-init across crates Issue #25088 has been part of `thread_local!` for quite some time now. Historical attempts have been made to add `#[inline]` to `__getit` in #43931, #50252, and #59720, but these attempts ended up not landing at the time due to segfaults on Windows. In the interim though with `const`-initialized thread locals AFAIK this is the only remaining bug which is why you might want to use `#[thread_local]` over `thread_local!`. As a result I figured it was time to resubmit this and see how it fares on CI and if I can help debugging any issues that crop up. Closes #25088
2021-05-18std: Attempt again to inline thread-local-init across cratesAlex Crichton-0/+24
Issue #25088 has been part of `thread_local!` for quite some time now. Historical attempts have been made to add `#[inline]` to `__getit` in #43931, #50252, and #59720, but these attempts ended up not landing at the time due to segfaults on Windows. In the interim though with `const`-initialized thread locals AFAIK this is the only remaining bug which is why you might want to use `#[thread_local]` over `thread_local!`. As a result I figured it was time to resubmit this and see how it fares on CI and if I can help debugging any issues that crop up. Closes #25088
2021-05-07Rollup merge of #84409 - mzohreva:mz/tls-dtors-before-join, r=jethrogbDylan DPC-0/+108
Ensure TLS destructors run before thread joins in SGX The excellent test is from ```@jethrogb``` For context see: https://github.com/rust-lang/rust/pull/83416#discussion_r617282907