@@ -14,61 +14,36 @@ pub fn get_concurrency() -> usize {
1414 }
1515 Err ( ..) => num_cpus ( ) ,
1616 } ;
17+ }
1718
18- #[ cfg( windows) ]
19- #[ allow( nonstandard_style) ]
20- fn num_cpus ( ) -> usize {
21- #[ repr( C ) ]
22- struct SYSTEM_INFO {
23- wProcessorArchitecture : u16 ,
24- wReserved : u16 ,
25- dwPageSize : u32 ,
26- lpMinimumApplicationAddress : * mut u8 ,
27- lpMaximumApplicationAddress : * mut u8 ,
28- dwActiveProcessorMask : * mut u8 ,
29- dwNumberOfProcessors : u32 ,
30- dwProcessorType : u32 ,
31- dwAllocationGranularity : u32 ,
32- wProcessorLevel : u16 ,
33- wProcessorRevision : u16 ,
34- }
35- extern "system" {
36- fn GetSystemInfo ( info : * mut SYSTEM_INFO ) -> i32 ;
37- }
38- unsafe {
39- let mut sysinfo = std:: mem:: zeroed ( ) ;
40- GetSystemInfo ( & mut sysinfo) ;
41- sysinfo. dwNumberOfProcessors as usize
19+ cfg_if:: cfg_if! {
20+ if #[ cfg( windows) ] {
21+ #[ allow( nonstandard_style) ]
22+ fn num_cpus( ) -> usize {
23+ #[ repr( C ) ]
24+ struct SYSTEM_INFO {
25+ wProcessorArchitecture: u16 ,
26+ wReserved: u16 ,
27+ dwPageSize: u32 ,
28+ lpMinimumApplicationAddress: * mut u8 ,
29+ lpMaximumApplicationAddress: * mut u8 ,
30+ dwActiveProcessorMask: * mut u8 ,
31+ dwNumberOfProcessors: u32 ,
32+ dwProcessorType: u32 ,
33+ dwAllocationGranularity: u32 ,
34+ wProcessorLevel: u16 ,
35+ wProcessorRevision: u16 ,
36+ }
37+ extern "system" {
38+ fn GetSystemInfo ( info: * mut SYSTEM_INFO ) -> i32 ;
39+ }
40+ unsafe {
41+ let mut sysinfo = std:: mem:: zeroed( ) ;
42+ GetSystemInfo ( & mut sysinfo) ;
43+ sysinfo. dwNumberOfProcessors as usize
44+ }
4245 }
43- }
44-
45- #[ cfg( target_os = "vxworks" ) ]
46- fn num_cpus ( ) -> usize {
47- // FIXME: Implement num_cpus on vxWorks
48- 1
49- }
50-
51- #[ cfg( target_os = "redox" ) ]
52- fn num_cpus ( ) -> usize {
53- // FIXME: Implement num_cpus on Redox
54- 1
55- }
56-
57- #[ cfg( target_os = "hermit" ) ]
58- fn num_cpus ( ) -> usize {
59- // FIXME: Implement num_cpus on HermitCore
60- 1
61- }
62-
63- #[ cfg( any(
64- all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ,
65- all( target_vendor = "fortanix" , target_env = "sgx" )
66- ) ) ]
67- fn num_cpus ( ) -> usize {
68- 1
69- }
70-
71- #[ cfg( any(
46+ } else if #[ cfg( any(
7247 target_os = "android" ,
7348 target_os = "cloudabi" ,
7449 target_os = "emscripten" ,
@@ -78,23 +53,46 @@ pub fn get_concurrency() -> usize {
7853 target_os = "macos" ,
7954 target_os = "solaris" ,
8055 target_os = "illumos" ,
81- ) ) ]
82- fn num_cpus ( ) -> usize {
83- unsafe { libc:: sysconf ( libc:: _SC_NPROCESSORS_ONLN) as usize }
84- }
85-
86- #[ cfg( any( target_os = "freebsd" , target_os = "dragonfly" , target_os = "netbsd" ) ) ]
87- fn num_cpus ( ) -> usize {
88- use std:: ptr;
56+ ) ) ] {
57+ fn num_cpus( ) -> usize {
58+ unsafe { libc:: sysconf( libc:: _SC_NPROCESSORS_ONLN) as usize }
59+ }
60+ } else if #[ cfg( any( target_os = "freebsd" , target_os = "dragonfly" , target_os = "netbsd" ) ) ] {
61+ fn num_cpus( ) -> usize {
62+ use std:: ptr;
8963
90- let mut cpus: libc:: c_uint = 0 ;
91- let mut cpus_size = std:: mem:: size_of_val ( & cpus) ;
64+ let mut cpus: libc:: c_uint = 0 ;
65+ let mut cpus_size = std:: mem:: size_of_val( & cpus) ;
9266
93- unsafe {
94- cpus = libc:: sysconf ( libc:: _SC_NPROCESSORS_ONLN) as libc:: c_uint ;
67+ unsafe {
68+ cpus = libc:: sysconf( libc:: _SC_NPROCESSORS_ONLN) as libc:: c_uint;
69+ }
70+ if cpus < 1 {
71+ let mut mib = [ libc:: CTL_HW , libc:: HW_NCPU , 0 , 0 ] ;
72+ unsafe {
73+ libc:: sysctl(
74+ mib. as_mut_ptr( ) ,
75+ 2 ,
76+ & mut cpus as * mut _ as * mut _,
77+ & mut cpus_size as * mut _ as * mut _,
78+ ptr:: null_mut( ) ,
79+ 0 ,
80+ ) ;
81+ }
82+ if cpus < 1 {
83+ cpus = 1 ;
84+ }
85+ }
86+ cpus as usize
9587 }
96- if cpus < 1 {
88+ } else if #[ cfg( target_os = "openbsd" ) ] {
89+ fn num_cpus( ) -> usize {
90+ use std:: ptr;
91+
92+ let mut cpus: libc:: c_uint = 0 ;
93+ let mut cpus_size = std:: mem:: size_of_val( & cpus) ;
9794 let mut mib = [ libc:: CTL_HW , libc:: HW_NCPU , 0 , 0 ] ;
95+
9896 unsafe {
9997 libc:: sysctl(
10098 mib. as_mut_ptr( ) ,
@@ -108,43 +106,12 @@ pub fn get_concurrency() -> usize {
108106 if cpus < 1 {
109107 cpus = 1 ;
110108 }
109+ cpus as usize
111110 }
112- cpus as usize
113- }
114-
115- #[ cfg( target_os = "openbsd" ) ]
116- fn num_cpus ( ) -> usize {
117- use std:: ptr;
118-
119- let mut cpus: libc:: c_uint = 0 ;
120- let mut cpus_size = std:: mem:: size_of_val ( & cpus) ;
121- let mut mib = [ libc:: CTL_HW , libc:: HW_NCPU , 0 , 0 ] ;
122-
123- unsafe {
124- libc:: sysctl (
125- mib. as_mut_ptr ( ) ,
126- 2 ,
127- & mut cpus as * mut _ as * mut _ ,
128- & mut cpus_size as * mut _ as * mut _ ,
129- ptr:: null_mut ( ) ,
130- 0 ,
131- ) ;
132- }
133- if cpus < 1 {
134- cpus = 1 ;
111+ } else {
112+ // FIXME: implement on vxWorks, Redox, HermitCore, Haiku, l4re
113+ fn num_cpus( ) -> usize {
114+ 1
135115 }
136- cpus as usize
137- }
138-
139- #[ cfg( target_os = "haiku" ) ]
140- fn num_cpus ( ) -> usize {
141- // FIXME: implement
142- 1
143- }
144-
145- #[ cfg( target_os = "l4re" ) ]
146- fn num_cpus ( ) -> usize {
147- // FIXME: implement
148- 1
149116 }
150117}
0 commit comments