微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > Linux学习-等待队列

Linux学习-等待队列

时间:12-01 来源:互联网 点击:
由于学习linux驱动编程,学习到了堵塞型IO读写,等待队列的操作比较的有意思,拿来分析分析,其中的一些代码还是蛮有意思的,感受到了linux的美,体会到了艺术家和一般程序员的差别。

我就简要的分析分析等待队列的一些问题,就相当于自己的总结吧。边学驱动,边学内核,还是蛮有意思的。

1、等待队列的定义,包括两个,等待队列头,节点。

struct __wait_queue_head {

spinlock_t lock; /*自旋锁*/

struct list_head task_list; /*链表头*/

};

typedef struct __wait_queue_head wait_queue_head_t;

...

struct __wait_queue {

unsigned int flags;

#define WQ_FLAG_EXCLUSIVE 0x01

void *private;

wait_queue_func_t func;

struct list_head task_list;

};

/*关于等待队列的操作主要是初始化操作*/

#define DECLARE_WAIT_QUEUE_HEAD(name)

wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)

/*就是初始化两个元素*/

#define __WAIT_QUEUE_HEAD_INITIALIZER(name) {

.lock = __SPIN_LOCK_UNLOCKED(name.lock),

.task_list = { &(name).task_list, &(name).task_list } }

#define init_waitqueue_head(q)

do {

static struct lock_class_key __key;

__init_waitqueue_head((q), &__key);

} while (0)

void __init_waitqueue_head(wait_queue_head_t *q, struct lock_class_key *key)

{

spin_lock_init(&q->lock);

lockdep_set_class(&q->lock, key);

INIT_LIST_HEAD(&q->task_list);

}

从上面的定义可知,实质上等待队列头很简单,只要就是一个链表头,而等待队列的节点主要包含了一个函数指针和对应的参数,以及链表。

我们在驱动过程中主要使用的函数主要包括wait_event(),wait_event_interruptible(),wait_event_killable(),以及唤醒过程中的wait_up(),wait_up_interruptible().

基本的流程就是:

#define wait_event(wq, condition)

do {

if (condition)

/*添加满足,则直接跳出*/

break;

/*负责进入等待队列*/

__wait_event(wq, condition);

} while (0)

#define __wait_event(wq, condition)

do {

/*定义新的等待队列节点*/

DEFINE_WAIT(__wait);

for (;;) {/*一个循环的过程,可能导致堵塞*/

/*将添加的节点添加到队列中,并改变进程的运行状态*/

prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);

if (condition)/*如果条件合适了,就跳出当前的循环,也就是等待条件获得*/

break;

/*当前进程放弃CPU,进行调度其他的进程,这时的进程进入睡眠状态

也就是说在schedule中函数就不在继续执行,只有调用wake_up函数唤

醒当前的进程,才会退出schedule函数,然后继续执行下面的函数,也就是继续循环

真正的退出循环,只有当条件满足时,如果条件不满足,调用wake_up函数

仍然不会满足条件,只会再次调度,再次失去CPU,

根据上面的分析可知,只有上面的条件满足了,并调用

wake_up函数才能跳出当前的for循环。

*/

schedule();

}

/*完成等待*/

finish_wait(&wq, &__wait);

} while (0)

#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)

#define DEFINE_WAIT_FUNC(name, function)

wait_queue_t name = {

.private = current,

.func = function,

.task_list = LIST_HEAD_INIT((name).task_list),

}

void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)

{

unsigned long flags;

/*改变状态*/

wait->flags &= ~WQ_FLAG_EXCLUSIVE;

spin_lock_irqsave(&q->lock, flags);

/*如果链表是空,则将当前的这个节点添加进来,这样能避免wait被反复的添加,造成大量的浪费*/

if (list_empty(&wait->task_list))

__add_wait_queue(q, wait);

/*修改当前进程的状态*/

set_current_state(state);

spin_unlock_irqrestore(&q->lock, flags);

}

#define set_current_state(state_value)

set_mb(current->state, (state_value))

static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)

{

/*就是将链表添加进来而已*/

list_add(&new->task_list, &head->task_list);

}

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);

__a

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top