Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi/src/p3/clocks/host.rs
3101 views
1
use crate::clocks::WasiClocksCtxView;
2
use crate::p3::bindings::clocks::{monotonic_clock, system_clock, types};
3
use crate::p3::clocks::WasiClocks;
4
use core::time::Duration;
5
use tokio::time::sleep;
6
use wasmtime::component::Accessor;
7
8
impl types::Host for WasiClocksCtxView<'_> {}
9
10
impl system_clock::Host for WasiClocksCtxView<'_> {
11
fn now(&mut self) -> wasmtime::Result<system_clock::Instant> {
12
let now = self.ctx.wall_clock.now();
13
Ok(system_clock::Instant {
14
seconds: now.as_secs().try_into()?,
15
nanoseconds: now.subsec_nanos(),
16
})
17
}
18
19
fn get_resolution(&mut self) -> wasmtime::Result<types::Duration> {
20
let res = self.ctx.wall_clock.resolution();
21
Ok(res.as_nanos().try_into()?)
22
}
23
}
24
25
impl monotonic_clock::HostWithStore for WasiClocks {
26
async fn wait_until<U>(
27
store: &Accessor<U, Self>,
28
when: monotonic_clock::Mark,
29
) -> wasmtime::Result<()> {
30
let clock_now = store.with(|mut view| view.get().ctx.monotonic_clock.now());
31
if when > clock_now {
32
sleep(Duration::from_nanos(when - clock_now)).await;
33
};
34
Ok(())
35
}
36
37
async fn wait_for<U>(
38
_store: &Accessor<U, Self>,
39
duration: types::Duration,
40
) -> wasmtime::Result<()> {
41
if duration > 0 {
42
sleep(Duration::from_nanos(duration)).await;
43
}
44
Ok(())
45
}
46
}
47
48
impl monotonic_clock::Host for WasiClocksCtxView<'_> {
49
fn now(&mut self) -> wasmtime::Result<monotonic_clock::Mark> {
50
Ok(self.ctx.monotonic_clock.now())
51
}
52
53
fn get_resolution(&mut self) -> wasmtime::Result<types::Duration> {
54
Ok(self.ctx.monotonic_clock.resolution())
55
}
56
}
57
58