use std::future::Future;
use std::sync::mpsc as std_mpsc;
#[cfg(feature = "macros")]
pub use kioto_uring_executor_macros::{main, test};
mod runtime;
pub use runtime::{Runtime, SpawnRing};
use runtime::ACTIVE_RUNTIME;
pub fn block_on<T: 'static, F: Future<Output = T> + 'static>(task: F) -> T {
tokio_uring::start(task)
}
pub fn new_spawn_ring() -> SpawnRing {
ACTIVE_RUNTIME.with_borrow(|r| {
let inner = r.as_ref().expect("No active runtime").clone();
SpawnRing::new(inner)
})
}
pub fn spawn<F: Future<Output = ()> + Send + 'static>(task: F) {
ACTIVE_RUNTIME.with_borrow(|r| r.as_ref().expect("No active runtime").spawn(task))
}
pub fn spawn_at<F: Future<Output = ()> + Send + 'static>(offset: usize, task: F) {
ACTIVE_RUNTIME.with_borrow(|r| {
r.as_ref()
.expect("No active runtime")
.spawn_at(offset, task)
})
}
pub fn block_on_runtime<T: Send + 'static, F: Future<Output = T> + Send + 'static>(task: F) -> T {
ACTIVE_RUNTIME.with_borrow(|r| r.as_ref().expect("No active runtime").block_on(task))
}
pub unsafe fn unsafe_block_on_runtime<T: 'static, F: Future<Output = T> + 'static>(task: F) -> T {
let (sender, receiver) = std_mpsc::channel();
unsafe_spawn(async move {
let res = task.await;
sender.send(res).expect("Notification failed");
});
receiver.recv().expect("Failed to wait for task")
}
pub unsafe fn unsafe_spawn<F: Future<Output = ()> + 'static>(task: F) {
ACTIVE_RUNTIME.with_borrow(|r| r.as_ref().expect("No active runtime").unsafe_spawn(task))
}
pub unsafe fn unsafe_spawn_at<F: Future<Output = ()> + 'static>(offset: usize, task: F) {
ACTIVE_RUNTIME.with_borrow(|r| {
r.as_ref()
.expect("No active runtime")
.unsafe_spawn_at(offset, task)
})
}