-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
55 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
name = "num_threads" | ||
version = "0.1.0" | ||
authors = ["Jacob Pratt <[email protected]>"] | ||
edition = "2018" | ||
repository = "https://github.com/jhpratt/thread_count" | ||
categories = ["api-bindings", "hardware-support", "os"] | ||
license = "MIT OR Apache-2.0" | ||
|
@@ -14,3 +13,6 @@ all-features = true | |
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[dependencies] | ||
|
||
[target.'cfg(target_os = "freebsd")'.dependencies] | ||
libc = "0.2.107" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
extern crate libc; | ||
|
||
use std::num::NonZeroUsize; | ||
|
||
extern "C" { | ||
fn kinfo_getproc(pid: libc::pid_t) -> *mut libc::kinfo_proc; | ||
} | ||
|
||
pub(crate) fn num_threads() -> Option<NonZeroUsize> { | ||
// Safety: `kinfo_getproc` and `getpid` are both thread-safe. All invariants of `as_ref` are | ||
// upheld. | ||
NonZeroUsize::new(unsafe { kinfo_getproc(libc::getpid()).as_ref() }?.ki_numthreads as usize) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//! Fallback if no OS matches. | ||
|
||
use std::num::NonZeroUsize; | ||
|
||
pub(crate) fn num_threads() -> Option<NonZeroUsize> { | ||
None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,19 @@ | ||
//! Minimum supported Rust version: 1.36 | ||
//! Minimum supported Rust version: 1.28 | ||
|
||
use std::num::NonZeroUsize; | ||
|
||
#[cfg_attr(target_os = "linux", path = "linux.rs")] | ||
#[cfg_attr(target_os = "freebsd", path = "freebsd.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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use std::fs; | ||
use std::num::NonZeroUsize; | ||
|
||
pub(crate) fn num_threads() -> Option<NonZeroUsize> { | ||
fs::read_dir("/proc/self/task") | ||
// If we can't read the directory, return `None`. | ||
.ok() | ||
// The number of files in the directory is the number of threads. | ||
.and_then(|tasks| NonZeroUsize::new(tasks.count())) | ||
} |