Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WasiCtxBuilder setters for the two clock types #6669

Merged
merged 4 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions crates/wasi/src/preview2/clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,3 @@ pub trait WasiMonotonicClock: Send + Sync {
fn resolution(&self) -> u64;
fn now(&self) -> u64;
}

pub struct WasiClocks {
pub wall: Box<dyn WasiWallClock + Send + Sync>,
pub monotonic: Box<dyn WasiMonotonicClock + Send + Sync>,
}
12 changes: 6 additions & 6 deletions crates/wasi/src/preview2/clocks/host.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{WasiClocks, WasiMonotonicClock, WasiWallClock};
use super::{WasiMonotonicClock, WasiWallClock};
use cap_std::time::{Duration, Instant, SystemClock};
use cap_std::{ambient_authority, AmbientAuthority};
use cap_time_ext::{MonotonicClockExt, SystemClockExt};
Expand Down Expand Up @@ -64,10 +64,10 @@ impl WasiMonotonicClock for MonotonicClock {
}
}

pub fn clocks_ctx() -> WasiClocks {
// Create the per-instance clock resources.
let monotonic = Box::new(MonotonicClock::new(ambient_authority()));
let wall = Box::new(WallClock::new(ambient_authority()));
pub fn monotonic_clock_ctx() -> Box<dyn WasiMonotonicClock + Send + Sync> {
Box::new(MonotonicClock::new(ambient_authority()))
}

WasiClocks { monotonic, wall }
pub fn wall_clock_ctx() -> Box<dyn WasiWallClock + Send + Sync> {
Box::new(WallClock::new(ambient_authority()))
}
29 changes: 21 additions & 8 deletions crates/wasi/src/preview2/ctx.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::preview2::{
clocks::{self, WasiClocks},
clocks::{self, WasiMonotonicClock, WasiWallClock},
filesystem::{Dir, TableFsExt},
pipe, random, stdio,
stream::{InputStream, OutputStream, TableStreamExt},
DirPerms, FilePerms, Table,
};
use cap_rand::{Rng, RngCore, SeedableRng};

use super::clocks::host::{monotonic_clock_ctx, wall_clock_ctx};

pub struct WasiCtxBuilder {
stdin: Box<dyn InputStream>,
stdout: Box<dyn OutputStream>,
Expand All @@ -18,7 +20,8 @@ pub struct WasiCtxBuilder {
random: Box<dyn RngCore + Send + Sync>,
insecure_random: Box<dyn RngCore + Send + Sync>,
insecure_random_seed: u128,
clocks: WasiClocks,
wall_clock: Box<dyn WasiWallClock + Send + Sync>,
monotonic_clock: Box<dyn WasiMonotonicClock + Send + Sync>,
}

impl WasiCtxBuilder {
Expand All @@ -43,7 +46,8 @@ impl WasiCtxBuilder {
random: random::thread_rng(),
insecure_random,
insecure_random_seed,
clocks: clocks::host::clocks_ctx(),
wall_clock: wall_clock_ctx(),
monotonic_clock: monotonic_clock_ctx(),
}
}

Expand Down Expand Up @@ -155,8 +159,14 @@ impl WasiCtxBuilder {
self.insecure_random_seed = insecure_random_seed;
self
}
pub fn set_clocks(mut self, clocks: WasiClocks) -> Self {
self.clocks = clocks;

pub fn set_wall_clock(mut self, clock: impl clocks::WasiWallClock + 'static) -> Self {
self.wall_clock = Box::new(clock);
self
}

pub fn set_monotonic_clock(mut self, clock: impl clocks::WasiMonotonicClock + 'static) -> Self {
self.monotonic_clock = Box::new(clock);
self
}

Expand All @@ -172,7 +182,8 @@ impl WasiCtxBuilder {
random,
insecure_random,
insecure_random_seed,
clocks,
wall_clock,
monotonic_clock,
} = self;

let stdin = table.push_input_stream(stdin).context("stdin")?;
Expand All @@ -199,7 +210,8 @@ impl WasiCtxBuilder {
random,
insecure_random,
insecure_random_seed,
clocks,
wall_clock,
monotonic_clock,
})
}
}
Expand All @@ -215,7 +227,8 @@ pub struct WasiCtx {
pub(crate) random: Box<dyn RngCore + Send + Sync>,
pub(crate) insecure_random: Box<dyn RngCore + Send + Sync>,
pub(crate) insecure_random_seed: u128,
pub(crate) clocks: WasiClocks,
pub(crate) wall_clock: Box<dyn WasiWallClock + Send + Sync>,
pub(crate) monotonic_clock: Box<dyn WasiMonotonicClock + Send + Sync>,
pub(crate) env: Vec<(String, String)>,
pub(crate) args: Vec<String>,
pub(crate) preopens: Vec<(u32, String)>,
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi/src/preview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub mod wasi;

pub use cap_fs_ext::SystemTimeSpec;
pub use cap_rand::RngCore;
pub use clocks::{WasiClocks, WasiMonotonicClock, WasiWallClock};
pub use clocks::{WasiMonotonicClock, WasiWallClock};
pub use ctx::{WasiCtx, WasiCtxBuilder, WasiView};
pub use error::I32Exit;
pub use filesystem::{DirPerms, FilePerms};
Expand Down
8 changes: 4 additions & 4 deletions crates/wasi/src/preview2/preview2/clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ impl TryFrom<SystemTime> for Datetime {
#[async_trait::async_trait]
impl<T: WasiView> wall_clock::Host for T {
async fn now(&mut self) -> anyhow::Result<Datetime> {
let now = self.ctx().clocks.wall.now();
let now = self.ctx().wall_clock.now();
Ok(Datetime {
seconds: now.as_secs(),
nanoseconds: now.subsec_nanos(),
})
}

async fn resolution(&mut self) -> anyhow::Result<Datetime> {
let res = self.ctx().clocks.wall.resolution();
let res = self.ctx().wall_clock.resolution();
Ok(Datetime {
seconds: res.as_secs(),
nanoseconds: res.subsec_nanos(),
Expand All @@ -46,11 +46,11 @@ impl<T: WasiView> wall_clock::Host for T {
#[async_trait::async_trait]
impl<T: WasiView> monotonic_clock::Host for T {
async fn now(&mut self) -> anyhow::Result<Instant> {
Ok(self.ctx().clocks.monotonic.now())
Ok(self.ctx().monotonic_clock.now())
}

async fn resolution(&mut self) -> anyhow::Result<Instant> {
Ok(self.ctx().clocks.monotonic.resolution())
Ok(self.ctx().monotonic_clock.resolution())
}

async fn subscribe(&mut self, when: Instant, absolute: bool) -> anyhow::Result<Pollable> {
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi/src/preview2/preview2/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<T: WasiView> poll::Host for T {
}
PollableEntry::MonotonicClock(when, absolute) => {
poll.subscribe_monotonic_clock(
&*self.ctx().clocks.monotonic,
&*self.ctx().monotonic_clock,
when,
absolute,
userdata,
Expand Down