wasi_common/tokio/
sched.rs

1#[cfg(unix)]
2mod unix;
3#[cfg(unix)]
4pub use unix::poll_oneoff;
5
6#[cfg(windows)]
7mod windows;
8#[cfg(windows)]
9pub use windows::poll_oneoff;
10
11use crate::{
12    sched::{Duration, Poll, WasiSched},
13    Error,
14};
15
16pub fn sched_ctx() -> Box<dyn crate::WasiSched> {
17    struct AsyncSched;
18
19    #[wiggle::async_trait]
20    impl WasiSched for AsyncSched {
21        async fn poll_oneoff<'a>(&self, poll: &mut Poll<'a>) -> Result<(), Error> {
22            poll_oneoff(poll).await
23        }
24        async fn sched_yield(&self) -> Result<(), Error> {
25            tokio::task::yield_now().await;
26            Ok(())
27        }
28        async fn sleep(&self, duration: Duration) -> Result<(), Error> {
29            tokio::time::sleep(duration).await;
30            Ok(())
31        }
32    }
33
34    Box::new(AsyncSched)
35}