vortex_io/runtime/mod.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! A Vortex runtime provides an abstract way of scheduling mixed I/O and CPU workloads onto the
5//! various threading models supported by Vortex.
6//!
7//! In the future, it may also include a buffer manager or other shared resources.
8//!
9//! The threading models we currently support are:
10//! * Single-threaded: all work is driven on the current thread.
11//! * Multi-threaded: work is driven on a pool of threads managed by Vortex.
12//! * Worker Pool: work is driven on a pool of threads provided by the caller.
13//! * Tokio: work is driven on a Tokio runtime provided by the caller.
14
15use futures::future::BoxFuture;
16
17mod blocking;
18pub use blocking::*;
19mod handle;
20pub use handle::*;
21
22#[cfg(not(target_arch = "wasm32"))]
23pub mod current;
24#[cfg(not(target_arch = "wasm32"))]
25mod pool;
26#[cfg(not(target_arch = "wasm32"))]
27pub mod single;
28#[cfg(not(target_arch = "wasm32"))]
29mod smol;
30#[cfg(feature = "tokio")]
31pub mod tokio;
32#[cfg(target_arch = "wasm32")]
33pub mod wasm;
34
35#[cfg(test)]
36mod tests;
37
38/// Trait used to abstract over different async runtimes.
39pub(crate) trait Executor: Send + Sync {
40 /// Spawns a future to be executed on the runtime.
41 ///
42 /// The future should continue to be polled in the background by the runtime.
43 /// The returned `AbortHandle` may be used to optimistically cancel the future.
44 fn spawn(&self, fut: BoxFuture<'static, ()>) -> AbortHandleRef;
45
46 /// Spawns a CPU-bound task for execution on the runtime.
47 ///
48 /// The returned `AbortHandle` may be used to optimistically cancel the task if it has not
49 /// yet started executing.
50 fn spawn_cpu(&self, task: Box<dyn FnOnce() + Send + 'static>) -> AbortHandleRef;
51
52 /// Spawns a blocking I/O task for execution on the runtime.
53 ///
54 /// The returned `AbortHandle` may be used to optimistically cancel the task if it has not
55 /// yet started executing.
56 fn spawn_blocking(&self, task: Box<dyn FnOnce() + Send + 'static>) -> AbortHandleRef;
57}
58
59/// A handle that may be used to optimistically abort a spawned task.
60///
61/// If dropped, the task should continue to completion.
62/// If explicitly aborted, the task should be cancelled if it has not yet started executing.
63pub(crate) trait AbortHandle: Send + Sync {
64 fn abort(self: Box<Self>);
65}
66
67pub(crate) type AbortHandleRef = Box<dyn AbortHandle>;