diff --git a/crates/wasi-common/src/sched.rs b/crates/wasi-common/src/sched.rs index 10d12d6621..3434ef62dc 100644 --- a/crates/wasi-common/src/sched.rs +++ b/crates/wasi-common/src/sched.rs @@ -1,16 +1,18 @@ use crate::clocks::WasiMonotonicClock; use crate::file::WasiFile; use crate::Error; -use cap_std::time::{Duration, Instant}; +use cap_std::time::Instant; use std::cell::Ref; pub mod subscription; +pub use cap_std::time::Duration; use subscription::{MonotonicClockSubscription, RwSubscription, Subscription, SubscriptionResult}; +#[wiggle::async_trait] pub trait WasiSched { - fn poll_oneoff(&self, poll: &Poll) -> Result<(), Error>; - fn sched_yield(&self) -> Result<(), Error>; - fn sleep(&self, duration: Duration) -> Result<(), Error>; + async fn poll_oneoff<'a>(&self, poll: &Poll<'a>) -> Result<(), Error>; + async fn sched_yield(&self) -> Result<(), Error>; + async fn sleep(&self, duration: Duration) -> Result<(), Error>; } pub struct Userdata(u64); diff --git a/crates/wasi-common/src/snapshots/preview_0.rs b/crates/wasi-common/src/snapshots/preview_0.rs index 02e6289cd2..477f6648b2 100644 --- a/crates/wasi-common/src/snapshots/preview_0.rs +++ b/crates/wasi-common/src/snapshots/preview_0.rs @@ -16,6 +16,7 @@ use wiggle::GuestPtr; wiggle::from_witx!({ witx: ["$WASI_ROOT/phases/old/snapshot_0/witx/wasi_unstable.witx"], errors: { errno => Error }, + async: { wasi_unstable::{poll_oneoff, sched_yield} } }); impl wiggle::GuestErrorType for types::Errno { @@ -332,6 +333,7 @@ convert_flags_bidirectional!( // This implementation, wherever possible, delegates directly to the Snapshot1 implementation, // performing the no-op type conversions along the way. +#[wiggle::async_trait] impl<'a> wasi_unstable::WasiUnstable for WasiCtx { fn args_get<'b>( &self, @@ -720,10 +722,10 @@ impl<'a> wasi_unstable::WasiUnstable for WasiCtx { // The implementations are identical, but the `types::` in scope locally is different. // The bodies of these functions is mostly about converting the GuestPtr and types::-based // representation to use the Poll abstraction. - fn poll_oneoff( + async fn poll_oneoff<'b>( &self, - subs: &GuestPtr, - events: &GuestPtr, + subs: &GuestPtr<'b, types::Subscription>, + events: &GuestPtr<'b, types::Event>, nsubscriptions: types::Size, ) -> Result { if nsubscriptions == 0 { @@ -741,7 +743,9 @@ impl<'a> wasi_unstable::WasiUnstable for WasiCtx { .flags .contains(types::Subclockflags::SUBSCRIPTION_CLOCK_ABSTIME) { - self.sched.sleep(Duration::from_nanos(clocksub.timeout))?; + self.sched + .sleep(Duration::from_nanos(clocksub.timeout)) + .await?; events.write(types::Event { userdata: sub.userdata, error: types::Errno::Success, @@ -807,7 +811,7 @@ impl<'a> wasi_unstable::WasiUnstable for WasiCtx { } } - self.sched.poll_oneoff(&poll)?; + self.sched.poll_oneoff(&poll).await?; let results = poll.results(); let num_results = results.len(); @@ -890,8 +894,8 @@ impl<'a> wasi_unstable::WasiUnstable for WasiCtx { Err(Error::trap("proc_raise unsupported")) } - fn sched_yield(&self) -> Result<(), Error> { - Snapshot1::sched_yield(self) + async fn sched_yield(&self) -> Result<(), Error> { + Snapshot1::sched_yield(self).await } fn random_get(&self, buf: &GuestPtr, buf_len: types::Size) -> Result<(), Error> { diff --git a/crates/wasi-common/src/snapshots/preview_1.rs b/crates/wasi-common/src/snapshots/preview_1.rs index 0ee4044c37..7896189542 100644 --- a/crates/wasi-common/src/snapshots/preview_1.rs +++ b/crates/wasi-common/src/snapshots/preview_1.rs @@ -22,6 +22,7 @@ use wiggle::GuestPtr; wiggle::from_witx!({ witx: ["$WASI_ROOT/phases/snapshot/witx/wasi_snapshot_preview1.witx"], errors: { errno => Error }, + async: { wasi_snapshot_preview1::{poll_oneoff, sched_yield} } }); impl wiggle::GuestErrorType for types::Errno { @@ -188,7 +189,8 @@ impl TryFrom for types::Errno { } } -impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { +#[wiggle::async_trait] +impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { fn args_get<'b>( &self, argv: &GuestPtr<'b, GuestPtr<'b, u8>>, @@ -891,10 +893,10 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { .unlink_file(path.as_str()?.deref()) } - fn poll_oneoff( + async fn poll_oneoff<'b>( &self, - subs: &GuestPtr, - events: &GuestPtr, + subs: &GuestPtr<'b, types::Subscription>, + events: &GuestPtr<'b, types::Event>, nsubscriptions: types::Size, ) -> Result { if nsubscriptions == 0 { @@ -912,7 +914,9 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { .flags .contains(types::Subclockflags::SUBSCRIPTION_CLOCK_ABSTIME) { - self.sched.sleep(Duration::from_nanos(clocksub.timeout))?; + self.sched + .sleep(Duration::from_nanos(clocksub.timeout)) + .await?; events.write(types::Event { userdata: sub.userdata, error: types::Errno::Success, @@ -978,7 +982,7 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { } } - self.sched.poll_oneoff(&poll)?; + self.sched.poll_oneoff(&poll).await?; let results = poll.results(); let num_results = results.len(); @@ -1066,8 +1070,8 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { Err(Error::trap("proc_raise unsupported")) } - fn sched_yield(&self) -> Result<(), Error> { - self.sched.sched_yield() + async fn sched_yield(&self) -> Result<(), Error> { + self.sched.sched_yield().await } fn random_get(&self, buf: &GuestPtr, buf_len: types::Size) -> Result<(), Error> {