diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:41 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:41 +0000 |
commit | 10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch) | |
tree | bdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/num_threads/src/lib.rs | |
parent | Releasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff) | |
download | rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip |
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/num_threads/src/lib.rs')
-rw-r--r-- | vendor/num_threads/src/lib.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/vendor/num_threads/src/lib.rs b/vendor/num_threads/src/lib.rs new file mode 100644 index 000000000..c21380282 --- /dev/null +++ b/vendor/num_threads/src/lib.rs @@ -0,0 +1,64 @@ +//! Minimum supported Rust version: 1.28 + +use std::num::NonZeroUsize; + +#[cfg_attr(any(target_os = "linux", target_os = "android"), path = "linux.rs")] +#[cfg_attr(target_os = "freebsd", path = "freebsd.rs")] +#[cfg_attr(any(target_os = "macos", target_os = "ios"), path = "apple.rs")] +mod imp; + +/// Obtain the number of threads currently part of the active process. Returns `None` if the number +/// of threads cannot be determined. +pub fn num_threads() -> Option<NonZeroUsize> { + imp::num_threads() +} + +/// Determine if the current process is single-threaded. Returns `None` if the number of threads +/// cannot be determined. +pub fn is_single_threaded() -> Option<bool> { + num_threads().map(|n| n.get() == 1) +} + +#[cfg(test)] +mod test { + use std::num::NonZeroUsize; + + // Run each expression in its own thread. + macro_rules! threaded { + ($first:expr;) => { + $first; + }; + ($first:expr; $($rest:expr;)*) => { + $first; + ::std::thread::spawn(|| { + threaded!($($rest;)*); + }) + .join() + .unwrap(); + }; + } + + #[test] + fn num_threads() { + threaded! { + assert_eq!(super::num_threads().map(NonZeroUsize::get), Some(1)); + assert_eq!(super::num_threads().map(NonZeroUsize::get), Some(2)); + assert_eq!(super::num_threads().map(NonZeroUsize::get), Some(3)); + assert_eq!(super::num_threads().map(NonZeroUsize::get), Some(4)); + assert_eq!(super::num_threads().map(NonZeroUsize::get), Some(5)); + assert_eq!(super::num_threads().map(NonZeroUsize::get), Some(6)); + } + } + + #[test] + fn is_single_threaded() { + threaded! { + assert_eq!(super::is_single_threaded(), Some(true)); + assert_eq!(super::is_single_threaded(), Some(false)); + assert_eq!(super::is_single_threaded(), Some(false)); + assert_eq!(super::is_single_threaded(), Some(false)); + assert_eq!(super::is_single_threaded(), Some(false)); + assert_eq!(super::is_single_threaded(), Some(false)); + } + } +} |