fluke_buffet/
lib.rs

1use std::future::Future;
2
3mod roll;
4pub use roll::*;
5
6mod piece;
7pub use piece::*;
8
9pub mod bufpool;
10use bufpool::*;
11
12mod io;
13pub use io::*;
14
15pub mod net;
16
17#[cfg(all(target_os = "linux", feature = "uring"))]
18mod uring;
19
20#[cfg(all(target_os = "linux", feature = "uring"))]
21pub use uring::get_ring;
22
23/// Spawns a new asynchronous task, returning a [tokio::task::JoinHandle] for it.
24///
25/// Spawning a task enables the task to execute concurrently to other tasks.
26/// There is no guarantee that a spawned task will execute to completion. When a
27/// runtime is shutdown, all outstanding tasks are dropped, regardless of the
28/// lifecycle of that task.
29///
30/// This must be executed from within a runtime created by [crate::start]
31pub fn spawn<T: Future + 'static>(task: T) -> tokio::task::JoinHandle<T::Output> {
32    tokio::task::spawn_local(task)
33}
34
35/// Build a new current-thread runtime and runs the provided future on it
36#[cfg(all(target_os = "linux", feature = "uring"))]
37pub fn start<F: Future>(task: F) -> F::Output {
38    use fluke_io_uring_async::IoUringAsync;
39    use send_wrapper::SendWrapper;
40    use tokio::task::LocalSet;
41
42    let u = SendWrapper::new(uring::get_ring());
43    let rt = tokio::runtime::Builder::new_current_thread()
44        .enable_all()
45        .on_thread_park(move || {
46            u.submit().unwrap();
47        })
48        .build()
49        .unwrap();
50    let res = rt.block_on(async move {
51        let local = LocalSet::new();
52        local.spawn_local(IoUringAsync::listen(get_ring()));
53
54        let res = local.run_until(task).await;
55        if (tokio::time::timeout(std::time::Duration::from_millis(250), local).await).is_err() {
56            eprintln!("timed out waiting for local set");
57        }
58        res
59    });
60    rt.shutdown_timeout(std::time::Duration::from_millis(250));
61    res
62}
63
64/// Build a new current-thread runtime and runs the provided future on it
65#[cfg(not(all(target_os = "linux", feature = "uring")))]
66pub fn start<F: Future>(task: F) -> F::Output {
67    use tokio::task::LocalSet;
68
69    tokio::runtime::Builder::new_current_thread()
70        .enable_all()
71        .build()
72        .unwrap()
73        .block_on(async move {
74            let local = LocalSet::new();
75            local.run_until(task).await
76        })
77}