Browse Source

add comments, make everything scheduler-related private to crate

pull/6391/head
Pat Hickey 1 year ago
parent
commit
317320e22c
  1. 14
      crates/wasi/src/preview2/ctx.rs
  2. 24
      crates/wasi/src/preview2/mod.rs
  3. 5
      crates/wasi/src/preview2/preview2/poll.rs
  4. 17
      crates/wasi/src/preview2/sched.rs
  5. 4
      crates/wasi/src/preview2/sched/sync.rs

14
crates/wasi/src/preview2/ctx.rs

@ -1,9 +1,7 @@
use crate::preview2::{
clocks::{self, WasiClocks},
filesystem::{Dir, TableFsExt},
pipe, random,
sched::{self, WasiSched},
stdio,
pipe, random, stdio,
stream::{InputStream, OutputStream, TableStreamExt},
DirPerms, FilePerms, Table,
};
@ -20,14 +18,11 @@ pub struct WasiCtxBuilder {
random: Option<Box<dyn RngCore + Send + Sync>>,
clocks: Option<WasiClocks>,
sched: Option<Box<dyn WasiSched>>,
}
impl WasiCtxBuilder {
pub fn new() -> Self {
Self::default()
.set_sched(sched::sync::SyncSched)
.set_clocks(clocks::host::clocks_ctx())
.set_random(random::thread_rng())
.set_stdin(pipe::ReadPipe::new(std::io::empty()))
@ -102,11 +97,6 @@ impl WasiCtxBuilder {
self
}
pub fn set_sched(mut self, sched: impl WasiSched + 'static) -> Self {
self.sched = Some(Box::new(sched));
self
}
pub fn build(self, table: &mut Table) -> Result<WasiCtx, anyhow::Error> {
use anyhow::Context;
@ -131,7 +121,6 @@ impl WasiCtxBuilder {
Ok(WasiCtx {
random: self.random.context("required member random")?,
clocks: self.clocks.context("required member clocks")?,
sched: self.sched.context("required member sched")?,
env: self.env,
args: self.args,
preopens,
@ -152,7 +141,6 @@ pub trait WasiView: Send {
pub struct WasiCtx {
pub random: Box<dyn RngCore + Send + Sync>,
pub clocks: WasiClocks,
pub sched: Box<dyn WasiSched>,
pub env: Vec<(String, String)>,
pub args: Vec<String>,
pub preopens: Vec<(u32, String)>,

24
crates/wasi/src/preview2/mod.rs

@ -1,3 +1,24 @@
///! # Wasmtime's WASI Preview 2 Implementation
///!
///! Welcome to the (new!) WASI implementation from the Wasmtime team. The goal
///! of this implementation is to support WASI Preview 2 via the Component
///! Model, as well as to provide legacy Preview 1 host support with an adapter
///! that is implemented in terms of the Preview 2 interfaces.
///!
///! Presently, this crate is experimental. We don't yet recommend you use it
///! in production. Specifically:
///! * it does not yet support a synchronous rust embedding
///! * polling and streams need a redesign. IO that currently shoudl be
///! non-blocking may be blocking. poll probably doesnt work at all.
///! * its internal organization could use some love
///! * the Preview 1 host support is in its infancy
///! * the wit files in tree describing preview 2 are not faithful to the
///! standards repos
///!
///! Once these issues are resolved, we expect to move this namespace up to the
///! root of the wasmtime-wasi crate, and move its other exports underneath a
///! `pub mod legacy` with an off-by-default feature flag, and after 2
///! releases, retire and remove that code from our tree.
pub mod clocks;
mod ctx;
mod error;
@ -7,7 +28,7 @@ pub mod pipe;
pub mod preview1;
pub mod preview2;
pub mod random;
pub mod sched;
mod sched;
pub mod stdio;
pub mod stream;
pub mod table;
@ -19,6 +40,5 @@ pub use clocks::{WasiClocks, WasiMonotonicClock, WasiWallClock};
pub use ctx::{WasiCtx, WasiCtxBuilder, WasiView};
pub use error::I32Exit;
pub use filesystem::{DirPerms, FilePerms};
pub use sched::{Poll, WasiSched};
pub use stream::{InputStream, OutputStream};
pub use table::{Table, TableError};

5
crates/wasi/src/preview2/preview2/poll.rs

@ -36,7 +36,7 @@ impl<T: WasiView> wasi::poll::Host for T {
}
async fn poll_oneoff(&mut self, futures: Vec<Pollable>) -> anyhow::Result<Vec<u8>> {
use crate::preview2::sched::{Poll, Userdata};
use crate::preview2::sched::{sync::SyncSched, Poll, Userdata, WasiSched};
// Convert `futures` into `Poll` subscriptions.
let mut poll = Poll::new();
@ -72,9 +72,8 @@ impl<T: WasiView> wasi::poll::Host for T {
}
}
let ctx = self.ctx();
// Do the poll.
ctx.sched.poll_oneoff(&mut poll).await?;
SyncSched.poll_oneoff(&mut poll).await?;
// Convert the results into a list of `u8` to return.
let mut results = vec![0_u8; len];

17
crates/wasi/src/preview2/sched.rs

@ -1,25 +1,26 @@
#![allow(dead_code)]
use crate::preview2::{
clocks::WasiMonotonicClock,
stream::{InputStream, OutputStream},
};
use anyhow::Error;
pub mod subscription;
pub mod sync;
pub(crate) mod subscription;
pub(crate) mod sync;
pub use cap_std::time::Duration;
pub use subscription::{
MonotonicClockSubscription, RwEventFlags, RwSubscription, Subscription, SubscriptionResult,
pub(crate) use subscription::{
MonotonicClockSubscription, RwSubscription, Subscription, SubscriptionResult,
};
#[async_trait::async_trait]
pub trait WasiSched: Send + Sync {
pub(crate) trait WasiSched: Send + Sync {
async fn poll_oneoff<'a>(&self, poll: &mut Poll<'a>) -> Result<(), Error>;
async fn sched_yield(&self) -> Result<(), Error>;
async fn sleep(&self, duration: Duration) -> Result<(), Error>;
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Userdata(u64);
pub(crate) struct Userdata(u64);
impl From<u64> for Userdata {
fn from(u: u64) -> Userdata {
Userdata(u)
@ -32,9 +33,7 @@ impl From<Userdata> for u64 {
}
}
pub type PollResults = Vec<(SubscriptionResult, Userdata)>;
pub struct Poll<'a> {
pub(crate) struct Poll<'a> {
subs: Vec<(Subscription<'a>, Userdata)>,
}

4
crates/wasi/src/preview2/sched/sync.rs

@ -8,7 +8,7 @@ use std::time::Duration;
use anyhow::Error;
pub async fn poll_oneoff<'a>(poll: &mut Poll<'a>) -> Result<(), Error> {
pub(crate) async fn poll_oneoff<'a>(poll: &mut Poll<'a>) -> Result<(), Error> {
// Collect all stream I/O subscriptions. Clock subscriptions are handled
// separately below.
let mut ready = false;
@ -139,7 +139,7 @@ pub async fn poll_oneoff<'a>(poll: &mut Poll<'a>) -> Result<(), Error> {
Ok(())
}
pub struct SyncSched;
pub(crate) struct SyncSched;
#[async_trait::async_trait]
impl WasiSched for SyncSched {
async fn poll_oneoff<'a>(&self, poll: &mut Poll<'a>) -> Result<(), Error> {

Loading…
Cancel
Save