Skip to main content

Module waitqueue

Module waitqueue 

Source
Expand description

Wait queue and condition variable for blocking/waking tasks.

§Overview

A WaitQueue holds a FIFO list of TaskIds waiting for an event. When a task calls WaitQueue::wait, it is blocked (removed from the scheduler’s ready queue) until another task calls WaitQueue::wake_one or WaitQueue::wake_all.

WaitQueue::wait_until is the preferred primitive: it atomically checks a caller-supplied condition before blocking, preventing the classical lost-wakeup race:

Incorrect (racy) pattern:
  if !condition() { queue.wait(); }   ← wakeup between check and wait is lost

Correct pattern (what wait_until does internally):
  loop {
      hold waiters lock
      check condition → return if true
      push self to waiters (lock still held)
      release waiters lock
      block_current_task()            ← wake_pending flag handles late wakeups
      // woken → re-check condition
  }

§Lost-wakeup guarantee

Even the simple WaitQueue::wait is safe: the scheduler’s wake_pending flag (set by wake_task() when the target is not yet in blocked_tasks) ensures that a wake_one() that races with the transition to Blocked state is never silently dropped.

§WaitCondition

A higher-level wrapper that stores the condition closure alongside the queue, inspired by Theseus’s wait_condition crate. Useful when the same condition is shared across multiple call sites.

Structs§

WaitCondition
A named condition variable backed by a WaitQueue.
WaitQueue
A FIFO queue of tasks waiting for an event.