Skip to main content

Runtime

Struct Runtime 

Source
pub struct Runtime { /* private fields */ }
Expand description

A running Byteflow runtime: worker pool + timer thread over one shared Chunk.

Owns M:N scheduling for flows (spawn, yield, sleep, mailboxes, FlowCap resolution, supervised restarts). Optional trace JIT when built with feature = "jit" and enabled in RuntimeConfig::jit.

Construct with Runtime::new (no natives) or Runtime::with_natives when the chunk uses CallNative / crate::std_native_table.

Implementations§

Source§

impl Runtime

Source

pub fn new(chunk: Chunk) -> Result<Self, SpawnError>

Convenience constructor for chunks that never call out through Opcode::CallNative. Equivalent to Runtime::with_natives(chunk, NativeTable::empty()).

Returns SpawnError instead of panicking: verify failures and OS thread-spawn refusals are category-A errors (see docs::error_model).

Source

pub fn with_natives( chunk: Chunk, natives: Arc<NativeTable>, ) -> Result<Self, SpawnError>

Construct a runtime whose flows can call into natives via Opcode::CallNative — the host FFI boundary.

Source

pub fn with_config( chunk: Chunk, config: RuntimeConfig, ) -> Result<Self, SpawnError>

Source

pub fn with_attested( bytes: &[u8], expected_digest: [u8; 32], natives: Arc<NativeTable>, config: RuntimeConfig, ) -> Result<Self, SpawnError>

Decode a .bf buffer only if its integrity fingerprint matches expected_digest, then verify and start the runtime.

Digest mismatch / decode failure → SpawnError::Attestation. Semantic verify failure → SpawnError::VerifyFailed.

Source

pub fn with_std_natives_and_config( chunk: Chunk, config: RuntimeConfig, ) -> Result<Self, SpawnError>

Like Runtime::with_config but wires crate::std_native_table_with using RuntimeConfig::output for the print native.

Source

pub fn with_natives_and_config( chunk: Chunk, natives: Arc<NativeTable>, config: RuntimeConfig, ) -> Result<Self, SpawnError>

Verify chunk, spawn the worker pool + timer thread, and return a live Runtime.

Failures here mean the runtime was never started (no orphan threads): either the bytecode is invalid (SpawnError::VerifyFailed) or the OS refused a thread (SpawnError::ThreadSpawnFailed). Partial spawn failure rolls back: already-started workers/timer are shut down and joined before the error is returned.

Source

pub fn spawn( &self, function: u32, args: &[Value], ) -> Result<FlowHandle, SpawnError>

Spawn a top-level flow starting at function in this runtime’s chunk, returning a FlowHandle the caller can .join().

Returns SpawnError::BadFunction if function is out of range.

Source

pub fn spawner(&self) -> RuntimeSpawner

A cheap, Send + Sync handle that can spawn flows into this runtime from any thread, independent of Runtime’s own lifetime bookkeeping (worker JoinHandles). Used by super::supervisor::Supervisor.

Source

pub fn supervisor(&self) -> Result<Supervisor, SpawnError>

A super::supervisor::Supervisor bound to this runtime, ready to take supervised children (design notes §15).

Source

pub fn function_index(&self, name: &str) -> Option<u32>

Look up a function by name in the runtime’s chunk — convenience for callers that built their chunk with crate::Program and don’t want to thread raw indices through their own code.

Source

pub fn metrics(&self) -> RuntimeMetricsSnapshot

Source

pub fn live_flows(&self) -> usize

Approximate number of flows currently registered in the directory.

Sharded sum without a global snapshot — fine for dashboards (Self::metrics), not for admission control (see [super::flow_limit::FlowLimit] / max_flows).

Source

pub fn memory_snapshot(&self) -> MemorySnapshot

Process-wide heap accounting snapshot (max_runtime_bytes ceiling).

Source

pub fn worker_count(&self) -> usize

Source

pub fn send(&self, target: FlowId, message: Value) -> Result<(), SendError>

Deliver an Atomic Hop (Value::Message) to target from the embedder (not from bytecode).

§Host trust boundary

