Skip to main content

FlowHandle

Struct FlowHandle 

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

A reference to a spawned flow, returned by super::runtime::Runtime::spawn.

Holding a FlowHandle does not keep the flow alive — it is a way to (a) read its FlowId for addressing with Send, and (b) block the calling native thread until it finishes via FlowHandle::join. Dropping without joining is fine (fire-and-forget).

Implementations§

Source§

impl FlowHandle

Source

pub fn id(&self) -> FlowId

Source

pub fn join(self) -> FlowOutcome

Block the current (native) thread until the flow terminates. Never call from inside a worker / from bytecode.

Returns FlowOutcome::Failed rather than blocking forever if the flow was destroyed without producing an outcome — most commonly Runtime::shutdown while the flow was suspended, since shutdown does not drain flows out of the timer or the worker deques. The message comes from super::error::RuntimeError::Abandoned, so it is distinguishable from a flow that genuinely faulted.

Examples found in repository?
examples/jit_loop.rs (line 41)
26fn run_once(jit: bool) -> Result<(), Box<dyn std::error::Error>> {
27    let chunk = loop_chunk(1_000_000);
28    let rt = Runtime::with_config(
29        chunk,
30        RuntimeConfig {
31            workers: 1,
32            quantum: 50_000_000,
33            jit: JitConfig {
34                enabled: jit,
35                hot_threshold: 1,
36            },
37            ..Default::default()
38        },
39    )?;
40    let start = Instant::now();
41    let outcome = rt.spawn(0, &[])?.join();
42    let snapshot = rt.metrics();
43    rt.shutdown();
44    let elapsed = start.elapsed();
45    match outcome {
46        FlowOutcome::Completed(Value::Int(n)) => {
47            println!("jit={jit} result={n} elapsed={elapsed:?} metrics={snapshot}");
48            Ok(())
49        }
50        other => Err(format!("unexpected outcome: {other:?}").into()),
51    }
52}
More examples
Hide additional examples
examples/ping_pong.rs (line 68)
40fn main() {
41    let rt = match Runtime::with_natives_and_config(
42        ping_pong_chunk(),
43        std_native_table(),
44        RuntimeConfig {
45            workers: 1,
46            quantum: DEFAULT_QUANTUM,
47            mailbox: MailboxConfig::DEFAULT,
48            ..Default::default()
49        },
50    ) {
51        Ok(rt) => rt,
52        Err(e) => {
53            eprintln!("runtime: {e}");
54            std::process::exit(1);
55        }
56    };
57    let Some(main_fn) = rt.function_index("main") else {
58        eprintln!("missing main");
59        std::process::exit(1);
60    };
61    let handle = match rt.spawn(main_fn, &[]) {
62        Ok(h) => h,
63        Err(e) => {
64            eprintln!("spawn: {e}");
65            std::process::exit(1);
66        }
67    };
68    let outcome = handle.join();
69    let metrics = rt.metrics();
70    rt.shutdown();
71
72    match outcome {
73        FlowOutcome::Completed(Value::Int(2)) => {
74            println!("pong replied 2 (Atomic Hop)");
75            println!("{metrics}");
76        }
77        other => {
78            eprintln!("unexpected {other:?}");
79            std::process::exit(1);
80        }
81    }
82}
examples/atomic_actors.rs (line 100)
72fn main() {
73    let rt = match Runtime::with_natives_and_config(
74        atomic_actors_chunk(),
75        std_native_table(),
76        RuntimeConfig {
77            workers: 2,
78            quantum: DEFAULT_QUANTUM,
79            mailbox: MailboxConfig::DEFAULT,
80            ..Default::default()
81        },
82    ) {
83        Ok(rt) => rt,
84        Err(e) => {
85            eprintln!("runtime: {e}");
86            std::process::exit(1);
87        }
88    };
89    let Some(main_fn) = rt.function_index("main") else {
90        eprintln!("missing main");
91        std::process::exit(1);
92    };
93    let handle = match rt.spawn(main_fn, &[]) {
94        Ok(h) => h,
95        Err(e) => {
96            eprintln!("spawn: {e}");
97            std::process::exit(1);
98        }
99    };
100    let outcome = handle.join();
101    let metrics = rt.metrics();
102    rt.shutdown();
103
104    match outcome {
105        FlowOutcome::Completed(Value::Int(72)) => {
106            println!("atomic actors ok: two clients × 8 Ask = 72");
107            println!("{metrics}");
108        }
109        other => {
110            eprintln!("unexpected {other:?}");
111            std::process::exit(1);
112        }
113    }
114}
examples/throughput.rs (line 71)
24fn main() {
25    let n: u32 = match std::env::args().nth(1) {
26        Some(s) => match s.parse() {
27            Ok(v) => v,
28            Err(_) => 50_000,
29        },
30        None => 50_000,
31    };
32
33    let workers = match std::thread::available_parallelism() {
34        Ok(p) => p.get(),
35        Err(_) => 1,
36    };
37
38    let rt = match Runtime::with_config(
39        trivial_chunk(),
40        RuntimeConfig {
41            workers,
42            quantum: DEFAULT_QUANTUM,
43            mailbox: MailboxConfig::DEFAULT,
44            ..Default::default()
45        },
46    ) {
47        Ok(rt) => rt,
48        Err(e) => {
49            eprintln!("runtime: {e}");
50            std::process::exit(1);
51        }
52    };
53    let Some(worker_fn) = rt.function_index("worker") else {
54        eprintln!("missing worker");
55        std::process::exit(1);
56    };
57
58    let start = Instant::now();
59    let mut handles = Vec::with_capacity(n as usize);
60    for _ in 0..n {
61        match rt.spawn(worker_fn, &[]) {
62            Ok(h) => handles.push(h),
63            Err(e) => {
64                eprintln!("spawn: {e}");
65                std::process::exit(1);
66            }
67        }
68    }
69    let mut ok = 0u32;
70    for h in handles {
71        if matches!(h.join(), FlowOutcome::Completed(Value::Int(1))) {
72            ok += 1;
73        }
74    }
75    let elapsed = start.elapsed();
76    let metrics = rt.metrics();
77    rt.shutdown();
78
79    let secs = elapsed.as_secs_f64().max(1e-9);
80    println!("processes={ok}/{n}");
81    println!("workers={workers}");
82    println!("elapsed_ms={:.2}", elapsed.as_secs_f64() * 1000.0);
83    println!("spawns_per_sec={:.0}", ok as f64 / secs);
84    println!("{metrics}");
85}
Source

pub fn try_join(&self) -> Option<FlowOutcome>

The outcome if the flow has already terminated, None if it is still running. Never waits.

For embedders that drive their own loop and cannot surrender a thread to FlowHandle::join. Takes &self, so it can be polled until it answers; note that the outcome is handed out exactly once, and a further poll after that reports super::error::RuntimeError::AlreadyCollected as FlowOutcome::Failed rather than repeating it.

Source

pub fn join_timeout(&self, timeout: Duration) -> Option<FlowOutcome>

Wait up to timeout for the flow to terminate; None if it is still running when the bound elapses.

This is the variant to reach for in anything with a deadline — a control loop, a watchdog, a test harness — since it is the only join whose worst-case duration the caller chooses.

Source

pub fn join_deadline(&self, deadline: Instant) -> Option<FlowOutcome>

FlowHandle::join_timeout against an absolute deadline, for callers that already track one and must not have it drift across repeated waits.

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.