Skip to main content

bb_ops/syscalls/triggers/
after.rs

1//! `After` syscall - delays a Trigger by `delay_ns`.
2//!
3//! Schedules a `TimerKind::After { key }` on `ctx.time.scheduler` and
4//! returns `Async(cmd)` so the engine routes the delayed Trigger
5//! through `handle_completion` after maturity.
6
7use bb_ir::proto::onnx::NodeProto;
8use bb_runtime::atomic::DispatchResult;
9use bb_runtime::bus::OpError;
10use bb_runtime::framework::TimerKind;
11use bb_runtime::runtime::RuntimeResourceRef;
12use bb_runtime::slot_value::SlotValue;
13
14/// Marker struct.
15pub struct AfterOp;
16
17/// Op domain.
18pub const DOMAIN: &str = "ai.bytesandbrains.syscall";
19/// Op type name.
20pub const OP_TYPE: &str = "After";
21
22/// Invoke fn - schedules a delayed Trigger.
23pub fn invoke(
24    node: &NodeProto,
25    _inputs: &[(&str, &dyn SlotValue)],
26    ctx: &mut RuntimeResourceRef<'_>,
27) -> Result<DispatchResult, OpError> {
28    let delay_ns = node
29        .attribute
30        .iter()
31        .find(|a| a.name == "delay_ns")
32        .map(|a| a.i as u64)
33        .unwrap_or(0);
34    let now = ctx.time.scheduler.now_ns();
35    let cmd = ctx.allocate_command_id();
36    ctx.time.scheduler.schedule(
37        now.saturating_add(delay_ns),
38        TimerKind::After { key: cmd.as_u64() },
39    );
40    Ok(DispatchResult::Async(cmd))
41}
42
43use bb_runtime::registry::OpRegistration as _BbOpsSyscallReg;
44
45inventory::submit! {
46    _BbOpsSyscallReg {
47        domain: DOMAIN,
48        op_type: OP_TYPE,
49        invoke,
50        kind: bb_runtime::registry::RegistrationKind::Syscall,
51    }
52}