File tree Expand file tree Collapse file tree 6 files changed +55
-5
lines changed
Expand file tree Collapse file tree 6 files changed +55
-5
lines changed Original file line number Diff line number Diff line change 2525 - .gitignore
2626
2727jobs :
28- check-other-targets :
28+ check :
2929 name : Type checking (${{ matrix.target.name }})
3030 runs-on : ubuntu-20.04
3131 if : ${{ (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork) || github.event_name == 'push' }}
4949 uses : actions-rs/toolchain@v1
5050 with :
5151 profile : minimal
52- toolchain : 1.36
52+ toolchain : 1.28
5353 target : ${{ matrix.target.triple }}
5454 override : true
5555
5858 with :
5959 key : ${{ matrix.target.triple }}
6060
61- - name : Check feature powerset
61+ - name : Type checking
6262 uses : actions-rs/cargo@v1
6363 with :
6464 command : check
Original file line number Diff line number Diff line change 22name = " num_threads"
33version = " 0.1.0"
44authors = [
" Jacob Pratt <[email protected] >" ]
5- edition = " 2018"
65repository = " https://github.com/jhpratt/thread_count"
76categories = [" api-bindings" , " hardware-support" , " os" ]
87license = " MIT OR Apache-2.0"
@@ -14,3 +13,6 @@ all-features = true
1413targets = [" x86_64-unknown-linux-gnu" ]
1514
1615[dependencies ]
16+
17+ [target .'cfg(target_os = "freebsd")' .dependencies ]
18+ libc = " 0.2.107"
Original file line number Diff line number Diff line change 1+ extern crate libc;
2+
3+ use std:: num:: NonZeroUsize ;
4+
5+ extern "C" {
6+ fn kinfo_getproc ( pid : libc:: pid_t ) -> * mut libc:: kinfo_proc ;
7+ }
8+
9+ pub ( crate ) fn num_threads ( ) -> Option < NonZeroUsize > {
10+ // Safety: `kinfo_getproc` and `getpid` are both thread-safe. All invariants of `as_ref` are
11+ // upheld.
12+ NonZeroUsize :: new ( unsafe { kinfo_getproc ( libc:: getpid ( ) ) . as_ref ( ) } ?. ki_numthreads as usize )
13+ }
Original file line number Diff line number Diff line change 1+ //! Fallback if no OS matches.
2+
3+ use std:: num:: NonZeroUsize ;
4+
5+ pub ( crate ) fn num_threads ( ) -> Option < NonZeroUsize > {
6+ None
7+ }
Original file line number Diff line number Diff line change 1- //! Minimum supported Rust version: 1.36
1+ //! Minimum supported Rust version: 1.28
2+
3+ use std:: num:: NonZeroUsize ;
4+
5+ #[ cfg_attr( target_os = "linux" , path = "linux.rs" ) ]
6+ #[ cfg_attr( target_os = "freebsd" , path = "freebsd.rs" ) ]
7+ mod imp;
8+
9+ /// Obtain the number of threads currently part of the active process. Returns `None` if the number
10+ /// of threads cannot be determined.
11+ pub fn num_threads ( ) -> Option < NonZeroUsize > {
12+ imp:: num_threads ( )
13+ }
14+
15+ /// Determine if the current process is single-threaded. Returns `None` if the number of threads
16+ /// cannot be determined.
17+ pub fn is_single_threaded ( ) -> Option < bool > {
18+ num_threads ( ) . map ( |n| n. get ( ) == 1 )
19+ }
Original file line number Diff line number Diff line change 1+ use std:: fs;
2+ use std:: num:: NonZeroUsize ;
3+
4+ pub ( crate ) fn num_threads ( ) -> Option < NonZeroUsize > {
5+ fs:: read_dir ( "/proc/self/task" )
6+ // If we can't read the directory, return `None`.
7+ . ok ( )
8+ // The number of files in the directory is the number of threads.
9+ . and_then ( |tasks| NonZeroUsize :: new ( tasks. count ( ) ) )
10+ }
You can’t perform that action at this time.
0 commit comments