This path takes a FlowId directly — no Cap required. The host is trusted; bytecode must use Value::Cap via Opcode::Send / Ask. The hop still goes through the same authentication choke-point as bytecode: sender is stamped FlowId::HOST (0), request_id is minted when unset, and payload Caps are reissued to the recipient. The host has no mailbox, so reply_cap is crate::bytecode::CapId::NONE. Bare scalars are rejected (SendError::NotAHop).

Source

pub fn mint_cap(&self, flow: FlowId) -> Result<CapId, LifecycleError>

Mint an addressing Cap (CapRights::ADDRESSING) for a live flow (host equivalent of SelfPid).

Source

pub fn grant_cap( &self, holder: FlowId, target: FlowId, ) -> Result<CapId, LifecycleError>

Mint a Cap held by holder targeting target (host introduction).

Source

pub fn monitor( &self, owner: FlowId, target: FlowId, ) -> Result<MonitorRef, LifecycleError>

Watch target; when it exits, owner receives a crate::TAG_SYS_DOWN hop.

Both flows must be live. owner == target is [LifecycleError::SelfRelation].

Source

pub fn demonitor( &self, owner: FlowId, monitor: MonitorRef, ) -> Result<(), LifecycleError>

Drop monitor if owner still owns it.

Bidirectional link. Without trap_exit, abnormal exit of either side kills the peer (Normal only drops the link). With Self::set_trap_exit on a peer, that peer receives a crate::TAG_SYS_EXIT hop for every linked exit (including Normal).

Source

pub fn set_trap_exit( &self, id: FlowId, enabled: bool, ) -> Result<(), LifecycleError>

BEAM-style process_flag(trap_exit, …).

When enabled, linked exits deliver a crate::TAG_SYS_EXIT hop (including Normal) instead of killing this flow.

Drop link if owner is one of the endpoints.

Source

pub fn register_name( &self, name: &str, cap: CapId, ) -> Result<(), LifecycleError>

Bind name to a live Cap (address). Names are swept when that flow exits.

Source

pub fn whereis(&self, name: &str) -> Result<Option<CapId>, LifecycleError>

Look up a registered Cap, or None if the name is free / was swept.

Source

pub fn kill(&self, id: FlowId) -> Result<(), LifecycleError>

Cooperative abort. Parked flows finalize immediately; a running flow dies at the next quantum with super::monitor::FlowExitReason::Killed.

Source

pub fn mint_admin_cap(&self, holder: FlowId) -> Result<CapId, LifecycleError>

Mint a scheduler ADMIN cap for a live flow (host introduction).

Source

pub fn admin_kill( &self, holder: FlowId, cap: CapId, target: FlowId, ) -> Result<(), LifecycleError>

Kill target only if holder presents a live ADMIN scheduler cap.

Source

pub fn admin_top_up_cpu( &self, holder: FlowId, cap: CapId, target: FlowId, extra: i64, ) -> Result<(), LifecycleError>

Top up target’s CPU budget. Requires a live ADMIN scheduler cap.

Source

pub fn admin_top_up_mem( &self, holder: FlowId, cap: CapId, target: FlowId, extra: usize, ) -> Result<(), LifecycleError>

Raise target’s heap limit. Requires a live ADMIN scheduler cap.

Source

pub fn admin_top_up_send( &self, holder: FlowId, cap: CapId, target: FlowId, extra: i64, ) -> Result<(), LifecycleError>

Credit target’s send bucket. Requires a live ADMIN scheduler cap.

Source

pub fn unregister_name(&self, name: &str) -> Result<bool, LifecycleError>

Remove a name without waiting for the flow to exit. false if unknown.

Source

pub fn reload_chunk(&mut self, chunk: Chunk) -> Result<(), SpawnError>

Replace the image used by new host Self::spawn calls and drop JIT traces. Live flows and bytecode Spawn keep the parent’s existing Vm chunk.

Source

pub fn shutdown(self)

Stop accepting new scheduling work, join every worker + the timer thread, then finalize any flow still parked (mailbox, timer, injector, worker deques). Joiners see FlowOutcome::Failed (runtime shutdown) instead of super::error::RuntimeError::Abandoned.

Mid-quantum bytecode is not aborted mid-instruction — the worker finalizes at the next loop check (cooperative preemption).

Trait Implementations§

Source§

impl Drop for Runtime

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.