Wait Queue Internals¶
A wait queue is the fundamental Linux kernel mechanism used to put tasks to sleep and wake them when an event occurs.
Many kernel subsystems are built on top of wait queues, including:
- Blocking I/O
poll()- Completions
- Event-driven drivers
- Various kernel worker mechanisms
Motivation¶
A task should not continuously poll for an event.
Inefficient approach:
Problems:
- Wastes CPU cycles
- Poor scalability
- Increases power consumption
A wait queue allows a task to sleep until the required condition becomes true.
Core Components¶
wait_queue_head_t¶
Represents a wait queue.
Conceptually:
Simplified structure:
Responsibilities:
- Maintain waiting tasks
- Protect wait queue state
- Support wake-up operations
wait_queue_entry_t¶
Represents a single waiting task.
Simplified structure:
Responsibilities:
- Track a waiting task
- Link into a wait queue
Task States¶
Wait queues work together with task states.
Common states:
TASK_RUNNING¶
The task is runnable.
The scheduler may execute it immediately or later.
TASK_INTERRUPTIBLE¶
The task is sleeping.
The task may be awakened by:
- Events
- Signals
- Explicit wake-up operations
Sleep Flow¶
The general sleep sequence:
prepare_to_wait()¶
Responsibilities:
Conceptually:
After execution:
and:
Wake-up Flow¶
The wake-up sequence:
wake_up()¶
Responsibilities:
Conceptually:
finish_wait()¶
Responsibilities:
Conceptually:
wait_event()¶
The most common wait queue API used by drivers.
Typical usage:
Conceptually:
Equivalent logic:
Blocking Driver Pattern¶
One of the most common wait queue applications.
Driver read path:
Interrupt path:
Read resumes:
Relationship with poll()¶
A driver using blocking I/O often also supports poll().
Relationship:
The wait queue remains the underlying notification mechanism.
Relationship with Completion¶
Completions are built on top of wait queues.
Conceptually:
Completions provide a higher-level synchronization abstraction.
Design Pattern¶
The wait queue pattern appears repeatedly in Linux:
Examples:
- Blocking drivers
- Sensor drivers
- Network receive paths
- Completion synchronization
- Event-driven subsystems
Summary¶
A wait queue is the kernel's generic sleep/wakeup infrastructure.
Core ideas:
Understanding wait queues is essential before learning:
poll()epoll()- Completions
- Advanced driver synchronization