Linux 等待队列
1. Linux内核中等待队列简介
Linux 内核等待队列可以用于许多用途,如中断、进程同步、以及定时。等待队列实现了在事件上的条件等待:希望等待特定事件的进程把自己放进合适的队列,
并且放弃控制权限。因此等待队列表示一组睡眠的进程,当某一事件为真时,有内核唤醒。
2. Linux 内核等待队列中重要数据结构
2.1 等待队列头
struct __wait_queue_head {
spinlock_t lock;
struct list_head task_list;
};
typedef struct __wait_queue_head wait_queue_head_t;
2.2 等待队列睡眠过程
使用等待等待队列通常会定义一个等待队列头,static wait_queue_head_t wq,然后调用wait_event_*函数等待某条件,condition 的当前进程插入到等待队列wq中
并且睡眠,一直等到condition的条件满足后,内核在将对等队列上某一进程或者所有进程唤醒。
#define __wait_event_interruptible(wq, condition, ret) \
do { \
DEFINE_WAIT(__wait); \
\
for (;;) { \
prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
if (condition) \
break; \
if (!signal_pending(current)) { \
schedule(); \
continue; \
} \
ret = -ERESTARTSYS; \
break; \
} \
finish_wait(&wq, &__wait); \
} while (0)
/**
* wait_event_interruptible - sleep until a condition gets true
* @wq: the waitqueue to wait on
* @condition: a C expression for the event to wait for
*
* The process is put to sleep (TASK_INTERRUPTIBLE) until the
* @condition evaluates to true or a signal is received.
* The @condition is checked each time the waitqueue @wq is woken up.
*
* wake_up() has to be called after changing any variable that could
* change the result of the wait condition.
*
* The function will return -ERESTARTSYS if it was interrupted by a
* signal and 0 if @condition evaluated to true.
*/
#define wait_event_interruptible(wq, condition) \
({ \
int __ret = 0; \
if (!(condition)) \
__wait_event_interruptible(wq, condition, __ret); \
__ret; \
})