等待佇列是核心同步基礎設施,支援任務在條件滿足前睡眠、條件達標後喚醒。核心兩大資料結構:
struct __wait_queue_head {
spinlock_t lock; // 保護佇列的自旋鎖
struct list_head task_list; // 掛載等待任務的鏈表頭
};
struct __wait_queue {
unsigned int flags;
void *private; // 通常指向 task_struct
wait_queue_func_t func; // 喚醒回調 (通常 autoremove_wake_function)
struct list_head task_list; // 掛入 wait_queue_head 的節點
};
DECLARE_WAIT_QUEUE_HEAD(name) 靜態初始化,或動態 init_waitqueue_head()。wait_event(wq, condition):task_struct 初始化 wait_queue;wake_up(wq) (喚醒一個) 或 wake_up_all(wq) (喚醒全部),將任務設為可執行並移出佇列。| 介面 | 功能 |
|---|---|
wait_event(wq, condition) | 睡眠等待條件滿足 (不可中斷) |
wait_event_timeout(wq, condition, timeout) | 帶超時機制 |
wait_event_interruptible(wq, condition) | 可被訊號中斷,返回 -ERESTARTSYS |
wait_event_interruptible_timeout(...) | 可中斷 + 超時 |
wake_up(wq) | 喚醒佇列中一個任務 |
wake_up_all(wq) | 喚醒佇列中所有任務 |
等待佇列廣泛應用於驅動程式、檔案系統、網路協定棧等核心子系統,理解其運作對核心開發與除錯至關重要。