Skip to content

Commit 70b1079

Browse files
authored
Rollup merge of rust-lang#133496 - rust-wasi-web:wasi-available-parallelism, r=Amanieu
thread::available_parallelism for wasm32-wasip1-threads The target has limited POSIX support and provides the `libc::sysconf` function which allows querying the number of available CPUs.
2 parents 9e716c2 + 4342ec0 commit 70b1079

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

library/std/src/sys/pal/wasi/thread.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use crate::ffi::CStr;
44
use crate::num::NonZero;
5-
use crate::sys::unsupported;
65
use crate::time::Duration;
76
use crate::{io, mem};
87

@@ -34,6 +33,8 @@ cfg_if::cfg_if! {
3433
#[allow(non_camel_case_types)]
3534
pub type pthread_t = *mut ffi::c_void;
3635

36+
pub const _SC_NPROCESSORS_ONLN: ffi::c_int = 84;
37+
3738
extern "C" {
3839
pub fn pthread_create(
3940
native: *mut pthread_t,
@@ -121,7 +122,7 @@ impl Thread {
121122
}
122123
} else {
123124
pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>) -> io::Result<Thread> {
124-
unsupported()
125+
crate::sys::unsupported()
125126
}
126127
}
127128
}
@@ -187,5 +188,14 @@ impl Thread {
187188
}
188189

189190
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
190-
unsupported()
191+
cfg_if::cfg_if! {
192+
if #[cfg(target_feature = "atomics")] {
193+
match unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) } {
194+
-1 => Err(io::Error::last_os_error()),
195+
cpus => NonZero::new(cpus as usize).ok_or(io::Error::UNKNOWN_THREAD_COUNT),
196+
}
197+
} else {
198+
crate::sys::unsupported()
199+
}
200+
}
191201
}

0 commit comments

Comments
 (0)