File tree 6 files changed +55
-5
lines changed
6 files changed +55
-5
lines changed Original file line number Diff line number Diff line change 25
25
- .gitignore
26
26
27
27
jobs :
28
- check-other-targets :
28
+ check :
29
29
name : Type checking (${{ matrix.target.name }})
30
30
runs-on : ubuntu-20.04
31
31
if : ${{ (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork) || github.event_name == 'push' }}
49
49
uses : actions-rs/toolchain@v1
50
50
with :
51
51
profile : minimal
52
- toolchain : 1.36
52
+ toolchain : 1.28
53
53
target : ${{ matrix.target.triple }}
54
54
override : true
55
55
58
58
with :
59
59
key : ${{ matrix.target.triple }}
60
60
61
- - name : Check feature powerset
61
+ - name : Type checking
62
62
uses : actions-rs/cargo@v1
63
63
with :
64
64
command : check
Original file line number Diff line number Diff line change 2
2
name = " num_threads"
3
3
version = " 0.1.0"
4
4
authors = [
" Jacob Pratt <[email protected] >" ]
5
- edition = " 2018"
6
5
repository = " https://github.com/jhpratt/thread_count"
7
6
categories = [" api-bindings" , " hardware-support" , " os" ]
8
7
license = " MIT OR Apache-2.0"
@@ -14,3 +13,6 @@ all-features = true
14
13
targets = [" x86_64-unknown-linux-gnu" ]
15
14
16
15
[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