Skip to content

Commit ebd39e5

Browse files
committed
windows: provide more accurate result for available_parallelism if cores > 64
1 parent fd9525a commit ebd39e5

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

library/std/src/sys/windows/c/windows_sys.lst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2477,11 +2477,11 @@ Windows.Win32.System.Pipes.PIPE_TYPE_BYTE
24772477
Windows.Win32.System.Pipes.PIPE_TYPE_MESSAGE
24782478
Windows.Win32.System.Pipes.PIPE_WAIT
24792479
Windows.Win32.System.SystemInformation.GetSystemDirectoryW
2480-
Windows.Win32.System.SystemInformation.GetSystemInfo
24812480
Windows.Win32.System.SystemInformation.GetSystemTimeAsFileTime
24822481
Windows.Win32.System.SystemInformation.GetWindowsDirectoryW
24832482
Windows.Win32.System.SystemInformation.PROCESSOR_ARCHITECTURE
24842483
Windows.Win32.System.SystemInformation.SYSTEM_INFO
2484+
Windows.Win32.System.SystemServices.ALL_PROCESSOR_GROUPS
24852485
Windows.Win32.System.SystemServices.DLL_PROCESS_DETACH
24862486
Windows.Win32.System.SystemServices.DLL_THREAD_DETACH
24872487
Windows.Win32.System.SystemServices.EXCEPTION_MAXIMUM_PARAMETERS
@@ -2513,6 +2513,7 @@ Windows.Win32.System.Threading.DEBUG_PROCESS
25132513
Windows.Win32.System.Threading.DETACHED_PROCESS
25142514
Windows.Win32.System.Threading.ExitProcess
25152515
Windows.Win32.System.Threading.EXTENDED_STARTUPINFO_PRESENT
2516+
Windows.Win32.System.Threading.GetActiveProcessorCount
25162517
Windows.Win32.System.Threading.GetCurrentProcess
25172518
Windows.Win32.System.Threading.GetCurrentProcessId
25182519
Windows.Win32.System.Threading.GetCurrentThread

library/std/src/sys/windows/c/windows_sys.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ extern "system" {
219219
pub fn FreeEnvironmentStringsW(penv: PCWSTR) -> BOOL;
220220
}
221221
#[link(name = "kernel32")]
222+
extern "system" {
223+
pub fn GetActiveProcessorCount(groupnumber: u16) -> u32;
224+
}
225+
#[link(name = "kernel32")]
222226
extern "system" {
223227
pub fn GetCommandLineW() -> PCWSTR;
224228
}
@@ -338,10 +342,6 @@ extern "system" {
338342
pub fn GetSystemDirectoryW(lpbuffer: PWSTR, usize: u32) -> u32;
339343
}
340344
#[link(name = "kernel32")]
341-
extern "system" {
342-
pub fn GetSystemInfo(lpsysteminfo: *mut SYSTEM_INFO) -> ();
343-
}
344-
#[link(name = "kernel32")]
345345
extern "system" {
346346
pub fn GetSystemTimeAsFileTime(lpsystemtimeasfiletime: *mut FILETIME) -> ();
347347
}
@@ -822,6 +822,7 @@ impl ::core::clone::Clone for ADDRINFOA {
822822
pub const AF_INET: ADDRESS_FAMILY = 2u16;
823823
pub const AF_INET6: ADDRESS_FAMILY = 23u16;
824824
pub const AF_UNSPEC: ADDRESS_FAMILY = 0u16;
825+
pub const ALL_PROCESSOR_GROUPS: u32 = 65535u32;
825826
#[repr(C)]
826827
pub union ARM64_NT_NEON128 {
827828
pub Anonymous: ARM64_NT_NEON128_0,

library/std/src/sys/windows/thread.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,13 @@ impl Thread {
101101

102102
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
103103
let res = unsafe {
104-
let mut sysinfo: c::SYSTEM_INFO = crate::mem::zeroed();
105-
c::GetSystemInfo(&mut sysinfo);
106-
sysinfo.dwNumberOfProcessors as usize
104+
// FIXME: windows::Win32::System::SystemServices::ALL_PROCESSOR_GROUPS should be u16, not u32
105+
// FIXME: if you need even more accurate result on 32-bit Windows with more, than 64 cores,
106+
// consider implementing this using GetLogicalProcessorInformationEx
107+
c::GetActiveProcessorCount(c::ALL_PROCESSOR_GROUPS as u16) as usize
107108
};
108109
match res {
109-
0 => Err(io::const_io_error!(
110-
io::ErrorKind::NotFound,
111-
"The number of hardware threads is not known for the target platform",
112-
)),
110+
0 => Err(io::Error::last_os_error()),
113111
cpus => Ok(unsafe { NonZeroUsize::new_unchecked(cpus) }),
114112
}
115113
}

0 commit comments

Comments
 (0)