diff --git a/host/src/clocks.rs b/host/src/clocks.rs index f71473e440e6..b2d4c05ffac4 100644 --- a/host/src/clocks.rs +++ b/host/src/clocks.rs @@ -1,16 +1,14 @@ #![allow(unused_variables)] use crate::command::wasi::{ - instance_monotonic_clock, instance_wall_clock, - monotonic_clock::{self, Instant, MonotonicClock}, + monotonic_clock::{self, Instant}, poll::Pollable, timezone::{self, Timezone, TimezoneDisplay}, - wall_clock::{self, Datetime, WallClock}, + wall_clock::{self, Datetime}, }; use crate::poll::PollableEntry; use crate::WasiCtx; use cap_std::time::SystemTime; -use wasi_common::clocks::{TableMonotonicClockExt, TableWallClockExt}; impl TryFrom for Datetime { type Error = anyhow::Error; @@ -26,74 +24,39 @@ impl TryFrom for Datetime { } } -#[async_trait::async_trait] -impl instance_wall_clock::Host for WasiCtx { - async fn instance_wall_clock(&mut self) -> anyhow::Result { - // Create a new handle to the default wall clock. - let new = self.clocks.instance_wall_clock.dup(); - Ok(self.table_mut().push(Box::new(new))?) - } -} - -#[async_trait::async_trait] -impl instance_monotonic_clock::Host for WasiCtx { - async fn instance_monotonic_clock(&mut self) -> anyhow::Result { - // Create a new handle to the default monotonic clock. - let new = self.clocks.instance_monotonic_clock.dup(); - Ok(self.table_mut().push(Box::new(new))?) - } -} - #[async_trait::async_trait] impl wall_clock::Host for WasiCtx { - async fn now(&mut self, fd: WallClock) -> anyhow::Result { - let clock = self.table().get_wall_clock(fd)?; - let now = clock.now(); + async fn now(&mut self) -> anyhow::Result { + let now = self.clocks.wall.now(); Ok(Datetime { seconds: now.as_secs(), nanoseconds: now.subsec_nanos(), }) } - async fn resolution(&mut self, fd: WallClock) -> anyhow::Result { - let clock = self.table().get_wall_clock(fd)?; - let res = clock.resolution(); + async fn resolution(&mut self) -> anyhow::Result { + let res = self.clocks.wall.resolution(); Ok(Datetime { seconds: res.as_secs(), nanoseconds: res.subsec_nanos(), }) } - - async fn drop_wall_clock(&mut self, clock: WallClock) -> anyhow::Result<()> { - Ok(self.table_mut().delete_wall_clock(clock)?) - } } #[async_trait::async_trait] impl monotonic_clock::Host for WasiCtx { - async fn now(&mut self, fd: MonotonicClock) -> anyhow::Result { - Ok(self.table().get_monotonic_clock(fd)?.now()) + async fn now(&mut self) -> anyhow::Result { + Ok(self.clocks.monotonic.now()) } - async fn resolution(&mut self, fd: MonotonicClock) -> anyhow::Result { - Ok(self.table().get_monotonic_clock(fd)?.now()) + async fn resolution(&mut self) -> anyhow::Result { + Ok(self.clocks.monotonic.resolution()) } - async fn drop_monotonic_clock(&mut self, clock: MonotonicClock) -> anyhow::Result<()> { - Ok(self.table_mut().delete_monotonic_clock(clock)?) - } - - async fn subscribe( - &mut self, - clock: MonotonicClock, - when: Instant, - absolute: bool, - ) -> anyhow::Result { + async fn subscribe(&mut self, when: Instant, absolute: bool) -> anyhow::Result { Ok(self .table_mut() - .push(Box::new(PollableEntry::MonotonicClock( - clock, when, absolute, - )))?) + .push(Box::new(PollableEntry::MonotonicClock(when, absolute)))?) } } diff --git a/host/src/command.rs b/host/src/command.rs index 5f2c80e065b8..8d2824d4a895 100644 --- a/host/src/command.rs +++ b/host/src/command.rs @@ -20,8 +20,6 @@ pub fn add_to_linker( wasi::wall_clock::add_to_linker(l, f)?; wasi::monotonic_clock::add_to_linker(l, f)?; wasi::timezone::add_to_linker(l, f)?; - wasi::instance_monotonic_clock::add_to_linker(l, f)?; - wasi::instance_wall_clock::add_to_linker(l, f)?; wasi::filesystem::add_to_linker(l, f)?; wasi::poll::add_to_linker(l, f)?; wasi::streams::add_to_linker(l, f)?; diff --git a/host/src/poll.rs b/host/src/poll.rs index 0cda531ad813..7247fa6b181e 100644 --- a/host/src/poll.rs +++ b/host/src/poll.rs @@ -1,12 +1,11 @@ use crate::{ command, - command::wasi::monotonic_clock::{Instant, MonotonicClock}, + command::wasi::monotonic_clock::Instant, command::wasi::poll::Pollable, command::wasi::streams::{InputStream, OutputStream, StreamError}, command::wasi::tcp::TcpSocket, proxy, WasiCtx, }; -use wasi_common::clocks::TableMonotonicClockExt; use wasi_common::stream::TableStreamExt; use wasi_common::tcp_socket::TableTcpSocketExt; @@ -26,7 +25,7 @@ pub(crate) enum PollableEntry { /// Poll for write events. Write(OutputStream), /// Poll for a monotonic-clock timer. - MonotonicClock(MonotonicClock, Instant, bool), + MonotonicClock(Instant, bool), /// Poll for a tcp-socket. TcpSocket(TcpSocket), } @@ -58,9 +57,8 @@ async fn poll_oneoff(ctx: &mut WasiCtx, futures: Vec) -> anyhow::Resul ctx.table().get_output_stream(stream).map_err(convert)?; poll.subscribe_write(wasi_stream, userdata); } - PollableEntry::MonotonicClock(clock, when, absolute) => { - let wasi_clock = ctx.table().get_monotonic_clock(clock).map_err(convert)?; - poll.subscribe_monotonic_clock(wasi_clock, when, absolute, userdata); + PollableEntry::MonotonicClock(when, absolute) => { + poll.subscribe_monotonic_clock(&*ctx.clocks.monotonic, when, absolute, userdata); } PollableEntry::TcpSocket(tcp_socket) => { let wasi_tcp_socket: &dyn wasi_common::WasiTcpSocket = diff --git a/host/tests/command.rs b/host/tests/command.rs index c49a94fe788d..3356f3e4a279 100644 --- a/host/tests/command.rs +++ b/host/tests/command.rs @@ -127,10 +127,6 @@ async fn run_time(mut store: Store, wasi: Command) -> Result<()> { fn now(&self) -> Duration { Duration::new(1431648000, 100) } - - fn dup(&self) -> Box { - Box::new(Self) - } } struct FakeMonotonicClock { @@ -148,18 +144,10 @@ async fn run_time(mut store: Store, wasi: Command) -> Result<()> { *now += 42 * 1_000_000_000; then } - - fn dup(&self) -> Box { - let now = *self.now.lock().unwrap(); - Box::new(Self { - now: Mutex::new(now), - }) - } } - store.data_mut().clocks.instance_wall_clock = Box::new(FakeWallClock); - store.data_mut().clocks.instance_monotonic_clock = - Box::new(FakeMonotonicClock { now: Mutex::new(0) }); + store.data_mut().clocks.wall = Box::new(FakeWallClock); + store.data_mut().clocks.monotonic = Box::new(FakeMonotonicClock { now: Mutex::new(0) }); wasi.call_run(&mut store) .await? diff --git a/wasi-common/cap-std-sync/src/clocks.rs b/wasi-common/cap-std-sync/src/clocks.rs index 121d20d6b78c..f33245aa13a5 100644 --- a/wasi-common/cap-std-sync/src/clocks.rs +++ b/wasi-common/cap-std-sync/src/clocks.rs @@ -6,17 +6,12 @@ use wasi_common::clocks::{WasiClocks, WasiMonotonicClock, WasiWallClock}; pub struct WallClock { /// The underlying system clock. clock: cap_std::time::SystemClock, - - /// The ambient authority used to create this `WallClock` and - /// which we use to create clones of it. - ambient_authority: AmbientAuthority, } impl WallClock { pub fn new(ambient_authority: AmbientAuthority) -> Self { Self { clock: cap_std::time::SystemClock::new(ambient_authority), - ambient_authority, } } } @@ -33,14 +28,6 @@ impl WasiWallClock for WallClock { .duration_since(SystemClock::UNIX_EPOCH) .unwrap() } - - fn dup(&self) -> Box { - let clock = cap_std::time::SystemClock::new(self.ambient_authority); - Box::new(Self { - clock, - ambient_authority: self.ambient_authority, - }) - } } pub struct MonotonicClock { @@ -50,21 +37,13 @@ pub struct MonotonicClock { /// The `Instant` this clock was created. All returned times are /// durations since that time. initial: Instant, - - /// The ambient authority used to create this `MonotonicClock` and - /// which we use to create clones of it. - ambient_authority: AmbientAuthority, } impl MonotonicClock { pub fn new(ambient_authority: AmbientAuthority) -> Self { let clock = cap_std::time::MonotonicClock::new(ambient_authority); let initial = clock.now(); - Self { - clock, - initial, - ambient_authority, - } + Self { clock, initial } } } @@ -83,24 +62,12 @@ impl WasiMonotonicClock for MonotonicClock { .try_into() .unwrap() } - - fn dup(&self) -> Box { - let clock = cap_std::time::MonotonicClock::new(self.ambient_authority); - Box::new(Self { - clock, - initial: self.initial, - ambient_authority: self.ambient_authority, - }) - } } pub fn clocks_ctx() -> WasiClocks { // Create the per-instance clock resources. - let instance_monotonic_clock = Box::new(MonotonicClock::new(ambient_authority())); - let instance_wall_clock = Box::new(WallClock::new(ambient_authority())); + let monotonic = Box::new(MonotonicClock::new(ambient_authority())); + let wall = Box::new(WallClock::new(ambient_authority())); - WasiClocks { - instance_monotonic_clock, - instance_wall_clock, - } + WasiClocks { monotonic, wall } } diff --git a/wasi-common/src/clocks.rs b/wasi-common/src/clocks.rs index 9dd03ff2eaba..ad3e6a6f8274 100644 --- a/wasi-common/src/clocks.rs +++ b/wasi-common/src/clocks.rs @@ -1,75 +1,16 @@ -use crate::Error; use cap_std::time::Duration; pub trait WasiWallClock: Send + Sync { fn resolution(&self) -> Duration; fn now(&self) -> Duration; - fn dup(&self) -> Box; } pub trait WasiMonotonicClock: Send + Sync { fn resolution(&self) -> u64; fn now(&self) -> u64; - fn dup(&self) -> Box; } pub struct WasiClocks { - pub instance_wall_clock: Box, - pub instance_monotonic_clock: Box, -} - -pub trait TableWallClockExt { - fn get_wall_clock(&self, fd: u32) -> Result<&(dyn WasiWallClock + Send + Sync), Error>; - fn get_wall_clock_mut( - &mut self, - fd: u32, - ) -> Result<&mut Box, Error>; - fn delete_wall_clock(&mut self, fd: u32) -> Result<(), Error>; -} -impl TableWallClockExt for crate::table::Table { - fn get_wall_clock(&self, fd: u32) -> Result<&(dyn WasiWallClock + Send + Sync), Error> { - self.get::>(fd) - .map(|f| f.as_ref()) - } - fn get_wall_clock_mut( - &mut self, - fd: u32, - ) -> Result<&mut Box, Error> { - self.get_mut::>(fd) - } - fn delete_wall_clock(&mut self, fd: u32) -> Result<(), Error> { - self.delete::>(fd) - .map(|_old| ()) - } -} - -pub trait TableMonotonicClockExt { - fn get_monotonic_clock( - &self, - fd: u32, - ) -> Result<&(dyn WasiMonotonicClock + Send + Sync), Error>; - fn get_monotonic_clock_mut( - &mut self, - fd: u32, - ) -> Result<&mut Box, Error>; - fn delete_monotonic_clock(&mut self, fd: u32) -> Result<(), Error>; -} -impl TableMonotonicClockExt for crate::table::Table { - fn get_monotonic_clock( - &self, - fd: u32, - ) -> Result<&(dyn WasiMonotonicClock + Send + Sync), Error> { - self.get::>(fd) - .map(|f| f.as_ref()) - } - fn get_monotonic_clock_mut( - &mut self, - fd: u32, - ) -> Result<&mut Box, Error> { - self.get_mut::>(fd) - } - fn delete_monotonic_clock(&mut self, fd: u32) -> Result<(), Error> { - self.delete::>(fd) - .map(|_old| ()) - } + pub wall: Box, + pub monotonic: Box, }