]> git.lizzy.rs Git - rust.git/blob - src/libtest/helpers/concurrency.rs
Rollup merge of #68176 - GuillaumeGomez:clean-up-err-codes, r=Dylan-DPC
[rust.git] / src / libtest / helpers / concurrency.rs
1 //! Helper module which helps to determine amount of threads to be used
2 //! during tests execution.
3 use std::env;
4
5 #[allow(deprecated)]
6 pub fn get_concurrency() -> usize {
7     return match env::var("RUST_TEST_THREADS") {
8         Ok(s) => {
9             let opt_n: Option<usize> = s.parse().ok();
10             match opt_n {
11                 Some(n) if n > 0 => n,
12                 _ => panic!("RUST_TEST_THREADS is `{}`, should be a positive integer.", s),
13             }
14         }
15         Err(..) => num_cpus(),
16     };
17
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
42         }
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(
72         target_os = "android",
73         target_os = "cloudabi",
74         target_os = "emscripten",
75         target_os = "fuchsia",
76         target_os = "ios",
77         target_os = "linux",
78         target_os = "macos",
79         target_os = "solaris",
80     ))]
81     fn num_cpus() -> usize {
82         unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as usize }
83     }
84
85     #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))]
86     fn num_cpus() -> usize {
87         use std::ptr;
88
89         let mut cpus: libc::c_uint = 0;
90         let mut cpus_size = std::mem::size_of_val(&cpus);
91
92         unsafe {
93             cpus = libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as libc::c_uint;
94         }
95         if cpus < 1 {
96             let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
97             unsafe {
98                 libc::sysctl(
99                     mib.as_mut_ptr(),
100                     2,
101                     &mut cpus as *mut _ as *mut _,
102                     &mut cpus_size as *mut _ as *mut _,
103                     ptr::null_mut(),
104                     0,
105                 );
106             }
107             if cpus < 1 {
108                 cpus = 1;
109             }
110         }
111         cpus as usize
112     }
113
114     #[cfg(target_os = "openbsd")]
115     fn num_cpus() -> usize {
116         use std::ptr;
117
118         let mut cpus: libc::c_uint = 0;
119         let mut cpus_size = std::mem::size_of_val(&cpus);
120         let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
121
122         unsafe {
123             libc::sysctl(
124                 mib.as_mut_ptr(),
125                 2,
126                 &mut cpus as *mut _ as *mut _,
127                 &mut cpus_size as *mut _ as *mut _,
128                 ptr::null_mut(),
129                 0,
130             );
131         }
132         if cpus < 1 {
133             cpus = 1;
134         }
135         cpus as usize
136     }
137
138     #[cfg(target_os = "haiku")]
139     fn num_cpus() -> usize {
140         // FIXME: implement
141         1
142     }
143
144     #[cfg(target_os = "l4re")]
145     fn num_cpus() -> usize {
146         // FIXME: implement
147         1
148     }
149 }