diff options
| author | Sebastian Urban <surban@surban.net> | 2024-11-26 14:01:49 +0100 |
|---|---|---|
| committer | Sebastian Urban <surban@surban.net> | 2024-11-26 14:06:05 +0100 |
| commit | f0b7008648dbe2fea5d23bb2ad8ce622ddf4e133 (patch) | |
| tree | dba1eb15ecd552d9d9377c928db35bf7cdd62e50 /library/std/src/sys | |
| parent | 7db7489f9bc274cb60c4956bfa56de0185eb1b9b (diff) | |
| download | rust-f0b7008648dbe2fea5d23bb2ad8ce622ddf4e133.tar.gz rust-f0b7008648dbe2fea5d23bb2ad8ce622ddf4e133.zip | |
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.
Diffstat (limited to 'library/std/src/sys')
| -rw-r--r-- | library/std/src/sys/pal/wasi/thread.rs | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/library/std/src/sys/pal/wasi/thread.rs b/library/std/src/sys/pal/wasi/thread.rs index 4b83870fdea..3b44f77631f 100644 --- a/library/std/src/sys/pal/wasi/thread.rs +++ b/library/std/src/sys/pal/wasi/thread.rs @@ -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 @@ cfg_if::cfg_if! { #[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 @@ impl Thread { } } else { pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>) -> io::Result<Thread> { - unsupported() + crate::sys::unsupported() } } } @@ -187,5 +188,15 @@ impl Thread { } 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() + } + } } |
