wasi-common: WasiSched is an async trait now; poll_oneoff and sched_yield are async methods
This commit is contained in:
@@ -1,16 +1,18 @@
|
|||||||
use crate::clocks::WasiMonotonicClock;
|
use crate::clocks::WasiMonotonicClock;
|
||||||
use crate::file::WasiFile;
|
use crate::file::WasiFile;
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
use cap_std::time::{Duration, Instant};
|
use cap_std::time::Instant;
|
||||||
use std::cell::Ref;
|
use std::cell::Ref;
|
||||||
pub mod subscription;
|
pub mod subscription;
|
||||||
|
pub use cap_std::time::Duration;
|
||||||
|
|
||||||
use subscription::{MonotonicClockSubscription, RwSubscription, Subscription, SubscriptionResult};
|
use subscription::{MonotonicClockSubscription, RwSubscription, Subscription, SubscriptionResult};
|
||||||
|
|
||||||
|
#[wiggle::async_trait]
|
||||||
pub trait WasiSched {
|
pub trait WasiSched {
|
||||||
fn poll_oneoff(&self, poll: &Poll) -> Result<(), Error>;
|
async fn poll_oneoff<'a>(&self, poll: &Poll<'a>) -> Result<(), Error>;
|
||||||
fn sched_yield(&self) -> Result<(), Error>;
|
async fn sched_yield(&self) -> Result<(), Error>;
|
||||||
fn sleep(&self, duration: Duration) -> Result<(), Error>;
|
async fn sleep(&self, duration: Duration) -> Result<(), Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Userdata(u64);
|
pub struct Userdata(u64);
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ use wiggle::GuestPtr;
|
|||||||
wiggle::from_witx!({
|
wiggle::from_witx!({
|
||||||
witx: ["$WASI_ROOT/phases/old/snapshot_0/witx/wasi_unstable.witx"],
|
witx: ["$WASI_ROOT/phases/old/snapshot_0/witx/wasi_unstable.witx"],
|
||||||
errors: { errno => Error },
|
errors: { errno => Error },
|
||||||
|
async: { wasi_unstable::{poll_oneoff, sched_yield} }
|
||||||
});
|
});
|
||||||
|
|
||||||
impl wiggle::GuestErrorType for types::Errno {
|
impl wiggle::GuestErrorType for types::Errno {
|
||||||
@@ -332,6 +333,7 @@ convert_flags_bidirectional!(
|
|||||||
|
|
||||||
// This implementation, wherever possible, delegates directly to the Snapshot1 implementation,
|
// This implementation, wherever possible, delegates directly to the Snapshot1 implementation,
|
||||||
// performing the no-op type conversions along the way.
|
// performing the no-op type conversions along the way.
|
||||||
|
#[wiggle::async_trait]
|
||||||
impl<'a> wasi_unstable::WasiUnstable for WasiCtx {
|
impl<'a> wasi_unstable::WasiUnstable for WasiCtx {
|
||||||
fn args_get<'b>(
|
fn args_get<'b>(
|
||||||
&self,
|
&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 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
|
// The bodies of these functions is mostly about converting the GuestPtr and types::-based
|
||||||
// representation to use the Poll abstraction.
|
// representation to use the Poll abstraction.
|
||||||
fn poll_oneoff(
|
async fn poll_oneoff<'b>(
|
||||||
&self,
|
&self,
|
||||||
subs: &GuestPtr<types::Subscription>,
|
subs: &GuestPtr<'b, types::Subscription>,
|
||||||
events: &GuestPtr<types::Event>,
|
events: &GuestPtr<'b, types::Event>,
|
||||||
nsubscriptions: types::Size,
|
nsubscriptions: types::Size,
|
||||||
) -> Result<types::Size, Error> {
|
) -> Result<types::Size, Error> {
|
||||||
if nsubscriptions == 0 {
|
if nsubscriptions == 0 {
|
||||||
@@ -741,7 +743,9 @@ impl<'a> wasi_unstable::WasiUnstable for WasiCtx {
|
|||||||
.flags
|
.flags
|
||||||
.contains(types::Subclockflags::SUBSCRIPTION_CLOCK_ABSTIME)
|
.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 {
|
events.write(types::Event {
|
||||||
userdata: sub.userdata,
|
userdata: sub.userdata,
|
||||||
error: types::Errno::Success,
|
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 results = poll.results();
|
||||||
let num_results = results.len();
|
let num_results = results.len();
|
||||||
@@ -890,8 +894,8 @@ impl<'a> wasi_unstable::WasiUnstable for WasiCtx {
|
|||||||
Err(Error::trap("proc_raise unsupported"))
|
Err(Error::trap("proc_raise unsupported"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sched_yield(&self) -> Result<(), Error> {
|
async fn sched_yield(&self) -> Result<(), Error> {
|
||||||
Snapshot1::sched_yield(self)
|
Snapshot1::sched_yield(self).await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn random_get(&self, buf: &GuestPtr<u8>, buf_len: types::Size) -> Result<(), Error> {
|
fn random_get(&self, buf: &GuestPtr<u8>, buf_len: types::Size) -> Result<(), Error> {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ use wiggle::GuestPtr;
|
|||||||
wiggle::from_witx!({
|
wiggle::from_witx!({
|
||||||
witx: ["$WASI_ROOT/phases/snapshot/witx/wasi_snapshot_preview1.witx"],
|
witx: ["$WASI_ROOT/phases/snapshot/witx/wasi_snapshot_preview1.witx"],
|
||||||
errors: { errno => Error },
|
errors: { errno => Error },
|
||||||
|
async: { wasi_snapshot_preview1::{poll_oneoff, sched_yield} }
|
||||||
});
|
});
|
||||||
|
|
||||||
impl wiggle::GuestErrorType for types::Errno {
|
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>(
|
fn args_get<'b>(
|
||||||
&self,
|
&self,
|
||||||
argv: &GuestPtr<'b, GuestPtr<'b, u8>>,
|
argv: &GuestPtr<'b, GuestPtr<'b, u8>>,
|
||||||
@@ -891,10 +893,10 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
|||||||
.unlink_file(path.as_str()?.deref())
|
.unlink_file(path.as_str()?.deref())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_oneoff(
|
async fn poll_oneoff<'b>(
|
||||||
&self,
|
&self,
|
||||||
subs: &GuestPtr<types::Subscription>,
|
subs: &GuestPtr<'b, types::Subscription>,
|
||||||
events: &GuestPtr<types::Event>,
|
events: &GuestPtr<'b, types::Event>,
|
||||||
nsubscriptions: types::Size,
|
nsubscriptions: types::Size,
|
||||||
) -> Result<types::Size, Error> {
|
) -> Result<types::Size, Error> {
|
||||||
if nsubscriptions == 0 {
|
if nsubscriptions == 0 {
|
||||||
@@ -912,7 +914,9 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
|||||||
.flags
|
.flags
|
||||||
.contains(types::Subclockflags::SUBSCRIPTION_CLOCK_ABSTIME)
|
.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 {
|
events.write(types::Event {
|
||||||
userdata: sub.userdata,
|
userdata: sub.userdata,
|
||||||
error: types::Errno::Success,
|
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 results = poll.results();
|
||||||
let num_results = results.len();
|
let num_results = results.len();
|
||||||
@@ -1066,8 +1070,8 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
|||||||
Err(Error::trap("proc_raise unsupported"))
|
Err(Error::trap("proc_raise unsupported"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sched_yield(&self) -> Result<(), Error> {
|
async fn sched_yield(&self) -> Result<(), Error> {
|
||||||
self.sched.sched_yield()
|
self.sched.sched_yield().await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn random_get(&self, buf: &GuestPtr<u8>, buf_len: types::Size) -> Result<(), Error> {
|
fn random_get(&self, buf: &GuestPtr<u8>, buf_len: types::Size) -> Result<(), Error> {
|
||||||
|
|||||||
Reference in New Issue
Block a user