Skip to main content

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// target_os = "unknown" matches wasm32-unknown-unknown (browser), excluding WASI targets
33// where wasm-bindgen's JS interop is not available.
34#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
35pub mod wasm;
36
37#[cfg(test)]
38mod tests;
39
40/// Trait used to abstract over different async runtimes.
41pub trait Executor: Send + Sync {
42    /// Spawns a future to be executed on the runtime.
43    ///
44    /// The future should continue to be polled in the background by the runtime.
45    /// The returned `AbortHandle` may be used to optimistically cancel the future.
46    fn spawn(&self, fut: BoxFuture<'static, ()>) -> AbortHandleRef;
47
48    /// Spawns a CPU-bound task for execution on the runtime.
49    ///
50    /// The returned `AbortHandle` may be used to optimistically cancel the task if it has not
51    /// yet started executing.
52    fn spawn_cpu(&self, task: Box<dyn FnOnce() + Send + 'static>) -> AbortHandleRef;
53
54    /// Spawns a blocking I/O task for execution on the runtime.
55    ///
56    /// The returned `AbortHandle` may be used to optimistically cancel the task if it has not
57    /// yet started executing.
58    fn spawn_blocking_io(&self, task: Box<dyn FnOnce() + Send + 'static>) -> AbortHandleRef;
59}
60
61/// A handle that may be used to optimistically abort a spawned task.
62///
63/// If dropped, the task should continue to completion.
64/// If explicitly aborted, the task should be cancelled if it has not yet started executing.
65pub trait AbortHandle: Send + Sync {
66    fn abort(self: Box<Self>);
67}
68
69pub type AbortHandleRef = Box<dyn AbortHandle>;