port subscriptions in from old branch

This commit is contained in:
Pat Hickey
2021-01-12 15:55:10 -08:00
parent 0e42c2e1d9
commit b79bdcee84
3 changed files with 148 additions and 4 deletions

View File

@@ -1,8 +1,15 @@
use crate::file::WasiFile;
use crate::Error;
use cap_std::time::{SystemClock, SystemTime};
use std::cell::Ref;
pub mod subscription;
use subscription::{
RwSubscription, Subscription, SubscriptionResult, SubscriptionSet, TimerSubscription,
};
pub trait WasiSched {
// XXX poll oneoff needs args and results.
fn poll_oneoff(&self) -> Result<(), Error>;
fn poll_oneoff(&self, subs: SubscriptionSet) -> Result<(), Error>;
fn sched_yield(&self) -> Result<(), Error>;
}
@@ -10,7 +17,7 @@ pub trait WasiSched {
pub struct SyncSched {}
impl WasiSched for SyncSched {
fn poll_oneoff(&self) -> Result<(), Error> {
fn poll_oneoff(&self, subs: SubscriptionSet) -> Result<(), Error> {
todo!()
}
fn sched_yield(&self) -> Result<(), Error> {
@@ -18,3 +25,48 @@ impl WasiSched for SyncSched {
Ok(())
}
}
pub struct Userdata(u64);
impl From<u64> for Userdata {
fn from(u: u64) -> Userdata {
Userdata(u)
}
}
impl From<Userdata> for u64 {
fn from(u: Userdata) -> u64 {
u.0
}
}
pub struct Poll<'a> {
subs: Vec<(Subscription<'a>, Userdata)>,
}
impl<'a> Poll<'a> {
pub fn new() -> Self {
Self { subs: Vec::new() }
}
pub fn subscribe_timer(&mut self, deadline: SystemTime, ud: Userdata) {
self.subs
.push((Subscription::Timer(TimerSubscription { deadline }), ud));
}
pub fn subscribe_read(&mut self, file: Ref<'a, dyn WasiFile>, ud: Userdata) {
self.subs
.push((Subscription::Read(RwSubscription::new(file)), ud));
}
pub fn subscribe_write(&mut self, file: Ref<'a, dyn WasiFile>, ud: Userdata) {
self.subs
.push((Subscription::Read(RwSubscription::new(file)), ud));
}
pub fn results(self, clock: &SystemClock) -> Vec<(SubscriptionResult, Userdata)> {
self.subs
.into_iter()
.filter_map(|(s, ud)| SubscriptionResult::from_subscription(s, clock).map(|r| (r, ud)))
.collect()
}
pub(crate) fn subscriptions(&'a mut self) -> SubscriptionSet<'a> {
SubscriptionSet {
subs: self.subs.iter().map(|(s, _ud)| s).collect(),
}
}
}