Fix thread::available_parallelism on WASI targets with threads#153604
Conversation
|
rustbot has assigned @Mark-Simulacrum. Use Why was this reviewer chosen?The reviewer was selected based on:
|
There was a problem hiding this comment.
Pull request overview
This PR restores thread::available_parallelism() support for WASI targets compiled with the atomics target feature. A prior refactoring (ba462864f11) unified WASI into the shared unix.rs thread module but failed to migrate the WASI+atomics implementation of available_parallelism(), causing it to silently fall through to the unsupported catch-all.
Changes:
- Adds a dedicated
cfg_selectarm forall(target_os = "wasi", target_feature = "atomics")inavailable_parallelism()that callslibc::sysconfwith a hardcoded constant_SC_NPROCESSORS_ONLN = 84(since thelibccrate does not export this constant for WASI targets). - Plain wasip1 without threads continues to fall through to the
_arm and return an unsupported error, matching pre-regression behavior.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
6ca9426 to
0fcb2b0
Compare
|
Given that |
|
Personally I think that this is a reasonable implementation, although I think it'd be best to lump this in with the implementation for For the correctness of this function for single-threaded wasm 1 is the correct answer, and otherwise the only other target would be for |
a41f6b5 to
00f08b0
Compare
Yes, I have opened PR rust-lang/libc#5023 for all
I have now changed it to always return |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
library/std/src/sys/thread/unix.rs
Outdated
| all(target_os = "wasi", not(target_feature = "atomics")) => { | ||
| // Single-threaded WASM always has exactly one thread of execution. | ||
| Ok(NonZero::new(1).unwrap()) | ||
| } |
| #[cfg_attr( | ||
| any( | ||
| target_os = "redox", | ||
| target_os = "l4re", | ||
| target_env = "sgx", | ||
| target_os = "solid_asp3", | ||
| target_os = "teeos", | ||
| target_os = "wasi" | ||
| ), | ||
| should_panic | ||
| )] |
I'd recommend always delegating to wasi-libc, and to avoid blocking on a libc release I think it'd be reasonable to inline the definition of the |
00f08b0 to
bc12037
Compare
This comment has been minimized.
This comment has been minimized.
|
@rustbot blocked |
This comment has been minimized.
This comment has been minimized.
… r=Mark-Simulacrum Update libc to v0.2.184 This includes the WASI _SC_* sysconf constants needed for `thread::available_parallelism` on WASI targets (rust-lang#153604).
… r=Mark-Simulacrum Update libc to v0.2.184 This includes the WASI _SC_* sysconf constants needed for `thread::available_parallelism` on WASI targets (rust-lang#153604).
|
@rustbot ready |
The refactoring in ba46286 ("std: Use more unix.rs code on WASI targets") moved WASI from its own thread module into the shared unix.rs module. However, it did not carry over the available_parallelism() implementation for WASI, causing it to fall through to the unsupported catch-all. This silently regressed the support originally added in f0b7008. Fix this by adding WASI to the sysconf-based cfg_select arm alongside other platforms that use libc::sysconf(_SC_NPROCESSORS_ONLN). This delegates to wasi-libc, which currently always returns 1 but opens up the possibility for wasi-libc to report actual processor counts in the future. This requires libc to export _SC_NPROCESSORS_ONLN for WASI targets, which has been added in libc 0.2.184.
bc12037 to
b5ddfd5
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
I have included the necessary constants in the updated libc. Hence this now follows the standard UNIX path. |
|
@bors: r+ Thanks! |
|
Unknown command ":". Run |
|
@bors r+ |
…lelism-fix, r=alexcrichton Fix thread::available_parallelism on WASI targets with threads The refactoring in ba46286 ("std: Use more unix.rs code on WASI targets") moved WASI from its own thread module into the shared unix.rs module. However, it did not carry over the available_parallelism() implementation for WASI with threads, causing it to fall through to the unsupported catch-all. This silently regressed the support originally added in f0b7008. Fix this by adding WASI to the standard UNIX cfg_select branch. Depends on rust-lang#155057 (Update libc to v0.2.184).
…uwer Rollup of 19 pull requests Successful merges: - #155162 (relnotes for 1.95) - #140763 (Change codegen of LLVM intrinsics to be name-based, and add llvm linkage support for `bf16(xN)` and `i1xN`) - #153604 (Fix thread::available_parallelism on WASI targets with threads) - #154193 (Implement EII for statics) - #154389 (Add more robust handling of nested query cycles) - #154435 (resolve: Some import resolution cleanups) - #155236 (Normalize individual predicate of `InstantiatedPredicates` inside `predicates_for_generics`) - #155243 (cg_ssa: transmute between scalable vectors) - #153941 (tests/debuginfo/basic-stepping.rs: Explain why all lines are not steppable) - #154587 (Add --verbose-run-make-subprocess-output flag to suppress verbose run-make output for passing tests) - #154624 (Make `DerefPure` dyn-incompatible) - #154929 (Add `const Default` impls for `LazyCell` and `LazyLock`) - #154944 (Small refactor of `arena_cache` query values) - #155055 (UI automation) - #155062 (Move tests from `tests/ui/issues/` to appropriate directories) - #155131 (Stabilize feature `uint_bit_width`) - #155147 (Stabilize feature `int_lowest_highest_one`) - #155174 (Improve emission of `UnknownDiagnosticAttribute` lint) - #155194 (Fix manpage version replacement and use verbose version)
Rollup merge of #153604 - rust-wasi-web:wasi-available-parallelism-fix, r=alexcrichton Fix thread::available_parallelism on WASI targets with threads The refactoring in ba46286 ("std: Use more unix.rs code on WASI targets") moved WASI from its own thread module into the shared unix.rs module. However, it did not carry over the available_parallelism() implementation for WASI with threads, causing it to fall through to the unsupported catch-all. This silently regressed the support originally added in f0b7008. Fix this by adding WASI to the standard UNIX cfg_select branch. Depends on #155057 (Update libc to v0.2.184).
…uwer Rollup of 19 pull requests Successful merges: - rust-lang/rust#155162 (relnotes for 1.95) - rust-lang/rust#140763 (Change codegen of LLVM intrinsics to be name-based, and add llvm linkage support for `bf16(xN)` and `i1xN`) - rust-lang/rust#153604 (Fix thread::available_parallelism on WASI targets with threads) - rust-lang/rust#154193 (Implement EII for statics) - rust-lang/rust#154389 (Add more robust handling of nested query cycles) - rust-lang/rust#154435 (resolve: Some import resolution cleanups) - rust-lang/rust#155236 (Normalize individual predicate of `InstantiatedPredicates` inside `predicates_for_generics`) - rust-lang/rust#155243 (cg_ssa: transmute between scalable vectors) - rust-lang/rust#153941 (tests/debuginfo/basic-stepping.rs: Explain why all lines are not steppable) - rust-lang/rust#154587 (Add --verbose-run-make-subprocess-output flag to suppress verbose run-make output for passing tests) - rust-lang/rust#154624 (Make `DerefPure` dyn-incompatible) - rust-lang/rust#154929 (Add `const Default` impls for `LazyCell` and `LazyLock`) - rust-lang/rust#154944 (Small refactor of `arena_cache` query values) - rust-lang/rust#155055 (UI automation) - rust-lang/rust#155062 (Move tests from `tests/ui/issues/` to appropriate directories) - rust-lang/rust#155131 (Stabilize feature `uint_bit_width`) - rust-lang/rust#155147 (Stabilize feature `int_lowest_highest_one`) - rust-lang/rust#155174 (Improve emission of `UnknownDiagnosticAttribute` lint) - rust-lang/rust#155194 (Fix manpage version replacement and use verbose version)
The refactoring in ba46286 ("std: Use more unix.rs code on WASI targets") moved WASI from its own thread module into the shared unix.rs module. However, it did not carry over the available_parallelism() implementation for WASI with threads, causing it to fall through to the unsupported catch-all. This silently regressed the support originally added in f0b7008.
Fix this by adding WASI to the standard UNIX cfg_select branch.
Depends on #155057 (Update libc to v0.2.184).