Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move available_concurrency implementation to sys #85182

Merged
merged 3 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions library/std/src/sys/hermit/thread.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#![allow(dead_code)]

use super::unsupported;
use crate::ffi::CStr;
use crate::io;
use crate::mem;
use crate::num::NonZeroUsize;
use crate::sys::hermit::abi;
use crate::sys::hermit::thread_local_dtor::run_dtors;
use crate::time::Duration;
Expand Down Expand Up @@ -95,6 +97,10 @@ impl Thread {
}
}

pub fn available_concurrency() -> io::Result<NonZeroUsize> {
unsupported()
}

pub mod guard {
pub type Guard = !;
pub unsafe fn current() -> Option<Guard> {
Expand Down
6 changes: 6 additions & 0 deletions library/std/src/sys/sgx/thread.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![cfg_attr(test, allow(dead_code))] // why is this necessary?
use super::unsupported;
use crate::ffi::CStr;
use crate::io;
use crate::num::NonZeroUsize;
use crate::time::Duration;

use super::abi::usercalls;
Expand Down Expand Up @@ -135,6 +137,10 @@ impl Thread {
}
}

pub fn available_concurrency() -> io::Result<NonZeroUsize> {
unsupported()
}

pub mod guard {
pub type Guard = !;
pub unsafe fn current() -> Option<Guard> {
Expand Down
83 changes: 83 additions & 0 deletions library/std/src/sys/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::cmp;
use crate::ffi::CStr;
use crate::io;
use crate::mem;
use crate::num::NonZeroUsize;
use crate::ptr;
use crate::sys::{os, stack_overflow};
use crate::time::Duration;
Expand Down Expand Up @@ -198,6 +199,88 @@ impl Drop for Thread {
}
}

pub fn available_concurrency() -> io::Result<NonZeroUsize> {
cfg_if::cfg_if! {
if #[cfg(any(
target_os = "android",
target_os = "emscripten",
target_os = "fuchsia",
target_os = "ios",
target_os = "linux",
target_os = "macos",
target_os = "solaris",
target_os = "illumos",
))] {
match unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) } {
-1 => Err(io::Error::last_os_error()),
0 => Err(io::Error::new_const(io::ErrorKind::NotFound, &"The number of hardware threads is not known for the target platform")),
cpus => Ok(unsafe { NonZeroUsize::new_unchecked(cpus as usize) }),
}
} else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))] {
use crate::ptr;

let mut cpus: libc::c_uint = 0;
let mut cpus_size = crate::mem::size_of_val(&cpus);

unsafe {
cpus = libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as libc::c_uint;
}

// Fallback approach in case of errors or no hardware threads.
if cpus < 1 {
let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
let res = unsafe {
libc::sysctl(
mib.as_mut_ptr(),
2,
&mut cpus as *mut _ as *mut _,
&mut cpus_size as *mut _ as *mut _,
ptr::null_mut(),
0,
)
};

// Handle errors if any.
if res == -1 {
return Err(io::Error::last_os_error());
} else if cpus == 0 {
return Err(io::Error::new_const(io::ErrorKind::NotFound, &"The number of hardware threads is not known for the target platform"));
}
}
Ok(unsafe { NonZeroUsize::new_unchecked(cpus as usize) })
} else if #[cfg(target_os = "openbsd")] {
use crate::ptr;

let mut cpus: libc::c_uint = 0;
let mut cpus_size = crate::mem::size_of_val(&cpus);
let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];

let res = unsafe {
libc::sysctl(
mib.as_mut_ptr(),
2,
&mut cpus as *mut _ as *mut _,
&mut cpus_size as *mut _ as *mut _,
ptr::null_mut(),
0,
)
};

// Handle errors if any.
if res == -1 {
return Err(io::Error::last_os_error());
} else if cpus == 0 {
return Err(io::Error::new_const(io::ErrorKind::NotFound, &"The number of hardware threads is not known for the target platform"));
}

Ok(unsafe { NonZeroUsize::new_unchecked(cpus as usize) })
} else {
// FIXME: implement on vxWorks, Redox, Haiku, l4re
Err(io::Error::new_const(io::ErrorKind::Unsupported, &"Getting the number of hardware threads is not supported on the target platform"))
}
}
}

#[cfg(all(
not(target_os = "linux"),
not(target_os = "freebsd"),
Expand Down
5 changes: 5 additions & 0 deletions library/std/src/sys/unsupported/thread.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::unsupported;
use crate::ffi::CStr;
use crate::io;
use crate::num::NonZeroUsize;
use crate::time::Duration;

pub struct Thread(!);
Expand Down Expand Up @@ -30,6 +31,10 @@ impl Thread {
}
}

