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
impl Runtime
Sourcepub fn new(chunk: Chunk) -> Result<Self, SpawnError>
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).
Sourcepub fn with_natives(
chunk: Chunk,
natives: Arc<NativeTable>,
) -> Result<Self, SpawnError>
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.
pub fn with_config( chunk: Chunk, config: RuntimeConfig, ) -> Result<Self, SpawnError>
Sourcepub fn with_attested(
bytes: &[u8],
expected_digest: [u8; 32],
natives: Arc<NativeTable>,
config: RuntimeConfig,
) -> Result<Self, SpawnError>
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.
Sourcepub fn with_std_natives_and_config(
chunk: Chunk,
config: RuntimeConfig,
) -> Result<Self, SpawnError>
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.
Sourcepub fn with_natives_and_config(
chunk: Chunk,
natives: Arc<NativeTable>,
config: RuntimeConfig,
) -> Result<Self, SpawnError>
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.
Sourcepub fn spawn(
&self,
function: u32,
args: &[Value],
) -> Result<FlowHandle, SpawnError>
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.
Sourcepub fn spawner(&self) -> RuntimeSpawner
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.
Sourcepub fn supervisor(&self) -> Result<Supervisor, SpawnError>
pub fn supervisor(&self) -> Result<Supervisor, SpawnError>
A super::supervisor::Supervisor bound to this runtime, ready to
take supervised children (design notes §15).
Sourcepub fn function_index(&self, name: &str) -> Option<u32>
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.
pub fn metrics(&self) -> RuntimeMetricsSnapshot
Sourcepub fn live_flows(&self) -> usize
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).
Sourcepub fn memory_snapshot(&self) -> MemorySnapshot
pub fn memory_snapshot(&self) -> MemorySnapshot
Process-wide heap accounting snapshot (max_runtime_bytes ceiling).
pub fn worker_count(&self) -> usize
Sourcepub fn send(&self, target: FlowId, message: Value) -> Result<(), SendError>
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).
Sourcepub fn mint_cap(&self, flow: FlowId) -> Result<CapId, LifecycleError>
pub fn mint_cap(&self, flow: FlowId) -> Result<CapId, LifecycleError>
Mint an addressing Cap (CapRights::ADDRESSING) for a live flow
(host equivalent of SelfPid).
Sourcepub fn grant_cap(
&self,
holder: FlowId,
target: FlowId,
) -> Result<CapId, LifecycleError>
pub fn grant_cap( &self, holder: FlowId, target: FlowId, ) -> Result<CapId, LifecycleError>
Mint a Cap held by holder targeting target (host introduction).
Sourcepub fn monitor(
&self,
owner: FlowId,
target: FlowId,
) -> Result<MonitorRef, LifecycleError>
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].
Sourcepub fn demonitor(
&self,
owner: FlowId,
monitor: MonitorRef,
) -> Result<(), LifecycleError>
pub fn demonitor( &self, owner: FlowId, monitor: MonitorRef, ) -> Result<(), LifecycleError>
Drop monitor if owner still owns it.
Sourcepub fn link(&self, a: FlowId, b: FlowId) -> Result<LinkId, LifecycleError>
pub fn link(&self, a: FlowId, b: FlowId) -> Result<LinkId, LifecycleError>
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).
Sourcepub fn set_trap_exit(
&self,
id: FlowId,
enabled: bool,
) -> Result<(), LifecycleError>
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.
Sourcepub fn unlink(&self, owner: FlowId, link: LinkId) -> Result<(), LifecycleError>
pub fn unlink(&self, owner: FlowId, link: LinkId) -> Result<(), LifecycleError>
Drop link if owner is one of the endpoints.
Sourcepub fn register_name(
&self,
name: &str,
cap: CapId,
) -> Result<(), LifecycleError>
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.
Sourcepub fn whereis(&self, name: &str) -> Result<Option<CapId>, LifecycleError>
pub fn whereis(&self, name: &str) -> Result<Option<CapId>, LifecycleError>
Look up a registered Cap, or None if the name is free / was swept.
Sourcepub fn kill(&self, id: FlowId) -> Result<(), LifecycleError>
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.
Sourcepub fn mint_admin_cap(&self, holder: FlowId) -> Result<CapId, LifecycleError>
pub fn mint_admin_cap(&self, holder: FlowId) -> Result<CapId, LifecycleError>
Mint a scheduler ADMIN cap for a live flow (host introduction).
Sourcepub fn admin_kill(
&self,
holder: FlowId,
cap: CapId,
target: FlowId,
) -> Result<(), LifecycleError>
pub fn admin_kill( &self, holder: FlowId, cap: CapId, target: FlowId, ) -> Result<(), LifecycleError>
Kill target only if holder presents a live ADMIN scheduler cap.
Sourcepub fn admin_top_up_cpu(
&self,
holder: FlowId,
cap: CapId,
target: FlowId,
extra: i64,
) -> Result<(), LifecycleError>
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.
Sourcepub fn admin_top_up_mem(
&self,
holder: FlowId,
cap: CapId,
target: FlowId,
extra: usize,
) -> Result<(), LifecycleError>
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.
Sourcepub fn admin_top_up_send(
&self,
holder: FlowId,
cap: CapId,
target: FlowId,
extra: i64,
) -> Result<(), LifecycleError>
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.
Sourcepub fn unregister_name(&self, name: &str) -> Result<bool, LifecycleError>
pub fn unregister_name(&self, name: &str) -> Result<bool, LifecycleError>
Remove a name without waiting for the flow to exit. false if unknown.
Sourcepub fn reload_chunk(&mut self, chunk: Chunk) -> Result<(), SpawnError>
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.
Sourcepub fn shutdown(self)
pub fn shutdown(self)
Stop accepting new scheduling work and join every worker + the timer thread. Processes that are mid-quantum are allowed to reach their next natural suspension point; this does not forcibly abort running bytecode (there is no safe way to do that to an OS thread mid-instruction — see design notes §11 on why preemption here is cooperative/budgeted rather than signal-based).