Skip to content

Commit b4615b5

Browse files
authored
Rollup merge of #89324 - yoshuawuyts:hardware-parallelism, r=m-ou-se
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](#74480 (comment)). 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`
2 parents 14da7fc + 03fbc16 commit b4615b5

File tree

11 files changed

+18
-16
lines changed

11 files changed

+18
-16
lines changed

library/std/src/sys/hermit/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl Thread {
9797
}
9898
}
9999

100-
pub fn available_concurrency() -> io::Result<NonZeroUsize> {
100+
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
101101
unsupported()
102102
}
103103

library/std/src/sys/sgx/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl Thread {
137137
}
138138
}
139139

140-
pub fn available_concurrency() -> io::Result<NonZeroUsize> {
140+
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
141141
unsupported()
142142
}
143143

library/std/src/sys/unix/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl Drop for Thread {
263263
}
264264
}
265265

266-
pub fn available_concurrency() -> io::Result<NonZeroUsize> {
266+
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
267267
cfg_if::cfg_if! {
268268
if #[cfg(any(
269269
target_os = "android",

library/std/src/sys/unsupported/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Thread {
3131
}
3232
}
3333

34-
pub fn available_concurrency() -> io::Result<NonZeroUsize> {
34+
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
3535
unsupported()
3636
}
3737

library/std/src/sys/wasi/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Thread {
6464
}
6565
}
6666

67-
pub fn available_concurrency() -> io::Result<NonZeroUsize> {
67+
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
6868
unsupported()
6969
}
7070

library/std/src/sys/wasm/atomics/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl Thread {
4040
pub fn join(self) {}
4141
}
4242

43-
pub fn available_concurrency() -> io::Result<NonZeroUsize> {
43+
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
4444
unsupported()
4545
}
4646

library/std/src/sys/windows/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl Thread {
100100
}
101101
}
102102

103-
pub fn available_concurrency() -> io::Result<NonZeroUsize> {
103+
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
104104
let res = unsafe {
105105
let mut sysinfo: c::SYSTEM_INFO = crate::mem::zeroed();
106106
c::GetSystemInfo(&mut sysinfo);

library/std/src/thread/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1453,12 +1453,14 @@ fn _assert_sync_and_send() {
14531453
///
14541454
/// ```
14551455
/// # #![allow(dead_code)]
1456-
/// #![feature(available_concurrency)]
1456+
/// #![feature(available_parallelism)]
14571457
/// use std::thread;
14581458
///
1459-
/// let count = thread::available_concurrency().map(|n| n.get()).unwrap_or(1);
1459+
/// let count = thread::available_parallelism().map(|n| n.get()).unwrap_or(1);
14601460
/// ```
1461-
#[unstable(feature = "available_concurrency", issue = "74479")]
1462-
pub fn available_concurrency() -> io::Result<NonZeroUsize> {
1463-
imp::available_concurrency()
1461+
#[doc(alias = "hardware_concurrency")] // Alias for C++ `std::thread::hardware_concurrency`.
1462+
#[doc(alias = "available_concurrency")] // Alias for a name we gave this API on unstable.
1463+
#[unstable(feature = "available_parallelism", issue = "74479")]
1464+
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
1465+
imp::available_parallelism()
14641466
}

library/test/src/helpers/concurrency.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ pub fn get_concurrency() -> usize {
99
_ => panic!("RUST_TEST_THREADS is `{}`, should be a positive integer.", value),
1010
}
1111
} else {
12-
thread::available_concurrency().map(|n| n.get()).unwrap_or(1)
12+
thread::available_parallelism().map(|n| n.get()).unwrap_or(1)
1313
}
1414
}

library/test/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#![feature(libc)]
2424
#![feature(rustc_private)]
2525
#![feature(nll)]
26-
#![feature(available_concurrency)]
26+
#![feature(available_parallelism)]
2727
#![feature(bench_black_box)]
2828
#![feature(internal_output_capture)]
2929
#![feature(panic_unwind)]

src/doc/rustc/src/tests/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ The following options affect how tests are executed.
161161

162162
Sets the number of threads to use for running tests in parallel. By default,
163163
uses the amount of concurrency available on the hardware as indicated by
164-
[`available_concurrency`].
164+
[`available_parallelism`].
165165

166166
This can also be specified with the `RUST_TEST_THREADS` environment variable.
167167

@@ -265,7 +265,7 @@ Experimental support for using custom test harnesses is available on the
265265

266266
[`--test` option]: ../command-line-arguments.md#option-test
267267
[`-Z panic-abort-tests`]: https://github.com/rust-lang/rust/issues/67650
268-
[`available_concurrency`]: ../../std/thread/fn.available_concurrency.html
268+
[`available_parallelism`]: ../../std/thread/fn.available_parallelism.html
269269
[`cargo test`]: ../../cargo/commands/cargo-test.html
270270
[`libtest`]: ../../test/index.html
271271
[`main` function]: ../../reference/crates-and-source-files.html#main-functions

0 commit comments

Comments
 (0)