您的位置:首页 > 其它

等待队列源码分析

2014-03-12 20:10 232 查看
1. struct list_head {

struct list_head *next, *prev; //定义了一个指向下一个的指针和一个指向上一个的指针

};

////////////////////////////////////////////////////

2.每一个等待队列有两部分组成:一个是等待队列头和一个等待队列成员

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; //指向任务的task_struct

wait_queue_func_t func; //默认唤醒函数

struct list_head task_list;

};

////////////////////////////////////////////////////

3.队列初始化函数分析

extern void __init_waitqueue_head(wait_queue_head_t *q, struct lock_class_key *);

//初始化队列函数

#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); //初始化队列

}

static inline void INIT_LIST_HEAD(struct list_head *list)

{

list->next = list; //链表操作

list->prev = list;

}

//////////////////////////////////////////////////

4.添加和移除等待队列函数分析

void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)

{

unsigned long flags;

wait->flags &= ~WQ_FLAG_EXCLUSIVE(=0x01);//相与必等于0,将进程调为非互斥

spin_lock_irqsave(&q->lock, flags); //关中断,保护临界区

__add_wait_queue(q, wait);//将wait进程添加队列中的头部位(分析下面的函数)

spin_unlock_irqrestore(&q->lock, flags); //中断恢复

}

========

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

{

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

}

static inline void list_add(struct list_head *new, struct list_head *head)

{

__list_add(new, head, head->next);

}

static inline void __list_add(struct list_head *new,

struct list_head *prev,

struct list_head *next)

{

next->prev = new;

new->next = next;

new->prev = prev;

prev->next = new;

}

=======

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); //移除wait进程

spin_unlock_irqrestore(&q->lock, flags); //中断恢复

}

======================

static inline void __remove_wait_queue(wait_queue_head_t *head,

wait_queue_t *old)

{

list_del(&old->task_list); //删除等待进程old的结点

}

static inline void list_del(struct list_head *entry)

{

__list_del(entry->prev, entry->next);

entry->next = LIST_POISON1; //指向其他地方,防止出错

entry->prev = LIST_POISON2;

}

static inline void __list_del(struct list_head * prev, struct list_head * next)

{

next->prev = prev;

prev->next = next;

}

=======================

void

prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)

{

unsigned long flags;

wait->flags |= WQ_FLAG_EXCLUSIVE; //结果为1,进程为互斥进程

spin_lock_irqsave(&q->lock, flags); //关中断

if (list_empty(&wait->task_list)) //判断wait队列是否为空,即些进程是否存在

__add_wait_queue_tail(q, wait); //添加到队列尾部

set_current_state(state); //设置进程状态

spin_unlock_irqrestore(&q->lock, flags);

}

============================================

#define set_current_state(state_value) \

set_mb(current->state, (state_value))

#define set_mb(var, value) do { var = value; mb(); } while (0)

#define mb() asm volatile ("": : :"memory") 听网上说是设置了一个内存屏障,这里不懂,如果好的理解,望解释解释

============================================

5./////////////////////////////////////////

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

if (list_empty(&wait->task_list)) //判断进程是否在队列中

__add_wait_queue(q, wait); //如果没有,加入队列

set_current_state(state);

spin_unlock_irqrestore(&q->lock, flags);

}

6////////////////////////////////////////////

void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)

{

unsigned long flags;

__set_current_state(TASK_RUNNING); //设置当前进程为RUNING

/*

* We can check for list emptiness outside the lock

* IFF:

* - we use the "careful" check that verifies both

* the next and prev pointers, so that there cannot

* be any half-pending updates in progress on other

* CPU's that we haven't seen yet (and that might

* still change the stack area.

* and

* - all other users take the lock (ie we can only

* have _one_ other CPU that looks at or modifies

* the list).

*/

if (!list_empty_careful(&wait->task_list)) { //判断是否在队列中

spin_lock_irqsave(&q->lock, flags);

list_del_init(&wait->task_list); //如果在删除

spin_unlock_irqrestore(&q->lock, flags);

}

}

EXPORT_SYMBOL(finish_wait);

=====================================

static inline int list_empty_careful(const struct list_head *head)

{

struct list_head *next = head->next;

return (next == head) && (next == head->prev);

}

static inline void list_del_init(struct list_head *entry)

{

__list_del(entry->prev, entry->next); //删除entry进程

INIT_LIST_HEAD(entry);//初始化指针

}

static inline void INIT_LIST_HEAD(struct list_head *list)

{

list->next = list;

list->prev = list;

}

======================================

void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,

unsigned int mode, void *key)

{

unsigned long flags;

__set_current_state(TASK_RUNNING);

spin_lock_irqsave(&q->lock, flags);

if (!list_empty(&wait->task_list))

list_del_init(&wait->task_list); //唤醒进程

else if (waitqueue_active(q)) //判断队列是否为空,如是不是空执行下一句

__wake_up_locked_key(q, mode, key); //唤醒你想唤醒的进程

spin_unlock_irqrestore(&q->lock, flags);

}

============================================

============================================

7、////////////////////////////////////////////////////////////////

int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)

{

int ret = default_wake_function(wait, mode, sync, key);

if (ret)

list_del_init(&wait->task_list); //默认唤醒函数

return ret;

}

主要函数就分析这些,如果上面有什么错误的,望大家指出来
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: