thread::available_parallelism for wasm32-wasip1-threads

The target has limited POSIX support and provides the sysconf
function which allows querying the number of available
CPUs.
This commit is contained in:
Sebastian Urban
2024-11-26 14:01:49 +01:00
parent 7db7489f9b
commit f0b7008648
+14 -3
View File
@@ -2,7 +2,6 @@
use crate::ffi::CStr;
use crate::num::NonZero;
use crate::sys::unsupported;
use crate::time::Duration;
use crate::{io, mem};
@@ -34,6 +33,8 @@ pub struct pthread_attr_t {
#[allow(non_camel_case_types)]
pub type pthread_t = *mut ffi::c_void;
pub const _SC_NPROCESSORS_ONLN: ffi::c_int = 84;
extern "C" {
pub fn pthread_create(
native: *mut pthread_t,
@@ -121,7 +122,7 @@ extern "C" fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
}
} else {
pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>) -> io::Result<Thread> {
unsupported()
crate::sys::unsupported()
}
}
}
@@ -187,5 +188,15 @@ pub fn join(self) {
}
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
unsupported()
cfg_if::cfg_if! {
if #[cfg(target_feature = "atomics")] {
match unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) } {
-1 => Err(io::Error::last_os_error()),
0 => Err(io::Error::UNKNOWN_THREAD_COUNT),
cpus => Ok(unsafe { NonZero::new_unchecked(cpus as usize) }),
}
} else {
crate::sys::unsupported()
}
}
}