wasi_common/sync/
sched.rs

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