about summary refs log tree commit diff
diff options
context:
space:
mode:
author许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com>2024-11-30 12:57:35 +0800
committerGitHub <noreply@github.com>2024-11-30 12:57:35 +0800
commit70b107910bc26ae21b6f3e7204b48b2cf4f4fd53 (patch)
tree32efbc0c094282ea0025a6bbc908b003ce6dcd14
parent9e716c27882eba9f073a5c6e768e9e371bd03169 (diff)
parent4342ec0cf29d5d2e8bc7f66546723882b7728789 (diff)
downloadrust-70b107910bc26ae21b6f3e7204b48b2cf4f4fd53.tar.gz
rust-70b107910bc26ae21b6f3e7204b48b2cf4f4fd53.zip
Rollup merge of #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.
-rw-r--r--library/std/src/sys/pal/wasi/thread.rs16
1 files changed, 13 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..f5e19f26bfe 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,14 @@ 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()),
+                cpus => NonZero::new(cpus as usize).ok_or(io::Error::UNKNOWN_THREAD_COUNT),
+            }
+        } else {
+            crate::sys::unsupported()
+        }
+    }
 }