#![allow(dead_code)]
use std::future::Future;
pub async fn bounded<T>(millis: u64, fut: impl Future<Output = T>) -> Option<T> {
let (tx, rx) = tokio::sync::oneshot::channel::<()>();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(millis));
let _ = tx.send(());
});
tokio::pin!(fut);
tokio::pin!(rx);
tokio::select! {
out = &mut fut => Some(out),
_ = &mut rx => None,
}
}
pub fn deadlock_watchdog<T: Send + 'static>(
failure: &str,
probe: impl FnOnce() -> T + Send + 'static,
) -> T {
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let out = probe();
let _ = tx.send(out);
});
rx.recv_timeout(std::time::Duration::from_secs(5))
.unwrap_or_else(|_| panic!("{failure}"))
}
pub fn ordinary_fiber_count(ctx: &cordis_core::Context) -> usize {
ctx.runtime_snapshot()
.fibers()
.iter()
.filter(|fiber| fiber.role() == cordis_core::lifecycle::FiberRole::Ordinary)
.count()
}