File tree 4 files changed +37
-3
lines changed
4 files changed +37
-3
lines changed Original file line number Diff line number Diff line change 36
36
# triple: x86_64-pc-windows-gnu
37
37
- name : Linux
38
38
triple : x86_64-unknown-linux-gnu
39
- # - name: macOS
40
- # triple: x86_64-apple-darwin
39
+ - name : macOS
40
+ triple : x86_64-apple-darwin
41
41
- name : FreeBSD
42
42
triple : x86_64-unknown-freebsd
43
43
Original file line number Diff line number Diff line change @@ -14,5 +14,5 @@ targets = ["x86_64-unknown-linux-gnu"]
14
14
15
15
[dependencies ]
16
16
17
- [target .'cfg(target_os = "freebsd")' .dependencies ]
17
+ [target .'cfg(any( target_os = "macos", target_os = " freebsd") )' .dependencies ]
18
18
libc = " 0.2.107"
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ use std::num::NonZeroUsize;
4
4
5
5
#[ cfg_attr( target_os = "linux" , path = "linux.rs" ) ]
6
6
#[ cfg_attr( target_os = "freebsd" , path = "freebsd.rs" ) ]
7
+ #[ cfg_attr( target_os = "macos" , path = "macos.rs" ) ]
7
8
mod imp;
8
9
9
10
/// Obtain the number of threads currently part of the active process. Returns `None` if the number
Original file line number Diff line number Diff line change
1
+ extern crate libc;
2
+
3
+ use std:: mem;
4
+ use std:: num:: NonZeroUsize ;
5
+
6
+ const PROC_TASKINFO_SIZE : usize = mem:: size_of :: < libc:: proc_taskinfo > ( ) ;
7
+
8
+ pub ( crate ) fn num_threads ( ) -> Option < NonZeroUsize > {
9
+ let buffer = unsafe { libc:: malloc ( PROC_TASKINFO_SIZE ) } ;
10
+ if buffer. is_null ( ) {
11
+ return None ;
12
+ }
13
+
14
+ let result = unsafe {
15
+ libc:: proc_pidinfo (
16
+ libc:: getpid ( ) ,
17
+ libc:: PROC_PIDTASKINFO ,
18
+ 0 ,
19
+ buffer,
20
+ PROC_TASKINFO_SIZE as libc:: c_int ,
21
+ )
22
+ } ;
23
+ if result != PROC_TASKINFO_SIZE as libc:: c_int {
24
+ return None ;
25
+ }
26
+
27
+ let pti = buffer as * mut libc:: proc_taskinfo ;
28
+ // Safety: `malloc`ed memory is aligned for repr(C) structs, so dereference is safe.
29
+ let num_threads = NonZeroUsize :: new ( unsafe { pti. as_ref ( ) } ?. pti_threadnum as usize ) ;
30
+
31
+ unsafe { libc:: free ( pti as * mut libc:: c_void ) } ;
32
+ num_threads
33
+ }
You can’t perform that action at this time.
0 commit comments