Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion compiler/rustc_target/src/spec/targets/wasm32_wasip3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! all component-model-level imports anyway. Over time the imports of the
//! standard library will change to WASIp3.

use crate::spec::{Env, Target};
use crate::spec::{Cc, Env, LinkerFlavor, Target, add_link_args};

pub(crate) fn target() -> Target {
// As of now WASIp3 is a lightly edited wasip2 target, so start with that
Expand All @@ -22,5 +22,19 @@ pub(crate) fn target() -> Target {
std: Some(true),
};
target.options.env = Env::P3;

// The `--cooperative-threading` flag to the linker dictates the ABI that's
// being used on this target which is to store the stack pointer in a
// component model intrinsic location, for example, rather than a wasm
// global.
//
// Note that this is only specified for `Cc::No`, because when `clang` is
// being used as a linker it'll already pass this.
add_link_args(
&mut target.pre_link_args,
LinkerFlavor::WasmLld(Cc::No),
&["--cooperative-threading"],
);

target
}
2 changes: 1 addition & 1 deletion library/std/src/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ cfg_select! {
pub mod linux;
}
target_family = "wasm" => {
#[cfg(any(target_env = "p1", target_env = "p2"))]
#[cfg(any(target_env = "p1", target_env = "p2", target_env = "p3"))]
pub mod wasi;
#[cfg(target_env = "p2")]
pub mod wasip2;
Expand Down
21 changes: 18 additions & 3 deletions library/std/src/sys/pal/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@
use crate::io;

pub mod conf;
#[allow(unused)]
#[path = "../wasm/atomics/futex.rs"]
pub mod futex;
pub mod stack_overflow;
#[path = "../unix/time.rs"]
pub mod time;

// The wasi-libc based futex is new enough that it's not present in older
// wasi-libc builds. For now that means it's only required on wasip3 (which
// requires a newer wasi-libc anyway). In the future this'll probably switch to
// unconditionally using `wasilibc_futex` as the implementation for all WASI
// targets (and switching all synchronization primitives to the futex version).
cfg_select! {
target_env = "p3" => {
pub mod wasilibc_futex;
pub use wasilibc_futex as futex;
}
target_feature = "atomics" => {
#[allow(unused)]
#[path = "../wasm/atomics/futex.rs"]
pub mod futex;
}
_ => {}
}

#[cfg(not(target_env = "p1"))]
mod cabi_realloc;

Expand Down
68 changes: 68 additions & 0 deletions library/std/src/sys/pal/wasi/wasilibc_futex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! A futex implementation based on the primitives provided by `wasi-libc`.
//!
//! This is currently only used on wasip3 targets, but in the future once
//! `wasi-libc`'s implementation of these symbols have percolated further it'll
//! be possible to use this on all WASI targets. The `wasi-libc` implementation
//! of these symbols differs depending on the target and configuration:
//!
//! * `wasm32-wasip{1,2}` - this will abort if blocking actually happens
//! * `wasm32-wasip1-threads` - this uses wasm `memory.atomic.*` instructions
//! * `wasm32-wasip3` - depending on libc configuration (`ENABLE_COOP_THREADS`)
//! this either aborts (coop threads disables) on blocking or does the
//! coop-thread-thing to manage threads.
//!
//! Regardless this module is effectively delegating to `wasi-libc` to determine
//! how to do thread management.

use libc::c_int;

use crate::ptr;
use crate::sync::atomic::Atomic;
use crate::time::Duration;

const __WASILIBC_FUTEX_WAKE_ALL: c_int = -1;

unsafe extern "C" {
fn __wasilibc_futex_wait(
addr: *mut c_int,
val: c_int,
clock: libc::clockid_t,
at: *const libc::timespec,
flags: libc::c_uint,
) -> c_int;
fn __wasilibc_futex_wake(addr: *const c_int, count: c_int, flags: libc::c_uint) -> c_int;
}

pub type Futex = Atomic<Primitive>;
pub type Primitive = u32;

pub type SmallFutex = Atomic<SmallPrimitive>;
pub type SmallPrimitive = u32;

pub fn futex_wait(futex: &Atomic<u32>, expected: u32, timeout: Option<Duration>) -> bool {
let timespec = timeout.and_then(|t| {
Some(libc::timespec {
tv_sec: t.as_secs().try_into().ok()?,
tv_nsec: t.subsec_nanos().try_into().ok()?,
})
});
unsafe {
__wasilibc_futex_wait(
futex.as_ptr().cast(),
expected.cast_signed(),
libc::CLOCK_REALTIME,
timespec.as_ref().map(ptr::from_ref).unwrap_or(ptr::null()),
0,
) == 0
}
}

pub fn futex_wake(futex: &Atomic<u32>) -> bool {
unsafe { __wasilibc_futex_wake(futex.as_ptr().cast(), 1, 0) == 1 }
}

pub fn futex_wake_all(futex: &Atomic<u32>) {
unsafe {
__wasilibc_futex_wake(futex.as_ptr().cast(), __WASILIBC_FUTEX_WAKE_ALL, 0);
}
}
1 change: 1 addition & 0 deletions library/std/src/sys/sync/condvar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cfg_select! {
target_os = "fuchsia",
all(target_family = "wasm", target_feature = "atomics"),
target_os = "hermit",
all(target_os = "wasi", target_env = "p3"),
) => {
mod futex;
pub use futex::Condvar;
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/sync/mutex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ cfg_select! {
target_os = "dragonfly",
all(target_family = "wasm", target_feature = "atomics"),
target_os = "hermit",
all(target_os = "wasi", target_env = "p3"),
) => {
mod futex;
pub use futex::Mutex;
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/sync/once/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cfg_select! {
target_os = "dragonfly",
target_os = "fuchsia",
target_os = "hermit",
all(target_os = "wasi", target_env = "p3"),
) => {
mod futex;
pub use futex::{Once, OnceState};
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/sys/sync/rwlock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ cfg_select! {
target_os = "fuchsia",
all(target_family = "wasm", target_feature = "atomics"),
target_os = "hermit",
target_os = "motor",
target_os = "motor",
all(target_os = "wasi", target_env = "p3"),
) => {
mod futex;
pub use futex::RwLock;
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/sync/thread_parking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cfg_select! {
target_os = "fuchsia",
target_os = "motor",
target_os = "hermit",
all(target_os = "wasi", target_env = "p3"),
) => {
mod futex;
pub use futex::Parker;
Expand Down
Loading