Skip to main content

nros/
transport_band.rs

1//! Where a hosted POSIX runner places zenoh-pico's transport tasks: the floor
2//! of the priority plan's `reserved.transport` band
3//! (`packages/boards/linux/nros-board.toml`, `[90, 99]`), above every sched task.
4//!
5//! ONE decision for both Linux runners, the Rust board's (`nros-board-linux`)
6//! and the C/C++ one (`nros-cpp`). Each reaches zenoh-pico's
7//! `zpico_set_task_config` its own way, because only the Rust board knows at
8//! compile time whether the zenoh backend is linked, so the CALL is the
9//! caller's and everything else is here.
10//!
11//! Why it is needed at all: a sched task with a priority runs `SCHED_FIFO`, and
12//! a `SCHED_FIFO` thread outranks every `SCHED_OTHER` thread unconditionally.
13//! A transport left on `SCHED_OTHER` is preempted by the app that publishes
14//! over it, always. `zpico_set_task_config` is a process-wide default read when
15//! the session opens and creates the transport tasks, so this must run BEFORE
16//! the open; after it, it changes nothing.
17
18use core::fmt;
19
20/// The floor of the plan's `reserved.transport` band. Cross-checked against
21/// the plan by `check-sched-task-priority-plan`, so the two cannot drift.
22pub const TRANSPORT_BAND_FLOOR: u32 = 90;
23
24/// Place the transport tasks at [`TRANSPORT_BAND_FLOOR`] when any sched task
25/// asks for a real-time priority (`max_sched_priority > 0`); with none, every
26/// thread is `SCHED_OTHER` and there is nothing to sit above.
27///
28/// `set_task_config(priority)` applies it and returns whether a transport was
29/// there to take it (false: no zenoh backend linked, which is not a
30/// degradation — such an image has no transport tasks to place). Best effort
31/// like every priority on this port: without `CAP_SYS_NICE` the attribute is
32/// ignored at spawn. Returns the priority placed.
33pub fn place(
34    max_sched_priority: i64,
35    set_task_config: impl FnOnce(u32) -> bool,
36    mut println: impl FnMut(fmt::Arguments<'_>),
37) -> Option<u32> {
38    if max_sched_priority <= 0 {
39        return None;
40    }
41    if !set_task_config(TRANSPORT_BAND_FLOOR) {
42        return None;
43    }
44    println(format_args!(
45        "nros: transport tasks at SCHED_FIFO {TRANSPORT_BAND_FLOOR} (reserved band floor; \
46         most urgent sched task is {max_sched_priority})"
47    ));
48    Some(TRANSPORT_BAND_FLOOR)
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn no_real_time_sched_task_places_nothing() {
57        let mut called = false;
58        assert_eq!(
59            place(
60                0,
61                |_| {
62                    called = true;
63                    true
64                },
65                |_| {}
66            ),
67            None
68        );
69        assert!(!called);
70    }
71
72    #[test]
73    fn a_real_time_sched_task_places_the_band_floor() {
74        let mut got = None;
75        assert_eq!(
76            place(
77                80,
78                |p| {
79                    got = Some(p);
80                    true
81                },
82                |_| {}
83            ),
84            Some(TRANSPORT_BAND_FLOOR)
85        );
86        assert_eq!(got, Some(TRANSPORT_BAND_FLOOR));
87    }
88
89    #[test]
90    fn no_transport_linked_places_nothing() {
91        assert_eq!(place(80, |_| false, |_| {}), None);
92    }
93}