|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! Per-core runtime helpers and dispatching executor. |
| 5 | +
|
| 6 | +use std::sync::Arc; |
| 7 | +use std::sync::atomic::AtomicUsize; |
| 8 | +use std::sync::atomic::Ordering; |
| 9 | + |
| 10 | +use futures::future::BoxFuture; |
| 11 | +use vortex_error::vortex_panic; |
| 12 | + |
| 13 | +use crate::runtime::AbortHandleRef; |
| 14 | +use crate::runtime::Executor; |
| 15 | +use crate::runtime::Handle; |
| 16 | +use crate::runtime::IoTask; |
| 17 | +use crate::runtime::LocalExecutor; |
| 18 | +use crate::runtime::LocalSpawn; |
| 19 | + |
| 20 | +/// An executor that dispatches work across a fixed set of underlying executors. |
| 21 | +/// |
| 22 | +/// Tasks are assigned round-robin; there is no work stealing. This is intended to pair with |
| 23 | +/// per-core runtimes where each executor owns a single thread/reactor. |
| 24 | +pub(crate) struct HandleSetExecutor { |
| 25 | + executors: Arc<[Arc<dyn Executor>]>, |
| 26 | + picker: AtomicUsize, |
| 27 | +} |
| 28 | + |
| 29 | +impl HandleSetExecutor { |
| 30 | + pub(crate) fn new(executors: Vec<Arc<dyn Executor>>) -> Self { |
| 31 | + assert!(!executors.is_empty()); |
| 32 | + Self { |
| 33 | + executors: executors.into(), |
| 34 | + picker: AtomicUsize::new(0), |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + fn pick(&self) -> &Arc<dyn Executor> { |
| 39 | + let idx = self.picker.fetch_add(1, Ordering::Relaxed); |
| 40 | + // Relaxed is sufficient: we only need uniqueness, not ordering guarantees. |
| 41 | + &self.executors[idx % self.executors.len()] |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// A thin wrapper around a set of executors that produces a dispatching [`Handle`]. |
| 46 | +/// |
| 47 | +/// This is intended to be backed by per-core runtimes (e.g., io_uring reactors), but it can be |
| 48 | +/// constructed from any set of executors for now. |
| 49 | +pub(crate) struct HandleSet { |
| 50 | + executors: Arc<[Arc<dyn Executor>]>, |
| 51 | + dispatcher: Arc<HandleSetExecutor>, |
| 52 | +} |
| 53 | + |
| 54 | +impl HandleSet { |
| 55 | + pub(crate) fn new(executors: Vec<Arc<dyn Executor>>) -> Self { |
| 56 | + let executors: Arc<[Arc<dyn Executor>]> = executors.into(); |
| 57 | + let dispatcher = Arc::new(HandleSetExecutor::new( |
| 58 | + executors.iter().cloned().collect(), |
| 59 | + )); |
| 60 | + Self { |
| 61 | + executors, |
| 62 | + dispatcher, |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// Returns a handle that round-robins spawned work across the underlying executors. |
| 67 | + pub(crate) fn dispatching_handle(&self) -> Handle { |
| 68 | + let exec: Arc<dyn Executor> = self.dispatcher.clone(); |
| 69 | + Handle::new(Arc::downgrade(&exec)) |
| 70 | + } |
| 71 | + |
| 72 | + /// Access to the underlying executors, useful for building per-core pools later. |
| 73 | + pub(crate) fn executors(&self) -> &[Arc<dyn Executor>] { |
| 74 | + &self.executors |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl Executor for HandleSetExecutor { |
| 79 | + fn spawn(&self, fut: BoxFuture<'static, ()>) -> AbortHandleRef { |
| 80 | + self.pick().spawn(fut) |
| 81 | + } |
| 82 | + |
| 83 | + fn spawn_cpu(&self, task: Box<dyn FnOnce() + Send + 'static>) -> AbortHandleRef { |
| 84 | + self.pick().spawn_cpu(task) |
| 85 | + } |
| 86 | + |
| 87 | + fn spawn_blocking(&self, task: Box<dyn FnOnce() + Send + 'static>) -> AbortHandleRef { |
| 88 | + self.pick().spawn_blocking(task) |
| 89 | + } |
| 90 | + |
| 91 | + fn spawn_io(&self, task: IoTask) { |
| 92 | + self.pick().spawn_io(task) |
| 93 | + } |
| 94 | + |
| 95 | + fn as_local_executor(&self) -> Option<Arc<dyn LocalExecutor>> { |
| 96 | + self.pick().as_local_executor() |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl LocalExecutor for HandleSetExecutor { |
| 101 | + fn spawn_local(&self, f: LocalSpawn) -> AbortHandleRef { |
| 102 | + match self.pick().as_local_executor() { |
| 103 | + Some(exec) => exec.spawn_local(f), |
| 104 | + None => vortex_panic!("LocalExecutor requested but not supported by any underlying executor"), |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments