Skip to content

Commit

Permalink
Implement Linux, FreeBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Nov 15, 2021
1 parent 876dbb8 commit 08f7d9e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ on:
- .gitignore

jobs:
check-other-targets:
check:
name: Type checking (${{ matrix.target.name }})
runs-on: ubuntu-20.04
if: ${{ (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork) || github.event_name == 'push' }}
Expand All @@ -49,7 +49,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.36
toolchain: 1.28
target: ${{ matrix.target.triple }}
override: true

Expand All @@ -58,7 +58,7 @@ jobs:
with:
key: ${{ matrix.target.triple }}

- name: Check feature powerset
- name: Type checking
uses: actions-rs/cargo@v1
with:
command: check
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -14,3 +13,6 @@ all-features = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]

[target.'cfg(target_os = "freebsd")'.dependencies]
libc = "0.2.107"
13 changes: 13 additions & 0 deletions src/freebsd.rs
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)
}
7 changes: 7 additions & 0 deletions src/imp.rs
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
}
20 changes: 19 additions & 1 deletion src/lib.rs
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)
}
10 changes: 10 additions & 0 deletions src/linux.rs
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()))
}

0 comments on commit 08f7d9e

Please sign in to comment.