Add a sleep function to the WasiSched trait.
This commit is contained in:
@@ -108,6 +108,9 @@ impl WasiSched for SyncSched {
|
||||
std::thread::yield_now();
|
||||
Ok(())
|
||||
}
|
||||
fn sleep(&self, duration: Duration) {
|
||||
std::thread::sleep(duration)
|
||||
}
|
||||
}
|
||||
|
||||
fn wasi_file_raw_fd(f: &dyn WasiFile) -> Option<RawFd> {
|
||||
|
||||
@@ -128,6 +128,9 @@ impl WasiSched for SyncSched {
|
||||
thread::yield_now();
|
||||
Ok(())
|
||||
}
|
||||
fn sleep(&self, duration: Duration) {
|
||||
std::thread::sleep(duration)
|
||||
}
|
||||
}
|
||||
|
||||
fn wasi_file_raw_handle(f: &dyn WasiFile) -> Option<RawHandle> {
|
||||
|
||||
@@ -10,6 +10,7 @@ use subscription::{MonotonicClockSubscription, RwSubscription, Subscription, Sub
|
||||
pub trait WasiSched {
|
||||
fn poll_oneoff(&self, poll: &Poll) -> Result<(), Error>;
|
||||
fn sched_yield(&self) -> Result<(), Error>;
|
||||
fn sleep(&self, duration: Duration);
|
||||
}
|
||||
|
||||
pub struct Userdata(u64);
|
||||
|
||||
@@ -738,6 +738,30 @@ impl<'a> wasi_unstable::WasiUnstable for WasiCtx {
|
||||
return Err(Error::invalid_argument().context("nsubscriptions must be nonzero"));
|
||||
}
|
||||
|
||||
// Special-case a `poll_oneoff` which is just sleeping on a single
|
||||
// relative timer event, such as what WASI libc uses to implement sleep
|
||||
// functions. This supports all clock IDs, because POSIX says that
|
||||
// `clock_settime` doesn't effect relative sleeps.
|
||||
if nsubscriptions == 1 {
|
||||
let sub = subs.read()?;
|
||||
if let types::SubscriptionU::Clock(clocksub) = sub.u {
|
||||
if !clocksub
|
||||
.flags
|
||||
.contains(types::Subclockflags::SUBSCRIPTION_CLOCK_ABSTIME)
|
||||
{
|
||||
self.sched.sleep(Duration::from_nanos(clocksub.timeout));
|
||||
|
||||
events.write(types::Event {
|
||||
userdata: sub.userdata,
|
||||
error: types::Errno::Success,
|
||||
type_: types::Eventtype::Clock,
|
||||
fd_readwrite: fd_readwrite_empty(),
|
||||
})?;
|
||||
return Ok(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let table = self.table();
|
||||
let mut poll = Poll::new();
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@ use std::cell::{Ref, RefMut};
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use std::io::{IoSlice, IoSliceMut};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::thread;
|
||||
use tracing::debug;
|
||||
use wiggle::GuestPtr;
|
||||
|
||||
@@ -921,7 +920,7 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
.flags
|
||||
.contains(types::Subclockflags::SUBSCRIPTION_CLOCK_ABSTIME)
|
||||
{
|
||||
thread::sleep(Duration::from_nanos(clocksub.timeout));
|
||||
self.sched.sleep(Duration::from_nanos(clocksub.timeout));
|
||||
|
||||
events.write(types::Event {
|
||||
userdata: sub.userdata,
|
||||
|
||||
Reference in New Issue
Block a user