Skip to main content

byteflow/scheduler/
runtime.rs

1use std::sync::atomic::{AtomicBool, Ordering};
2use std::sync::{Arc, Condvar, Mutex};
3use std::thread::JoinHandle;
4
5use crate::bytecode::{Chunk, Value};
6use crate::vm::{NativeTable, Vm};
7use crossbeam_deque::{Injector, Stealer, Worker as LocalDeque};
8
9use super::directory::Directory;
10use super::error::SpawnError;
11use super::handle::FlowHandle;
12use super::mailbox::{Delivery, Mailbox, MailboxConfig};
13use super::metrics::{RuntimeMetrics, RuntimeMetricsSnapshot};
14use super::process::{Flow, FlowId, RestartPolicy};
15use super::supervisor::SupervisorLink;
16use super::timer::TimerWheel;
17use super::worker;
18
19/// Default instruction budget per scheduling turn (design notes §10).
20/// Chosen as a middle ground: large enough that the per-yield bookkeeping
21/// cost is amortized over meaningful work, small enough that a
22/// pathological `loop {}` in one flow can't visibly stall the others —
23/// at 10k simple instructions/turn and even a conservative tens-of-millions
24/// of instructions/sec per core, worst-case added latency for a sibling
25/// flow is sub-millisecond.
26pub const DEFAULT_QUANTUM: u32 = 10_000;
27
28/// Tunables for [`Runtime::new`]. Everything has a sensible default via
29/// [`RuntimeConfig::default`] so the common case is `Runtime::new(chunk)`.
30#[derive(Clone, Debug)]
31pub struct RuntimeConfig {
32    /// Number of worker OS threads. Defaults to the number of logical CPUs
33    /// — one worker per core is the right starting point for a CPU-bound
34    /// M:N scheduler; embedders running alongside other CPU-heavy work on
35    /// the same machine may want fewer.
36    pub workers: usize,
37    /// Instructions a flow runs before being preempted back to the
38    /// scheduler even if it never hits `Yield`.
39    pub quantum: u32,
40    /// Memory + overflow contract applied to **every** flow mailbox
41    /// spawned by this runtime (bytecode `Spawn` and host `spawn`).
42    /// See [`MailboxConfig`] / `docs/mailbox.md`.
43    pub mailbox: MailboxConfig,
44}
45
46impl Default for RuntimeConfig {
47    fn default() -> Self {
48        RuntimeConfig {
49            workers: num_cpus::get().max(1),
50            quantum: DEFAULT_QUANTUM,
51            mailbox: MailboxConfig::DEFAULT,
52        }
53    }
54}
55
56/// State shared by every worker thread and the timer thread. Everything in
57/// here is either internally synchronized (`Injector`, `Directory`,
58/// `CapTable`, `TimerWheel`, the `RuntimeMetrics` atomics) or immutable after
59/// construction (`stealers`, `quantum`) — there is no top-level lock
60/// covering the whole runtime, by design: a global lock is exactly what an
61/// M:N scheduler exists to avoid.
62pub struct Shared {
63    pub(crate) injector: Injector<Box<Flow>>,
64    pub(crate) stealers: Vec<Stealer<Box<Flow>>>,
65    /// FlowId → mailbox (delivery after Cap resolution).
66    pub(crate) directory: Directory,
67    /// CapId → { FlowId, rights } (bytecode Send/Ask addressing — FlowCap).
68    pub(crate) caps: super::capability::CapTable,
69    pub(crate) timer: Arc<TimerWheel>,
70    pub(crate) notify: (Mutex<()>, Condvar),
71    pub(crate) metrics: RuntimeMetrics,
72    pub(crate) shutdown: AtomicBool,
73    pub(crate) quantum: u32,
74    pub(crate) mailbox: MailboxConfig,
75}
76
77/// A running Byteflow runtime: worker pool + timer thread over one shared
78/// [`Chunk`].
79///
80/// Owns M:N scheduling for **flows** (spawn, yield, sleep, mailboxes,
81/// FlowCap resolution, supervised restarts). Intentionally *not* here yet:
82/// JIT, native quotas, distribution across machines.
83///
84/// Construct with [`Runtime::new`] (no natives) or
85/// [`Runtime::with_natives`] when the chunk uses `CallNative` /
86/// [`crate::std_native_table`].
87pub struct Runtime {
88    shared: Arc<Shared>,
89    chunk: Arc<Chunk>,
90    natives: Arc<NativeTable>,
91    workers: Vec<JoinHandle<()>>,
92    timer_thread: Option<JoinHandle<()>>,
93}
94
95impl Runtime {
96    /// Convenience constructor for chunks that never call out through
97    /// `Opcode::CallNative`. Equivalent to
98    /// `Runtime::with_natives(chunk, NativeTable::empty())`.
99    ///
100    /// Returns [`SpawnError`] instead of panicking: verify failures and OS
101    /// thread-spawn refusals are category-A errors (see
102    /// [`super::error`]).
103    pub fn new(chunk: Chunk) -> Result<Self, SpawnError> {
104        Self::with_config(chunk, RuntimeConfig::default())
105    }
106
107    /// Construct a runtime whose flows can call into `natives` via
108    /// `Opcode::CallNative` — the host FFI boundary.
109    pub fn with_natives(chunk: Chunk, natives: Arc<NativeTable>) -> Result<Self, SpawnError> {
110        Self::with_natives_and_config(chunk, natives, RuntimeConfig::default())
111    }
112
113    pub fn with_config(chunk: Chunk, config: RuntimeConfig) -> Result<Self, SpawnError> {
114        Self::with_natives_and_config(chunk, NativeTable::empty(), config)
115    }
116
117    /// Verify `chunk`, spawn the worker pool + timer thread, and return a
118    /// live [`Runtime`].
119    ///
120    /// Failures here mean the runtime was **never** started (no orphan
121    /// threads): either the bytecode is invalid
122    /// ([`SpawnError::VerifyFailed`]) or the OS refused a thread
123    /// ([`SpawnError::ThreadSpawnFailed`]).
124    pub fn with_natives_and_config(
125        chunk: Chunk,
126        natives: Arc<NativeTable>,
127        config: RuntimeConfig,
128    ) -> Result<Self, SpawnError> {
129        crate::bytecode::verify(&chunk).map_err(|e| SpawnError::VerifyFailed(e.to_string()))?;
130        let chunk = Arc::new(chunk);
131        let workers_n = config.workers.max(1);
132
133        let locals: Vec<LocalDeque<Box<Flow>>> =
134            (0..workers_n).map(|_| LocalDeque::new_fifo()).collect();
135        let stealers: Vec<Stealer<Box<Flow>>> = locals.iter().map(|l| l.stealer()).collect();
136
137        let shared = Arc::new(Shared {
138            injector: Injector::new(),
139            stealers,
140            directory: Directory::new(),
141            caps: super::capability::CapTable::new(),
142            timer: TimerWheel::new(),
143            notify: (Mutex::new(()), Condvar::new()),
144            metrics: RuntimeMetrics::default(),
145            shutdown: AtomicBool::new(false),
146            quantum: config.quantum,
147            mailbox: config.mailbox,
148        });
149
150        let mut workers = Vec::with_capacity(workers_n);
151        for local in locals {
152            let shared = shared.clone();
153            let handle = std::thread::Builder::new()
154                .name("byteflow-worker".into())
155                .spawn(move || worker::run_worker(shared, local))
156                .map_err(|e| SpawnError::ThreadSpawnFailed(e.to_string()))?;
157            workers.push(handle);
158        }
159
160        let shared_timer = shared.clone();
161        let timer_thread = std::thread::Builder::new()
162            .name("byteflow-timer".into())
163            .spawn(move || {
164                shared_timer
165                    .timer
166                    .clone()
167                    .drive(&shared_timer.injector, &shared_timer.notify)
168            })
169            .map_err(|e| SpawnError::ThreadSpawnFailed(e.to_string()))?;
170
171        Ok(Runtime {
172            shared,
173            chunk,
174            natives,
175            workers,
176            timer_thread: Some(timer_thread),
177        })
178    }
179
180    /// Spawn a top-level flow starting at `function` in this runtime's
181    /// chunk, returning a [`FlowHandle`] the caller can `.join()`.
182    ///
183    /// Returns [`SpawnError::BadFunction`] if `function` is out of range.
184    pub fn spawn(&self, function: u32, args: &[Value]) -> Result<FlowHandle, SpawnError> {
185        spawn_on(
186            &self.shared,
187            &self.chunk,
188            &self.natives,
189            function,
190            args,
191            RestartPolicy::Never,
192            None,
193        )
194    }
195
196    /// A cheap, `Send + Sync` handle that can spawn processes into this
197    /// runtime from any thread, independent of `Runtime`'s own lifetime
198    /// bookkeeping (worker `JoinHandle`s). Used by [`super::supervisor::Supervisor`].
199    pub fn spawner(&self) -> RuntimeSpawner {
200        RuntimeSpawner { shared: self.shared.clone(), chunk: self.chunk.clone(), natives: self.natives.clone() }
201    }
202
203    /// A [`super::supervisor::Supervisor`] bound to this runtime, ready to
204    /// take supervised children (design notes §15).
205    pub fn supervisor(&self) -> Result<super::supervisor::Supervisor, SpawnError> {
206        super::supervisor::Supervisor::new(self.spawner())
207    }
208
209    /// Look up a function by name in the runtime's chunk — convenience for
210    /// callers that built their chunk with [`crate::bytecode::ChunkBuilder`]
211    /// and don't want to thread raw indices through their own code.
212    pub fn function_index(&self, name: &str) -> Option<u32> {
213        self.chunk.functions.iter().position(|f| f.name == name).map(|i| i as u32)
214    }
215
216    pub fn metrics(&self) -> RuntimeMetricsSnapshot {
217        self.shared.metrics.snapshot()
218    }
219
220    /// Number of flows currently registered in the directory — i.e.
221    /// alive (running, ready, sleeping, or waiting), not counting ones that
222    /// have already completed or failed.
223    pub fn live_flows(&self) -> usize {
224        self.shared.directory.len()
225    }
226
227    pub fn worker_count(&self) -> usize {
228        self.workers.len()
229    }
230
231    /// Deliver an **Atomic Hop** (`Value::Message`) to `target` from the
232    /// embedder (not from bytecode).
233    ///
234    /// # Host trust boundary
235    ///
236    /// This path takes a [`FlowId`] directly — **no Cap required**. The host
237    /// is trusted; bytecode must use `Value::Cap` via `Opcode::Send` /
238    /// `Ask`. Host-injected messages are not re-stamped (`sender` /
239    /// `reply_cap` stay as built). Bare scalars are rejected
240    /// ([`SendError::NotAHop`]).
241    pub fn send(&self, target: FlowId, message: Value) -> Result<(), SendError> {
242        if message.as_message().is_none() {
243            return Err(SendError::NotAHop {
244                got: message.type_name(),
245            });
246        }
247        let mailbox = match self.shared.directory.lookup(target) {
248            Ok(Some(m)) => m,
249            Ok(None) => return Err(SendError::NoSuchFlow(target)),
250            Err(e) => {
251                super::error::report_fault(e);
252                return Err(SendError::NoSuchFlow(target));
253            }
254        };
255        match mailbox.push(message.clone()) {
256            Ok(Ok(Delivery::Queued | Delivery::QueuedDropOldest | Delivery::DroppedNewest)) => {
257                Ok(())
258            }
259            Ok(Ok(Delivery::Handoff(mut flow))) => {
260                if let Some(dest) = flow.last_receive_dest {
261                    let _ = flow.vm.resume_with(dest, message);
262                }
263                self.shared.injector.push(flow);
264                wake_workers(&self.shared);
265                Ok(())
266            }
267            Ok(Err(_)) => Err(SendError::MailboxFull(target)),
268            Err(e) => {
269                super::error::report_fault(e);
270                Err(SendError::NoSuchFlow(target))
271            }
272        }
273    }
274
275    /// Stop accepting new scheduling work and join every worker + the timer
276    /// thread. Processes that are mid-quantum are allowed to reach their
277    /// next natural suspension point; this does **not** forcibly abort
278    /// running bytecode (there is no safe way to do that to an OS thread
279    /// mid-instruction — see design notes §11 on why preemption here is
280    /// cooperative/budgeted rather than signal-based).
281    pub fn shutdown(mut self) {
282        self.shared.shutdown.store(true, Ordering::Release);
283        self.shared.timer.shutdown();
284        {
285            let (lock, cvar) = &self.shared.notify;
286            match super::sync_lock::lock(lock, "Runtime::shutdown") {
287                Ok(_g) => cvar.notify_all(),
288                Err(e) => super::error::report_fault(e),
289            }
290        }
291        for w in self.workers.drain(..) {
292            let _ = w.join();
293        }
294        if let Some(t) = self.timer_thread.take() {
295            let _ = t.join();
296        }
297    }
298}
299
300/// A convenience Pid constructor for embedders that stored a raw `u64`
301/// (e.g. round-tripped through `Value::Pid`) and need a [`FlowId`] to
302/// call APIs that take one.
303pub fn flow_id_from_u64(raw: u64) -> FlowId {
304    FlowId(raw)
305}
306
307/// Why [`Runtime::send`] could not deliver a hop.
308#[derive(Debug, Clone, PartialEq, Eq)]
309pub enum SendError {
310    NoSuchFlow(FlowId),
311    /// Atomic Hop rule: only [`crate::Value::Message`] may cross `Send`.
312    NotAHop { got: &'static str },
313    /// Target inbox is at its logical capacity ([`OverflowPolicy::Reject`]).
314    MailboxFull(FlowId),
315}
316
317impl std::fmt::Display for SendError {
318    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
319        match self {
320            SendError::NoSuchFlow(id) => write!(f, "no live flow {id}"),
321            SendError::NotAHop { got } => {
322                write!(f, "atomic hop requires Value::Message, got {got}")
323            }
324            SendError::MailboxFull(id) => write!(f, "mailbox full for {id}"),
325        }
326    }
327}
328
329impl std::error::Error for SendError {}
330
331/// Shared machinery behind `Runtime::spawn` and `RuntimeSpawner::spawn`
332/// (and, transitively, `Supervisor`): build a fresh `Flow` (VM +
333/// mailbox + completion channel), register it in the directory, and push
334/// it onto the global injector for any worker to pick up.
335///
336/// Returns [`SpawnError`] on bad function index / VM init / directory
337/// poison — never panics. Bytecode `Opcode::Spawn` that fails here turns
338/// into `FlowOutcome::Failed` for the *parent* (see `worker`).
339pub(crate) fn spawn_on(
340    shared: &Arc<Shared>,
341    chunk: &Arc<Chunk>,
342    natives: &Arc<NativeTable>,
343    function: u32,
344    args: &[Value],
345    restart_policy: RestartPolicy,
346    supervisor: Option<SupervisorLink>,
347) -> Result<FlowHandle, SpawnError> {
348    let id = super::process::next_flow_id();
349    let vm = Vm::new(chunk.clone(), natives.clone(), function, args)?;
350    let mailbox = Arc::new(Mailbox::with_config(shared.mailbox));
351    if let Err(e) = shared.directory.register(id, mailbox.clone()) {
352        super::error::report_fault(e);
353        return Err(SpawnError::VmInit(
354            "directory register failed (poisoned lock)".into(),
355        ));
356    }
357    let (tx, rx) = super::oneshot::channel();
358    let mut flow = Box::new(Flow::new(id, vm, mailbox, restart_policy, tx));
359    flow.supervisor = supervisor;
360    RuntimeMetrics::inc(&shared.metrics.processes_spawned);
361    shared.injector.push(flow);
362    wake_workers(shared);
363    Ok(FlowHandle { id, receiver: rx })
364}
365
366pub(crate) fn wake_workers(shared: &Shared) {
367    let (lock, cvar) = &shared.notify;
368    match super::sync_lock::lock(lock, "wake_workers") {
369        Ok(_g) => cvar.notify_one(),
370        Err(e) => super::error::report_fault(e),
371    }
372}
373
374/// A `Send + Sync`, freely cloneable capability to spawn processes into a
375/// [`Runtime`], detached from the `Runtime` value itself. Exists because
376/// [`super::supervisor::Supervisor`] needs to respawn processes from a
377/// background monitor thread whose lifetime isn't tied to the `Runtime`
378/// object's own (which owns non-`Sync` `JoinHandle`s for its workers).
379#[derive(Clone)]
380pub struct RuntimeSpawner {
381    pub(crate) shared: Arc<Shared>,
382    pub(crate) chunk: Arc<Chunk>,
383    pub(crate) natives: Arc<NativeTable>,
384}
385
386impl RuntimeSpawner {
387    pub fn spawn(
388        &self,
389        function: u32,
390        args: &[Value],
391        restart_policy: RestartPolicy,
392    ) -> Result<FlowHandle, SpawnError> {
393        spawn_on(
394            &self.shared,
395            &self.chunk,
396            &self.natives,
397            function,
398            args,
399            restart_policy,
400            None,
401        )
402    }
403
404    pub(crate) fn spawn_linked(
405        &self,
406        function: u32,
407        args: &[Value],
408        restart_policy: RestartPolicy,
409        supervisor: SupervisorLink,
410    ) -> Result<FlowHandle, SpawnError> {
411        spawn_on(
412            &self.shared,
413            &self.chunk,
414            &self.natives,
415            function,
416            args,
417            restart_policy,
418            Some(supervisor),
419        )
420    }
421
422    pub fn metrics(&self) -> RuntimeMetricsSnapshot {
423        self.shared.metrics.snapshot()
424    }
425}
426
427#[cfg(test)]
428mod tests {
429    use super::*;
430    use crate::bytecode::{ChunkBuilder, Opcode, Value};
431    use crate::scheduler::FlowOutcome;
432
433    fn add_chunk() -> Chunk {
434        let mut b = ChunkBuilder::new("test");
435        b.begin_function("main", 0, 2);
436        b.emit_load_imm(0, 41);
437        b.emit_load_imm(1, 1);
438        b.emit_binop(Opcode::Add, 0, 0, 1);
439        b.emit_return(0);
440        b.finish()
441    }
442
443    #[test]
444    fn spawn_and_join_add() -> Result<(), Box<dyn std::error::Error>> {
445        let rt = Runtime::with_config(
446            add_chunk(),
447            RuntimeConfig {
448                workers: 1,
449                quantum: 1_000,
450                mailbox: MailboxConfig::DEFAULT,
451            },
452        )?;
453        let outcome = rt.spawn(0, &[])?.join();
454        rt.shutdown();
455        match outcome {
456            FlowOutcome::Completed(Value::Int(42)) => Ok(()),
457            other => Err(format!("unexpected outcome: {other:?}").into()),
458        }
459    }
460}