rust/library/std/src
bors c38ddb8040 Auto merge of #74480 - yoshuawuyts:hardware_threads, r=dtolnay
Add std:🧵:available_concurrency

This PR adds a counterpart to [C++'s `std:🧵:hardware_concurrency`](https://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency) to Rust, tracking issue https://github.com/rust-lang/rust/issues/74479.

cc/ `@rust-lang/libs`

## Motivation

Being able to know how many hardware threads a platform supports is a core part of building multi-threaded code. In C++ 11 this has become available through the [`std:🧵:hardware_concurrency`](https://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency) API. Currently in Rust most of the ecosystem depends on the [`num_cpus` crate](https://docs.rs/num_cpus/1.13.0/num_cpus/) ([no.35 in top 500 crates](https://docs.google.com/spreadsheets/d/1wwahRMHG3buvnfHjmPQFU4Kyfq15oTwbfsuZpwHUKc4/edit#gid=1253069234)) to provide this functionality. This PR proposes an API to provide access to the number of hardware threads available on a given platform.

__edit (2020-07-24):__ The purpose of this PR is to provide a hint for how many threads to spawn to saturate the processor. There's value in introducing APIs for NUMA and Windows processor groups, but those are intentionally out of scope for this PR. See: https://github.com/rust-lang/rust/pull/74480#issuecomment-662116186.

## Naming

Discussing the naming of the API on Zulip surfaced two options:

- `std:🧵:hardware_concurrency`
- `std:🧵:hardware_threads`

Both options seemed acceptable, but overall people seem to gravitate the most towards `hardware_threads`. Additionally `@jonas-schievink` pointed out that the "hardware threads" terminology is well-established and is used in among other the [RISC-V specification](https://riscv.org/specifications/isa-spec-pdf/) (page 20):

> A component is termed a core if it contains an independent instruction fetch unit. A RISC-V-compatible core might support multiple RISC-V-compatible __hardware threads__, or harts, through multithreading.

It's also worth noting that [the original paper introducing C++'s `std::thread` submodule](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2320.html) unfortunately doesn't feature any discussion on the naming of `hardware_concurrency`, so we can't use that to help inform our decision here.

## Return type

An important consideration `@joshtriplett` brought up is that we don't want to default to `1` for platforms where the number of available threads cannot be retrieved. Instead we want to inform the users of the fact that we don't know and allow them to handle that case. Which is why this PR uses `Option<NonZeroUsize>` as its return type, where `None` is returned on platforms where we don't know the number of hardware threads available.

The reasoning for `NonZeroUsize` vs `usize` is that if the number of threads for a platform are known, they'll always be at least 1. As evidenced by the example the `NonZero*` family of APIs may currently not be the most ergonomic to use, but improving the ergonomics of them is something that I think we can address separately.

## Implementation

`@Mark-Simulacrum` pointed out that most of the code we wanted to expose here was already available under `libtest`. So this PR mostly moves the internal code of libtest into a public API.
2020-10-18 02:28:21 +00:00
..
backtrace std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
collections Rollup merge of #77072 - sharnoff:hash-docs, r=LukasKalbertodt 2020-10-04 15:45:33 +02:00
env std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
error std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
f32 std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
f64 std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
ffi Deny broken intra-doc links in linkchecker 2020-10-15 20:22:16 -04:00
fs std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
io Rollup merge of #76084 - Lucretiel:split-buffered, r=dtolnay 2020-10-16 02:10:04 +02:00
lazy std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
memchr std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
net Parse SocketAddrV6::scope_id 2020-10-06 22:13:15 +00:00
num std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
os Take sys/vxworks/{fd,fs,io} from sys/unix instead. 2020-10-16 06:19:00 +02:00
panic review: fix nits and move panic safety tests to the correct place 2020-09-25 23:10:24 +02:00
path std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
prelude Fix incorrect link in prelude 2020-09-02 17:38:21 -04:00
process std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
sync (docs): make mutex error comment consistent with codebase 2020-10-07 11:48:26 -06:00
sys Auto merge of #77455 - asm89:faster-spawn, r=kennytm 2020-10-17 06:16:00 +00:00
sys_common Rollup merge of #77648 - fusion-engineering-forks:static-mutex, r=dtolnay 2020-10-16 02:10:15 +02:00
thread Add std:🧵:available_concurrency 2020-10-16 23:36:15 +02:00
time std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
alloc.rs Rename AllocErr to AllocError 2020-09-28 14:51:03 -04:00
ascii.rs Convert many files to intra-doc links 2020-09-02 17:37:40 -04:00
backtrace.rs For backtrace, use StaticMutex instead of a raw sys Mutex. 2020-10-07 13:59:03 +02:00
env.rs Improve docs for std::env::args() 2020-09-04 14:00:09 -07:00
error.rs Rename AllocErr to AllocError 2020-09-28 14:51:03 -04:00
f32.rs std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
f64.rs std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
fs.rs Convert many files to intra-doc links 2020-09-02 17:37:40 -04:00
future.rs Fix stabilization marker for future_readiness_fns 2020-09-15 23:12:08 +02:00
keyword_docs.rs Improve wording for const pointers 2020-09-01 19:44:20 -07:00
lazy.rs Simplify SyncOnceCell's take and drop. 2020-09-12 14:00:38 +02:00
lib.rs stop relying on feature(untagged_unions) in stdlib 2020-10-16 11:33:35 +02:00
macros.rs Add missing links 2020-08-22 20:23:50 -07:00
memchr.rs std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
num.rs std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
panic.rs review: fix nits and move panic safety tests to the correct place 2020-09-25 23:10:24 +02:00
panicking.rs Abort when catch_unwind catches a foreign exception 2020-08-27 21:08:30 +01:00
path.rs Use intra-doc links for links to module-level docs 2020-10-12 19:22:47 -07:00
primitive_docs.rs Fix link to foreign calling conventions 2020-10-15 00:57:22 -07:00
process.rs Add accessors to Command. 2020-09-26 18:58:38 -07:00
rt.rs Prevent __rust_begin_short_backtrace frames from being tail-call optimised away 2020-08-07 19:31:25 +01:00
time.rs Rollup merge of #76388 - poliorcetics:system-time-document-panic, r=KodrAus 2020-10-06 16:25:53 +09:00