wasi_common/tokio/
sched.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#[cfg(unix)]
mod unix;
#[cfg(unix)]
pub use unix::poll_oneoff;

#[cfg(windows)]
mod windows;
#[cfg(windows)]
pub use windows::poll_oneoff;

use crate::{
    sched::{Duration, Poll, WasiSched},
    Error,
};

pub fn sched_ctx() -> Box<dyn crate::WasiSched> {
    struct AsyncSched;

    #[wiggle::async_trait]
    impl WasiSched for AsyncSched {
        async fn poll_oneoff<'a>(&self, poll: &mut Poll<'a>) -> Result<(), Error> {
            poll_oneoff(poll).await
        }
        async fn sched_yield(&self) -> Result<(), Error> {
            tokio::task::yield_now().await;
            Ok(())
        }
        async fn sleep(&self, duration: Duration) -> Result<(), Error> {
            tokio::time::sleep(duration).await;
            Ok(())
        }
    }

    Box::new(AsyncSched)
}