Skip to main content

bb_ops/syscalls/clock_rng/
sleep.rs

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