Skip to main content

nros/
thread_name.rs

1//! What a sched task's THREAD is called, for every runner that spawns one.
2//!
3//! A kernel keeps few bytes of a thread name: Linux 15 plus the NUL, and
4//! FreeRTOS's default `configMAX_TASK_NAME_LEN` is 16. The POSIX port cuts a
5//! longer name there (glibc refuses it outright), so whatever tells two
6//! threads apart has to come first. A runner names a sched task's thread
7//! `nros-<label>`, with the label from [`label`](crate::thread_name::label).
8
9/// The prefix the priority derivation gives every sched task it synthesises
10/// (`derived-<node>`, `derived-level-<k>`). Every derived level carries it, so
11/// in a thread name it is eight bytes that tell no two levels apart.
12pub const DERIVED_PREFIX: &str = "derived-";
13
14/// The part of a sched task's name its thread is labelled with: the name, less
15/// [`DERIVED_PREFIX`]. A runner formats it as `nros-<label>`.
16pub fn label(sched_task: &str) -> &str {
17    sched_task
18        .strip_prefix(DERIVED_PREFIX)
19        .unwrap_or(sched_task)
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25
26    #[test]
27    fn a_derived_level_loses_the_prefix() {
28        assert_eq!(label("derived-urgent"), "urgent");
29        assert_eq!(label("derived-level-2"), "level-2");
30    }
31
32    #[test]
33    fn an_authored_sched_task_keeps_its_name() {
34        assert_eq!(label("high"), "high");
35        assert_eq!(label("derived"), "derived");
36    }
37}