pub fn available_concurrency() -> io::Result<NonZeroUsize> {
unsupported()
}

pub mod guard {
pub type Guard = !;
pub unsafe fn current() -> Option<Guard> {
Expand Down
5 changes: 5 additions & 0 deletions library/std/src/sys/wasi/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::ffi::CStr;
use crate::io;
use crate::mem;
use crate::num::NonZeroUsize;
use crate::sys::unsupported;
use crate::time::Duration;

Expand Down Expand Up @@ -63,6 +64,10 @@ impl Thread {
}
}

pub fn available_concurrency() -> io::Result<NonZeroUsize> {
unsupported()
}

pub mod guard {
pub type Guard = !;
pub unsafe fn current() -> Option<Guard> {
Expand Down
6 changes: 6 additions & 0 deletions library/std/src/sys/wasm/atomics/thread.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::unsupported;
use crate::ffi::CStr;
use crate::io;
use crate::num::NonZeroUsize;
use crate::sys::unsupported;
use crate::time::Duration;

Expand Down Expand Up @@ -39,6 +41,10 @@ impl Thread {
pub fn join(self) {}
}

pub fn available_concurrency() -> io::Result<NonZeroUsize> {
unsupported()
}

pub mod guard {
pub type Guard = !;
pub unsafe fn current() -> Option<Guard> {
Expand Down
18 changes: 18 additions & 0 deletions library/std/src/sys/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use libc::{c_void, size_t, wchar_t};
pub use self::EXCEPTION_DISPOSITION::*;
pub use self::FILE_INFO_BY_HANDLE_CLASS::*;

pub type DWORD_PTR = ULONG_PTR;
pub type DWORD = c_ulong;
pub type NonZeroDWORD = NonZero_c_ulong;
pub type HANDLE = LPVOID;
Expand Down Expand Up @@ -53,6 +54,7 @@ pub type LPWSADATA = *mut WSADATA;
pub type LPWSAPROTOCOL_INFO = *mut WSAPROTOCOL_INFO;
pub type LPWSTR = *mut WCHAR;
pub type LPFILETIME = *mut FILETIME;
pub type LPSYSTEM_INFO = *mut SYSTEM_INFO;
pub type LPWSABUF = *mut WSABUF;
pub type LPWSAOVERLAPPED = *mut c_void;
pub type LPWSAOVERLAPPED_COMPLETION_ROUTINE = *mut c_void;
Expand Down Expand Up @@ -533,6 +535,21 @@ pub struct FILETIME {
pub dwHighDateTime: DWORD,
}

#[repr(C)]
pub struct SYSTEM_INFO {
pub wProcessorArchitecture: WORD,
pub wReserved: WORD,
pub dwPageSize: DWORD,
pub lpMinimumApplicationAddress: LPVOID,
pub lpMaximumApplicationAddress: LPVOID,
pub dwActiveProcessorMask: DWORD_PTR,
pub dwNumberOfProcessors: DWORD,
pub dwProcessorType: DWORD,
pub dwAllocationGranularity: DWORD,
pub wProcessorLevel: WORD,
pub wProcessorRevision: WORD,
}

#[repr(C)]
pub struct OVERLAPPED {
pub Internal: *mut c_ulong,
Expand Down Expand Up @@ -934,6 +951,7 @@ extern "system" {
pub fn GetModuleHandleW(lpModuleName: LPCWSTR) -> HMODULE;

pub fn GetSystemTimeAsFileTime(lpSystemTimeAsFileTime: LPFILETIME);
pub fn GetSystemInfo(lpSystemInfo: LPSYSTEM_INFO);

pub fn CreateEventW(
lpEventAttributes: LPSECURITY_ATTRIBUTES,
Expand Down
16 changes: 16 additions & 0 deletions library/std/src/sys/windows/thread.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::ffi::CStr;
use crate::io;
use crate::num::NonZeroUsize;
use crate::ptr;
use crate::sys::c;
use crate::sys::handle::Handle;
Expand Down Expand Up @@ -98,6 +99,21 @@ impl Thread {
}
}

pub fn available_concurrency() -> io::Result<NonZeroUsize> {
let res = unsafe {
let mut sysinfo: c::SYSTEM_INFO = crate::mem::zeroed();
c::GetSystemInfo(&mut sysinfo);
sysinfo.dwNumberOfProcessors as usize
};
match res {
0 => Err(io::Error::new_const(
io::ErrorKind::NotFound,
&"The number of hardware threads is not known for the target platform",
)),
cpus => Ok(unsafe { NonZeroUsize::new_unchecked(cpus) }),
}
}

#[cfg_attr(test, allow(dead_code))]
pub mod guard {
pub type Guard = !;
Expand Down
Loading