Skip to main content

bb_ops/syscalls/clock_rng/
deadline_match.rs

1//! `DeadlineMatch` syscall - first-arrival selector between a
2//! `then` trigger and a `timeout` trigger.
3
4use bb_ir::proto::onnx::NodeProto;
5use bb_runtime::atomic::DispatchResult;
6use bb_runtime::bus::OpError;
7use bb_runtime::runtime::RuntimeResourceRef;
8use bb_runtime::slot_value::SlotValue;
9use bb_runtime::syscall::values::TriggerValue;
10
11/// Marker struct.
12pub struct DeadlineMatchOp;
13
14/// Op domain.
15pub const DOMAIN: &str = "ai.bytesandbrains.syscall";
16/// Op type name.
17pub const OP_TYPE: &str = "DeadlineMatch";
18
19/// Invoke fn - emits a single `winner` Trigger per IR_AND_DSL.md
20/// §5a. First-to-fire semantics: whichever of `then` / `timeout`
21/// has a non-empty slot becomes the winner. Once a winner is
22/// determined, the per-Op latch absorbs subsequent invocations so
23/// downstream consumers don't double-fire.
24pub fn invoke(
25    _node: &NodeProto,
26    inputs: &[(&str, &dyn SlotValue)],
27    ctx: &mut RuntimeResourceRef<'_>,
28) -> Result<DispatchResult, OpError> {
29    if inputs.is_empty() {
30        return Err(OpError {
31            detail: "DeadlineMatch requires at least one input".to_string(),
32            ..Default::default()
33        });
34    }
35    // Latch on first fire per execution. Keyed by (OpRef, ExecId)
36    // so the same op fires once per logical execution rather than
37    // once per Node lifetime.
38    let latch_key = (ctx.current.op_ref.as_u64(), ctx.current.exec_id.as_u64());
39    if !ctx.syscall.deadline_match_fired.insert(latch_key) {
40        return Ok(DispatchResult::Immediate(vec![]));
41    }
42    Ok(DispatchResult::Immediate(vec![(
43        "winner".to_string(),
44        Box::new(TriggerValue),
45    )]))
46}
47
48use bb_runtime::registry::OpRegistration as _BbOpsSyscallReg;
49
50inventory::submit! {
51    _BbOpsSyscallReg {
52        domain: DOMAIN,
53        op_type: OP_TYPE,
54        invoke,
55        kind: bb_runtime::registry::RegistrationKind::Syscall,
56    }
57}