Linux 內核的等待隊列(wait queue)建立在雙向循環鏈表之上,與進程調度機制深度耦合,是實作「事件發生後再通知」這類非同步機制的基礎零件。在 Linux 2.4.21 的程式碼樹中,相關定義位於 include/linux/wait.h,本質上就是用 list_head 串起來的一條雙向循環鏈表。
這條鏈表上掛著兩種角色:等待隊列頭 wait_queue_head_t 與等待隊列項 wait_queue_t。兩者都內嵌一個 list_head 型別的欄位充當「連接件」。由於對這套基礎設施的操作只有插入與刪除,並不會改動等待隊列項本身的內容,因此只需一把鎖保護全部結構即可,這把鎖放在等待隊列頭中,型別為 wq_lock_t。實作上可透過巨集在讀寫鎖(rwlock)與自旋鎖(spinlock)之間切換:使用讀寫鎖時把 wq_lock_t 定義為 rwlock_t,使用自旋鎖時定義為 spinlock_t。無論採用哪一種,都會相應地定義 wq_read_lock、wq_read_unlock、wq_read_lock_irqsave、wq_read_unlock_irqrestore、wq_write_lock_irq、wq_write_unlock、wq_write_lock_irqsave、wq_write_unlock_irqrestore 這一族巨集。
struct __wait_queue_head {
spinlock_t lock;
struct list_head task_list;
};
typedef struct __wait_queue_head wait_queue_head_t;
在內核裡,等待隊列可謂無所不在:中斷處理、進程同步、定時等待等場合都能見到它的身影。它以隊列為底層結構,與調度器緊密咬合,一方面用來實作「阻塞進程的喚醒」,另一方面承擔核心的異步事件通知,以及對系統資源的同步存取。
在操作 task_list 的過程中,靠這把鎖保證等待隊列的互斥存取。
雙向循環鏈表,節點即為等待中的進程。
寫法一,先定義再初始化:
wait_queue_head_t my_queue; init_waitqueue_head(&my_queue);
init_waitqueue_head() 會把自旋鎖設為未鎖狀態,並把等待隊列初始化為空的雙向循環鏈表。
寫法二,一步到位:
DECLARE_WAIT_QUEUE_HEAD(my_queue);
效果等同於上面的兩行。
接著定義等待隊列項本身:
DECLARE_WAITQUEUE(name, tsk);
注意這裡定義的是一個 wait_queue_t 型別的變數 name,並把它的 private 指向 tsk。wait_queue_t 的定義如下:
struct __wait_queue {
unsigned int flags;
void *private;
wait_queue_func_t func;
struct list_head task_list;
};
flags 用來標明該等待項是互斥還是共用:0 代表非互斥進程,WQ_FLAG_EXCLUSIVE(0x01)代表互斥進程。等待隊列項與等待隊列頭的關係很簡單——前者是後者的成員,也就是隊列頭的 task_list 所串起來的節點,正是這些 wait_queue_t。
void fastcall add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
{
unsigned long flags;
wait->flags &= ~WQ_FLAG_EXCLUSIVE;
spin_lock_irqsave(&q->lock, flags);
__add_wait_queue(q, wait);
spin_unlock_irqrestore(&q->lock, flags);
}
這段程式碼先把等待項標記為非互斥,再插入到隊列頭 q 的頭端。
void fastcall add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
{
unsigned long flags;
wait->flags |= WQ_FLAG_EXCLUSIVE;
spin_lock_irqsave(&q->lock, flags);
__add_wait_queue_tail(q, wait);
spin_unlock_irqrestore(&q->lock, flags);
}
功能幾乎相同,差別在於它把等待項設為互斥,並且掛到鏈表尾端。
void fastcall remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
{
unsigned long flags;
spin_lock_irqsave(&q->lock, flags);
__remove_wait_queue(q, wait);
spin_unlock_irqrestore(&q->lock, flags);
}
當等待的資源或事件條件成立、進程被喚醒之後,就用這個函數把它從隊列頭中摘除。
#define wait_event(wq, condition) \
do { \
if (condition) \
break; \
__wait_event(wq, condition); \
} while (0)
#define __wait_event_timeout(wq, condition, ret) \
do { \
DEFINE_WAIT(__wait); \
for (;;) { \
prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
if (condition) \
break; \
ret = schedule_timeout(ret); \
if (!ret) \
break; \
} \
finish_wait(&wq, &__wait); \
} while (0)
wait_event 的語意是:在隊列上睡到 condition 成立為止。等待期間進程被置為 TASK_UNINTERRUPTIBLE,而且每次被喚醒都會重新檢查一次條件。
wait_event_interruptible() 與它的差別在於睡眠時狀態是 TASK_INTERRUPTIBLE。每次被喚醒先判斷條件是否成立,成立則回傳 0;若是被信號喚醒,則回傳 -ERESTARTSYS。
wait_event_timeout() 多了一個睡眠上限:若傳入的時間為負數會立刻返回;睡眠中被喚醒且條件成立時回傳剩餘時間,否則繼續睡到時間用盡,最後回傳 0。
wait_event_interruptible_timeout() 相當於兩者相加,若睡眠期間被信號打斷則回傳 ERESTARTSYS。
wait_event_interruptible_exclusive() 行為同 wait_event_interruptible(),只是睡眠的進程被標記為互斥進程。
#define wake_up(x) __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1, NULL)
void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,
int nr_exclusive, void *key)
{
unsigned long flags;
spin_lock_irqsave(&q->lock, flags);
__wake_up_common(q, mode, nr_exclusive, 0, key);
spin_unlock_irqrestore(&q->lock, flags);
}
static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
int nr_exclusive, int sync, void *key)
{
struct list_head *tmp, *next;
list_for_each_safe(tmp, next, &q->task_list) {
wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list);
unsigned flags = curr->flags;
if (curr->func(curr, mode, sync, key) &&
(flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
break;
}
}
wake_up() 可喚醒 TASK_INTERRUPTIBLE 與 TASK_UNINTERRUPTIBLE 兩種狀態的進程,通常與 wait_event/wait_event_timeout 成對出現。
#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
它與 wake_up() 唯一的差別,是只喚醒 TASK_INTERRUPTIBLE 狀態的進程,搭配 wait_event_interruptible 系列使用。
#define wake_up_all(x) __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 0, NULL) #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL) #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
這幾個變體的邏輯與前面一致,只是把喚醒範圍或數量參數換掉。
void __sched sleep_on(wait_queue_head_t *q)
{
unsigned long flags;
wait_queue_t wait;
init_waitqueue_entry(&wait, current);
current->state = TASK_UNINTERRUPTIBLE;
sleep_on_head(q, &wait, &flags);
schedule();
sleep_on_tail(q, &wait, &flags);
}
它先定義一個等待項並把當前進程掛上去,狀態設為 TASK_UNINTERRUPTIBLE,加入隊列頭 q 後交出 CPU;直到資源可用被喚醒,才從隊列頭移出。等待期間該進程無法被信號喚醒。
long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
{
unsigned long flags;
wait_queue_t wait;
init_waitqueue_entry(&wait, current);
current->state = TASK_UNINTERRUPTIBLE;
sleep_on_head(q, &wait, &flags);
timeout = schedule_timeout(timeout);
sleep_on_tail(q, &wait, &flags);
return timeout;
}
與 sleep_on() 的差別在於多了時間上限,靠 schedule_timeout() 實作;若傳入的 timeout 小於 0 則完全不睡。回傳值是實際睡眠的時間。
void __sched interruptible_sleep_on(wait_queue_head_t *q)
{
unsigned long flags;
wait_queue_t wait;
init_waitqueue_entry(&wait, current);
current->state = TASK_INTERRUPTIBLE;
sleep_on_head(q, &wait, &flags);
schedule();
sleep_on_tail(q, &wait, &flags);
}
與 sleep_on() 唯一的差別是把狀態改成 TASK_INTERRUPTIBLE,因此睡眠中收到信號會被喚醒。
long __sched interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
{
unsigned long flags;
wait_queue_t wait;
init_waitqueue_entry(&wait, current);
current->state = TASK_INTERRUPTIBLE;
sleep_on_head(q, &wait, &flags);
timeout = schedule_timeout(timeout);
sleep_on_tail(q, &wait, &flags);
return timeout;
}
性質等同 sleep_on_timeout(),只是喚醒來源可能是信號,也可能是時間到。
以上四個函數都是讓進程在等待隊列上睡覺,差異只在「能否被信號打斷」與「有沒有超時」。實務上依情境挑選即可:例如讀寫軟碟時若裝置未就緒,就用 sleep_on() 睡到資料可讀可寫;開啟串口時若埠尚未開啟,可用 interruptible_sleep_on() 等待;音效驅動在沒有資料可讀時,則通常帶超時地等待一段時間。