Files
wasmtime/crates/wasi-common/cap-std-sync/src/clocks.rs
Ingvar Stepanyan 873d3b50a0 Allow to disable clocks in WasiCtx (#6007)
Takes the approach described in #6004, but also creates a wrapper for the monotonic time that encapsulates the `creation_time` field as well, since they logically belong and are always used together.

This makes it easier to configure `WasiCtx` with custom clocks as well as disable them for security or determinism reasons.

Closes #6004.
2023-03-13 23:47:04 +00:00

42 lines
1.3 KiB
Rust

use cap_std::time::{Duration, Instant, SystemTime};
use cap_std::{ambient_authority, AmbientAuthority};
use cap_time_ext::{MonotonicClockExt, SystemClockExt};
use wasi_common::clocks::{WasiClocks, WasiMonotonicClock, WasiSystemClock};
pub struct SystemClock(cap_std::time::SystemClock);
impl SystemClock {
pub fn new(ambient_authority: AmbientAuthority) -> Self {
SystemClock(cap_std::time::SystemClock::new(ambient_authority))
}
}
impl WasiSystemClock for SystemClock {
fn resolution(&self) -> Duration {
self.0.resolution()
}
fn now(&self, precision: Duration) -> SystemTime {
self.0.now_with(precision)
}
}
pub struct MonotonicClock(cap_std::time::MonotonicClock);
impl MonotonicClock {
pub fn new(ambient_authority: AmbientAuthority) -> Self {
MonotonicClock(cap_std::time::MonotonicClock::new(ambient_authority))
}
}
impl WasiMonotonicClock for MonotonicClock {
fn resolution(&self) -> Duration {
self.0.resolution()
}
fn now(&self, precision: Duration) -> Instant {
self.0.now_with(precision)
}
}
pub fn clocks_ctx() -> WasiClocks {
WasiClocks::new()
.with_system(SystemClock::new(ambient_authority()))
.with_monotonic(MonotonicClock::new(ambient_authority()))
}