A naive scheduler iterates over all tasks to find the highest priority runnable task. That's O(n) — unacceptable for a deterministic RTOS where worst-case scheduling time must be bounded regardless of task count.
O(1) scheduling uses a priority bitmap: one bit per priority level, set when a task at that priority is runnable. Finding the next task is a single find-first-set-bit (FFS) instruction — clz (count leading zeros) on Cortex-M, cpop-adjacent on RISC-V. One instruction regardless of how many tasks exist.
struct Scheduler {
ready_mask: u32, // bit N = priority N task is runnable
tasks: [Option<Task>; 32],
}
impl Scheduler {
fn next_task(&self) -> Option<usize> {
if self.ready_mask == 0 { return None; }
// count_leading_zeros gives highest set bit
let priority = 31 - self.ready_mask.leading_zeros() as usize;
Some(priority)
}
}The constraint: you need one task slot per priority level. Cerberus-OS uses 32 priority levels (fits in u32). Linux's O(1) scheduler (pre-CFS) used 140 levels with two 5-word bitmaps.
Combined with the priority inheritance mutex implementation, this gives Cerberus-OS worst-case scheduling time that is independent of the number of tasks — which is the definition of schedulability for hard real-time systems.