1
Fork 0

Move available_concurrency implementation to sys

This commit is contained in:
Christiaan Dirkx 2021-04-29 11:40:14 +02:00
parent 3824017f8e
commit 9063edaf3b
10 changed files with 194 additions and 162 deletions

View file

@ -155,6 +155,7 @@ use crate::fmt;
use crate::io;
use crate::mem;
use crate::num::NonZeroU64;
use crate::num::NonZeroUsize;
use crate::panic;
use crate::panicking;
use crate::str;
@ -174,15 +175,9 @@ use crate::time::Duration;
#[macro_use]
mod local;
#[unstable(feature = "available_concurrency", issue = "74479")]
mod available_concurrency;
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::local::{AccessError, LocalKey};
#[unstable(feature = "available_concurrency", issue = "74479")]
pub use available_concurrency::available_concurrency;
// The types used by the thread_local! macro to access TLS keys. Note that there
// are two types, the "OS" type and the "fast" type. The OS thread local key
// type is accessed via platform-specific API calls and is slow, while the fast
@ -1422,3 +1417,39 @@ fn _assert_sync_and_send() {
_assert_both::<JoinHandle<()>>();
_assert_both::<Thread>();
}
/// Returns the number of hardware threads available to the program.
///
/// This value should be considered only a hint.
///
/// # Platform-specific behavior
///
/// If interpreted as the number of actual hardware threads, it may undercount on
/// Windows systems with more than 64 hardware threads. If interpreted as the
/// available concurrency for that process, it may overcount on Windows systems
/// when limited by a process wide affinity mask or job object limitations, and
/// it may overcount on Linux systems when limited by a process wide affinity
/// mask or affected by cgroups limits.
///
/// # Errors
///
/// This function will return an error in the following situations, but is not
/// limited to just these cases:
///
/// - If the number of hardware threads is not known for the target platform.
/// - The process lacks permissions to view the number of hardware threads
/// available.
///
/// # Examples
///
/// ```
/// # #![allow(dead_code)]
/// #![feature(available_concurrency)]
/// use std::thread;
///
/// let count = thread::available_concurrency().map(|n| n.get()).unwrap_or(1);
/// ```
#[unstable(feature = "available_concurrency", issue = "74479")]
pub fn available_concurrency() -> io::Result<NonZeroUsize> {
imp::available_concurrency()
}