Skip to content

kernel::timer

Timer subsystem: sleep and one-shot/periodic callbacks (plan W1, W3.5).

sleep_ms blocks the current task until the target tick, then requests a switch via the software interrupt. once/every register software timers whose callbacks fire from the trap handler. Software-timer state is guarded by a critical section against the trap handler.

struct TimerEntry {
pub id: u32,
pub fire_at: u64,
pub interval: u32,
pub callback: Option<fn(..)>,
}
fn cancel(id: u32)

Cancel a timer by id.

fn every(ms: u32, callback: fn(..)) -> u32

Register a periodic timer. Returns the timer id, or 0 if the table is full.

fn once(ms: u32, callback: fn(..)) -> u32

Register a one-shot timer. Returns the timer id, or 0 if the table is full.

fn process_timers(now: u64)

Fire any due timers. Called from the trap handler, outside the scheduler lock, taking the timer lock itself for each table access.

No &mut into the static TIMERS array is ever held across a callback invocation (item 6). A callback can therefore call once, cancel, or every without aliasing the table or re-entering its lock.

fn sleep_ms(ms: u32)

Sleep the current task for ms milliseconds.

Blocking, so it must never be called from trap context (a top-half ISR or a timer callback) — that would suspend whichever task was interrupted, not “the caller”, wedging it forever (item 11). sleep_ms has no way to return an error to its ()-returning caller, and silently returning without sleeping would be exactly the “plausible-looking wrong result” the project’s philosophy forbids, so misuse is a loud panic instead.