]> git.lizzy.rs Git - rust.git/blob - library/test/src/helpers/concurrency.rs
Auto merge of #75157 - rodrimati1992:patch-1, r=oli-obk
[rust.git] / library / test / src / 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     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
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             }
45         }
46     } else if #[cfg(any(
47         target_os = "android",
48         target_os = "cloudabi",
49         target_os = "emscripten",
50         target_os = "fuchsia",
51         target_os = "ios",
52         target_os = "linux",
53         target_os = "macos",
54         target_os = "solaris",
55         target_os = "illumos",
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;
63
64             let mut cpus: libc::c_uint = 0;
65             let mut cpus_size = std::mem::size_of_val(&cpus);
66
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
87         }
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);
94             let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
95
96             unsafe {
97                 libc::sysctl(
98                     mib.as_mut_ptr(),
99                     2,
100                     &mut cpus as *mut _ as *mut _,
101                     &mut cpus_size as *mut _ as *mut _,
102                     ptr::null_mut(),
103                     0,
104                 );
105             }
106             if cpus < 1 {
107                 cpus = 1;
108             }
109             cpus as usize
110         }
111     } else {
112         // FIXME: implement on vxWorks, Redox, HermitCore, Haiku, l4re
113         fn num_cpus() -> usize {
114             1
115         }
116     }
117 }