wasi-common: WasiSched is an async trait now; poll_oneoff and sched_yield are async methods

This commit is contained in:
Pat Hickey
2021-04-12 18:55:25 -07:00
parent c62d7f9ea1
commit 68daec7e7d
3 changed files with 29 additions and 19 deletions

View File

@@ -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);

View File

@@ -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<types::Subscription>,
events: &GuestPtr<types::Event>,
subs: &GuestPtr<'b, types::Subscription>,
events: &GuestPtr<'b, types::Event>,
nsubscriptions: types::Size,
) -> Result<types::Size, Error> {
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<u8>, buf_len: types::Size) -> Result<(), Error> {

View File

@@ -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<std::io::Error> 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<types::Subscription>,
events: &GuestPtr<types::Event>,
subs: &GuestPtr<'b, types::Subscription>,
events: &GuestPtr<'b, types::Event>,
nsubscriptions: types::Size,
) -> Result<types::Size, Error> {
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<u8>, buf_len: types::Size) -> Result<(), Error> {