1 use futures::channel::oneshot; 2 use std::thread; 3 4 pub async fn sleep(duration: std::time::Duration) { 5 if cfg!(miri) { 6 // TODO: We should be able to use `tokio::time::sleep` here, but as of 7 // this writing the miri-compatible version of `wasmtime-fiber` uses 8 // threads behind the scenes, which means thread-local storage is not 9 // preserved when we switch fibers, and that confuses Tokio. If we ever 10 // fix that we can stop using our own, special version of `sleep` and 11 // switch back to the Tokio version. 12 13 let (tx, rx) = oneshot::channel(); 14 let handle = thread::spawn(move || { 15 thread::sleep(duration); 16 _ = tx.send(()); 17 }); 18 _ = rx.await; 19 _ = handle.join(); 20 } else { 21 tokio::time::sleep(duration).await; 22 } 23 } 24