Skip to main content

wasmtime_wasi/p3/clocks/
host.rs

1use crate::clocks::WasiClocksCtxView;
2use crate::p3::bindings::clocks::{monotonic_clock, system_clock, types};
3use crate::p3::clocks::WasiClocks;
4use crate::runtime::with_ambient_tokio_runtime;
5use core::time::Duration;
6use wasmtime::component::Accessor;
7
8fn sleep(duration: Duration) -> tokio::time::Sleep {
9    with_ambient_tokio_runtime(|| tokio::time::sleep(duration))
10}
11
12impl WasiClocksCtxView<'_> {
13    fn monotonic_wait_until_duration(&mut self, when: monotonic_clock::Mark) -> Option<Duration> {
14        let clock_now = self.ctx.monotonic_clock.now();
15        if when > clock_now {
16            Some(Duration::from_nanos(when - clock_now))
17        } else {
18            None
19        }
20    }
21}
22
23impl types::Host for WasiClocksCtxView<'_> {}
24
25impl system_clock::Host for WasiClocksCtxView<'_> {
26    fn now(&mut self) -> wasmtime::Result<system_clock::Instant> {
27        let now = self.ctx.wall_clock.now();
28        Ok(system_clock::Instant {
29            seconds: now.as_secs().try_into()?,
30            nanoseconds: now.subsec_nanos(),
31        })
32    }
33
34    fn get_resolution(&mut self) -> wasmtime::Result<types::Duration> {
35        let res = self.ctx.wall_clock.resolution();
36        Ok(res.as_nanos().try_into()?)
37    }
38}
39
40impl<U> monotonic_clock::HostWithStore<U> for WasiClocks {
41    async fn wait_until(
42        store: &Accessor<U, Self>,
43        when: monotonic_clock::Mark,
44    ) -> wasmtime::Result<()> {
45        if let Some(dur) = store.with(|mut view| view.get().monotonic_wait_until_duration(when)) {
46            sleep(dur).await;
47        }
48        Ok(())
49    }
50
51    async fn wait_for(
52        _store: &Accessor<U, Self>,
53        duration: types::Duration,
54    ) -> wasmtime::Result<()> {
55        if duration > 0 {
56            sleep(Duration::from_nanos(duration)).await;
57        }
58        Ok(())
59    }
60}
61
62impl monotonic_clock::Host for WasiClocksCtxView<'_> {
63    fn now(&mut self) -> wasmtime::Result<monotonic_clock::Mark> {
64        Ok(self.ctx.monotonic_clock.now())
65    }
66
67    fn get_resolution(&mut self) -> wasmtime::Result<types::Duration> {
68        Ok(self.ctx.monotonic_clock.resolution())
69    }
70}
71
72mod named {
73    use super::sleep;
74    use crate::clocks::{WasiClocksNamed, WasiClocksNamedView};
75    use crate::p3::bindings::clocks::monotonic_clock::Mark;
76    use crate::p3::bindings::clocks::system_clock::Instant;
77    use crate::p3::bindings::clocks::types;
78    use crate::p3::bindings::named_imports::wasi::clocks::{monotonic_clock, system_clock};
79    use crate::{NamedId, WasiCtxNamedView};
80    use core::time::Duration;
81    use wasmtime::component::Accessor;
82
83    impl<T> system_clock::Host for WasiCtxNamedView<'_, T>
84    where
85        T: WasiClocksNamedView,
86    {
87        fn now(&mut self, id: NamedId) -> wasmtime::Result<Instant> {
88            super::system_clock::Host::now(&mut self.0.clocks(id))
89        }
90
91        fn get_resolution(&mut self, id: NamedId) -> wasmtime::Result<types::Duration> {
92            super::system_clock::Host::get_resolution(&mut self.0.clocks(id))
93        }
94    }
95
96    impl<T, U> monotonic_clock::HostWithStore<U> for WasiClocksNamed<T>
97    where
98        T: WasiClocksNamedView,
99    {
100        async fn wait_until(
101            store: &Accessor<U, Self>,
102            id: NamedId,
103            when: Mark,
104        ) -> wasmtime::Result<()> {
105            if let Some(dur) =
106                store.with(|mut view| view.get().0.clocks(id).monotonic_wait_until_duration(when))
107            {
108                sleep(dur).await;
109            }
110            Ok(())
111        }
112
113        async fn wait_for(
114            _store: &Accessor<U, Self>,
115            _id: NamedId,
116            duration: types::Duration,
117        ) -> wasmtime::Result<()> {
118            if duration > 0 {
119                sleep(Duration::from_nanos(duration)).await;
120            }
121            Ok(())
122        }
123    }
124
125    impl<T> monotonic_clock::Host for WasiCtxNamedView<'_, T>
126    where
127        T: WasiClocksNamedView,
128    {
129        fn now(&mut self, id: NamedId) -> wasmtime::Result<Mark> {
130            super::monotonic_clock::Host::now(&mut self.0.clocks(id))
131        }
132
133        fn get_resolution(&mut self, id: NamedId) -> wasmtime::Result<types::Duration> {
134            super::monotonic_clock::Host::get_resolution(&mut self.0.clocks(id))
135        }
136    }
137}