等待队列在linux内核中有着举足轻重的作用,很多linux驱动都或多或少涉及到了等待队列。因此,对于linux内核及驱动开发者来说,掌握等待队列是必须课之一。举凡 device driver,semaphore 等方面都会使用到 wait_queue 来 implement。所以,它算是 kernel 里蛮 基本的一个数据结构。
Linux内核的等待队列是以双循环链表为基础数据结构,与进程调度机制紧密结合,能够用于实现核心的异步事件通知机制。它有两种数据结构:等待队列头(wait_queue_head_t)和等待队列项(wait_queue_t)。等待队列头和等待队列项中都包含一个list_head类型的域作为”连接件”。它通过一个双链表把等待task的头,和等待的进程列表链接起来。下面具体介绍。
一、定义:
头文件:/include/linux/wait.h
struct __wait_queue_head
{
spinlock_t lock;
struct list_head task_list;
};
typedef struct __wait_queue_head wait_queue_head_t;
二、作用:
在内核里面,等待队列是有很多用处的,尤其是在中断处理、进程同步、定时等场合。可以使用等待队列实现阻塞进程的唤醒。它以队列为基础数据结构,与进程调度机制紧密结合,能够用于实现内核中的异步事件通知机制,同步对系统资源的访问等。
三、字段详解:
1、spinlock_t lock;
在对task_list与操作的过程中,使用该锁实现对等待队列的互斥访问。
2、srtuct list_head_t task_list;
双向循环链表,存放等待的进程。
四、操作:
1、定义并初始化等待队列头:
(1)
wait_queue_head_t my_queue;
init_waitqueue_head(&my_queue);
直接定义并初始化。init_waitqueue_head()函数会将自旋锁初始化为未锁,等待队列初始化为空的双向循环链表。
(2)
DECLARE_WAIT_QUEUE_HEAD(my_queue);
定义并初始化,相当于(1)。
(3) 定义等待队列:
DECLARE_WAITQUEUE(name,tsk);
注意此处是定义一个wait_queue_t类型的变量name,并将其private与设置为tsk。wait_queue_t类型定义如下:
typedef struct __wait_queue wait_queue_t;
struct __wait_queue {
unsigned int flags;
#define WQ_FLAG_EXCLUSIVE 0x01
void *private;
wait_queue_func_t func;
struct list_head task_list;
};
其中flags域指明该等待的进程是互斥进程还是非互斥进程。其中0是非互斥进程,WQ_FLAG_EXCLUSIVE(0×01)是互斥进程。等待队列(wait_queue_t)和等待对列头(wait_queue_head_t)的区别是等待队列是等待队列头的成员。也就是说等待队列头的task_list域链接的成员就是等待队列类型的(wait_queue_t)。
2、(从等待队列头中)添加/移出等待队列:
(1) add_wait_queue()函数:
void 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);
}
EXPORT_SYMBOL(add_wait_queue);
设置等待的进程为非互斥进程,并将其添加进等待队列头(q)的队头中。
void 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);
}
EXPORT_SYMBOL(add_wait_queue_exclusive);
该函数也和add_wait_queue()函数功能基本一样,只不过它是将等待的进程(wait)设置为互斥进程。
(2)remove_wait_queue()函数:
void 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);
}
EXPORT_SYMBOL(remove_wait_queue);
在等待的资源或事件满足时,进程被唤醒,使用该函数被从等待头中删除。
3、等待事件:
(1)wait_event()宏:
/**
* wait_event - 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_UNINTERRUPTIBLE) until the
* @condition evaluates to true. 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.
*/
#define wait_event(wq, condition) \
do { \
if (condition) \
break; \
__wait_event(wq, condition); \
} while (0)
在等待会列中睡眠直到condition为真。在等待的期间,进程会被置为TASK_UNINTERRUPTIBLE进入睡眠,直到condition变量变为真。每次进程被唤醒的时候都会检查condition的值.
(2)wait_event_interruptible()函数:
和wait_event()的区别是调用该宏在等待的过程中当前进程会被设置为TASK_INTERRUPTIBLE状态.在每次被唤醒的时候,首先检查condition是否为真,如果为真则返回,否则检查如果进程是被信号唤醒,会返回-ERESTARTSYS错误码.如果是condition为真,则返回0.
(3)wait_event_timeout()宏:
也与wait_event()类似.不过如果所给的睡眠时间为负数则立即返回.如果在睡眠期间被唤醒,且condition为真则返回剩余的睡眠时间,否则继续睡眠直到到达或超过给定的睡眠时间,然后返回0.
(4)wait_event_interruptible_timeout()宏:
与wait_event_timeout()类似,不过如果在睡眠期间被信号打断则返回ERESTARTSYS错误码.
(5) wait_event_interruptible_exclusive()宏
同样和wait_event_interruptible()一样,不过该睡眠的进程是一个互斥进程.
5、唤醒队列:
(1)wake_up()函数:
#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
/**
* __wake_up - wake up threads blocked on a waitqueue.
* @q: the waitqueue
* @mode: which threads
* @nr_exclusive: how many wake-one or wake-many threads to wake up
* @key: is directly passed to the wakeup function
*/
void __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);
}
EXPORT_SYMBOL(__wake_up);
唤醒等待队列.可唤醒处于TASK_INTERRUPTIBLE和TASK_UNINTERUPTIBLE状态的进程,和wait_event/wait_event_timeout成对使用.
(2)wake_up_interruptible()函数:
#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
和wake_up()唯一的区别是它只能唤醒TASK_INTERRUPTIBLE状态的进程.,与wait_event_interruptible/wait_event_interruptible_timeout/ wait_event_interruptible_exclusive成对使用. (3)
#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 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)
这些也基本都和wake_up/wake_up_interruptible一样.
6、在等待队列上睡眠:
(1) sleep_on()函数:
void __sched sleep_on(wait_queue_head_t *q)
{
sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
}
static long __sched
sleep_on_common(wait_queue_head_t *q, int state, long timeout)
{
unsigned long flags;
wait_queue_t wait;
init_waitqueue_entry(&wait, current);
__set_current_state(state);
spin_lock_irqsave(&q->lock, flags);
__add_wait_queue(q, &wait);
spin_unlock(&q->lock);
timeout = schedule_timeout(timeout);
spin_lock_irq(&q->lock);
__remove_wait_queue(q, &wait);
spin_unlock_irqrestore(&q->lock, flags);
return timeout;
}
该函数的作用是定义一个等待队列(wait),并将当前进程添加到等待队列中(wait),然后将当前进程的状态置为TASK_UNINTERRUPTIBLE,并将等待队列(wait)添加到等待队列头(q)中。之后就被挂起直到资源可以获取,才被从等待队列头(q)中唤醒,从等待队列头中移出。在被挂起等待资源期间,该进程不能被信号唤醒。
(2)sleep_on_timeout()函数:
long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
{
return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
}
EXPORT_SYMBOL(sleep_on_timeout);
与sleep_on()函数的区别在于调用该函数时,如果在指定的时间内(timeout)没有获得等待的资源就会返回。实际上是调用schedule_timeout()函数实现的。值得注意的是如果所给的睡眠时间(timeout)小于0,则不会睡眠。该函数返回的是真正的睡眠时间。
(3)interruptible_sleep_on()函数:
void __sched interruptible_sleep_on(wait_queue_head_t *q)
{
sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
}
EXPORT_SYMBOL(interruptible_sleep_on);
该函数和sleep_on()函数唯一的区别是将当前进程的状态置为TASK_INTERRUPTINLE,这意味在睡眠如果该进程收到信号则会被唤醒。
(4)interruptible_sleep_on_timeout()函数:
long __sched
interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
{
return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
}
EXPORT_SYMBOL(interruptible_sleep_on_timeout);
类似于sleep_on_timeout()函数。进程在睡眠中可能在等待的时间没有到达就被信号打断而被唤醒,也可能是等待的时间到达而被唤醒。
以上四个函数都是让进程在等待队列上睡眠,不过是小有诧异而已。在实际用的过程中,根据需要选择合适的函数使用就是了。例如在对软驱数据的读写中,如果设备没有就绪则调用sleep_on()函数睡眠直到数据可读(可写),在打开串口的时候,如果串口端口处于关闭状态则调用interruptible_sleep_on()函数尝试等待其打开。在声卡驱动中,读取声音数据时,如果没有数据可读,就会等待足够常的时间直到可读取。
深入理解
首先,我们得明白,linux中的所有的进程都由task_struct这个结构管理。在生成进程的时候将会分配一个task_struct结构,之后将通过这个结构对进程进行管理。 task_struct结构存在于平坦地址空间内,任何时候Linux内核都可以参照所有进程的所有管理情报。内核堆栈也同样位于平坦地址空间内。(平坦的意思是"独立的连续区间")
下面是tesk_struct的主要成员:
struct task_struct
{
struct files_struct* files; //文件描述符
struct signal_struct* sig; //信号控制signal handler
struct mm_struct* mm; //内存管理模块
long stat //进程状态
struct list_head runlist; //用于联结RUN队列
long priority; //基本优先权
long counter; //变动优先权
char comm[]; //命令名
struct thread_struct tss; //上下文保存领域
...
};
我们现在只需了解它里面的state就可以,state有下面几种状态:
状态 说明
TASK_RUNNING 执行可能状态
TASK_INTERRUPTIBLE 等待状态。可接受信号
TASK_UNINTERRUPTIBLE 等待状态。不能接受信号
TASK_ZOMBIE 僵尸状态。exit后的状态
TASK_STOPPED 延缓状态
我们要知道内核没有多进程,就只有一个进程(SMP就不清楚了,SMP(Symmetric Multi-Processing),对称多处理结构的简称,是指在一个计算机上汇集了一组处理器(多CPU),各CPU之间共享内存子系统以及总线结构),这跟在user space下是不同的.在用户空间里,我们可以使一个进程跑起while(1),其他的进程也能用,但是在内核中就不行了,原因在上面。
假设我们在 kernel 里产生一个 buffer,user 可以经由 read,write 等 system call 来读取或写资料到这个 buffer 里。如果有一个 user 写资料到 buffer 时,此时 buffer 已经满了。那请问你要如何去处理这种情形呢 ? 第一种,传给 user 一个错误讯息,说 buffer 已经满了,不能再写入。第二种,将 user 的要求 block 住, 等有人将 buffer 内容读走,留出空位时,再让 user 写入资料。但问题来了,你要怎么将 user 的要求 block 住。难道你要用
while ( is_full );
write_to_buffer;
这样的程序代码吗? 想想看,如果你这样做会发生什么事? 第一,kernel会一直在这个 while 里执行。第二个,如果 kernel 一直在这个 while 里执行,表示它没有办法去 maintain系统的运作。那此时系统就相当于当掉了。在这里 is_full 是一个变量,当然,你可以让 is_full 是一个 function,在这个 function里会去做别的事让 kernel 可以运作,那系统就不会当。这是一个方式。还有,你说可以在while里面把buffer里的内容读走,再把is_full的值改了,但是我们会可能把重要的数据在我们不想被读的时候被读走了,那是比较麻烦的,而且很不灵活.如果我们使用 wait_queue 的话, 那程序看起来会比较漂亮,而且也比较让人了解,如下所示:
struct wait_queue_head_t wq; /* global variable */
DECLARE_WAIT_QUEUE_HEAD (wq);
while ( is_full )
{
interruptible_sleep_on( &wq );
}
write_to_buffer();
interruptible_sleep_on( &wq ) 是用来将目前的 process,也就是要求写资料到buffer 的 process放到 wq 这个 wait_queue 里。在 interruptible_sleep_on 里,则是最后会呼叫 schedule() 来做 schedule 的动作,谁调用了schedule谁就趴下,让别人去运行,醒来就原地起来,执行schedule()后的代码。那那个调用了schedule的家伙什么醒过来呢?这时候就需要用到另一个函数了wake_up_interruptible()了,如下所示:
<span style="font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;font-size:12px;">if ( !is_empty )
{
read_from_buffer();
wake_up_interruptible( &wq );
}</span>
这就wait_queue的用法,挺好懂的.那wait_queue到底是怎么工作的呢?wait_queue_head_t是一个相单简单的结构,在中,代码如下:
<span style="font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;"><span style="font-size:12px;">struct __wait_queue_head
{
wq_lock_t lock;
truct list_head task_list;
#if WAITQUEUE_DEBUG
long __magic;
long __creator;#endif
};
typedef struct __wait_queue_head wait_queue_head_t;</span></span>
其中task_list是一个正在睡眠的进程的链表,链表中的各个数据项的类型是wait_queue_t,链表就是在中定义的通用链表,wait_queue_t代码如下:
<span style="font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;"><span style="font-size:12px;">struct __wait_queue
{
unsigned int flags;
#define WQ_FLAG_EXCLUSIVE 0x01
struct task_struct * task;
struct list_head task_list;
#if WAITQUEUE_DEBUG
long __magic;
long __waker;#endif
};
typedef struct __wait_queue wait_queue_t;</span></span>
其实,主要的结构是wait_queue_t.让我们来看一下interruptible_sleep_on的代码中,代码如下:
#define SLEEP_ON_VAR \
unsigned long flags; \
wait_queue_t wait; \
init_waitqueue_entry(&wait, current); //用当前进程生成一个wait_queue_t
#define SLEEP_ON_HEAD \
spin_lock_irqsave(&q->lock,flags); \
__add_wait_queue(q, &wait); //把 wait 放到 q 所属的wait_queue_t list 的开头
spin_unlock(&q->lock);
#define SLEEP_ON_TAIL \
spin_lock_irq(&q->lock); \
__remove_wait_queue(q, &wait); \
spin_unlock_irqrestore(&q->lock, flags);
void interruptible_sleep_on(wait_queue_head_t *q)
{
SLEEP_ON_VAR
current->state = TASK_INTERRUPTIBLE;
SLEEP_ON_HEAD
schedule(); //状态为TASK_INTERRUPTIBLE的进程是不会执行的
SLEEP_ON_TAIL
}
static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
{
#if WAITQUEUE_DEBUG
if (!head || !new)
WQ_BUG();
CHECK_MAGIC_WQHEAD(head);
CHECK_MAGIC(new->__magic);
if (!head->task_list.next || !head->task_list.prev)
WQ_BUG();
#endif
list_add(&new->task_list, &head->task_list);
}
static inline void __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
{
#if WAITQUEUE_DEBUG
if (!old)
WQ_BUG();
CHECK_MAGIC(old->__magic);
#endif
list_del(&old->task_list);
}
static inline void __list_del(struct list_head *prev, struct list_head *next)
{
next->prev = prev;
prev->next = next;
}
/*** list_del - deletes entry from list.
* @entry: the element to delete from the list
* Note: list_empty on entry does not return true after this, the entry is in an undefined state.
*/
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = (void *) 0;
entry->prev = (void *) 0;
}
上面的代码都应该比较好懂.我们先用当前进程生成了一个wait_queue_t,把当前进程的state改成TASK_INTERRUPTIBLE,然后把这个wait_queue_t加到我们已经声明并初始化好的全局变量q中去.这时调用shedule,current 所指到的 process 会被放到 scheduling queue 中等待被挑出来执行。执行完 schedule() 之后,current 就没办法继续执行了。而当 current 以后被 wake up 时,就会从 schedule() 之后,也就是从 SLEEP_ON_TAIL 开始执行。我们现在当然明白wake_up_interruptible所需做的是把进程的状态改成Running的,其代码如下:
#define wake_up_interruptible(x) __wake_up((x),TASK_INTERRUPTIBLE, 1)
void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
{
unsigned long flags;
if (unlikely(!q))
return;
spin_lock_irqsave(&q->lock, flags);
__wake_up_common(q, mode, nr_exclusive, 0);
spin_unlock_irqrestore(&q->lock, flags);
}
static inline void __wake_up_common(wait_queue_head_t *q,
unsigned int mode, int nr_exclusive, int sync)
{
struct list_head *tmp;
unsigned int state;
wait_queue_t *curr;
task_t *p
list_for_each(tmp, &q->task_list)
{
curr = list_entry(tmp, wait_queue_t, task_list);
p = curr->task;
state = p->state;
if ((state & mode) && try_to_wake_up(p, mode, sync) && ((curr->flags & WQ_FLAG_EXCLUSIVE)
&& !--nr_exclusive))
break;
}
}
static int try_to_wake_up(task_t * p, unsigned int state, int sync)
{
unsigned long flags;
int success = 0;
long old_state;
runqueue_t *rq;
sync &= SYNC_WAKEUPS;repeat_lock_task:
rq = task_rq_lock(p, &flags);
old_state = p->state;
if (old_state & state)
{ //状态相同的就改
if (!p->array) {
/*
* Fast-migrate the task if it's not running or runnable * currently. Do not violate hard affinity.
*/
if (unlikely(sync&&!task_running(rq, p) && (task_cpu(p)!= smp_processor_id())
&& (p->cpus_allowed & (1UL << smp_processor_id()))))
{
set_task_cpu(p, smp_processor_id());
task_rq_unlock(rq, &flags);
goto repeat_lock_task;
}
if (old_state == TASK_UNINTERRUPTIBLE)
rq->nr_uninterruptible--;
if (sync)
__activate_task(p, rq);
else
{
activate_task(p, rq);
resched_task(rq->curr);
}
success = 1;
}
if (p->state >= TASK_ZOMBIE)
BUG();
p->state = TASK_RUNNING;
}
task_rq_unlock(rq, &flags);
return success;
}
由于 schedule的代码量比较大,就不贴出来了。
http://velep.com/archives/815.html
http://www.cnblogs.com/xmphoenix/archive/2011/11/20/2256419.html