wait _event_interruptible()函数分析
标签:
杂谈 |
分类: linuxkernel |
Linux-2.6提供如下关于等待队列的操作:
(1) 定义"等待队列头",
wait_queue_head_t my_queue;
defined in linux/wait.h
50 struct __wait_queue_head {
(2) 初始化"等待队列头"
init_waitqueue_head(&my_queue);
defined in linux/wait.c header file
13 void init_waitqueue_head(wait_queue_head_t *q)
定义和初始化的快捷方式:
DECLARE_WAIT_QUEUE_HEAD(my_queue);
linux/wait.h
(3) 定义等待队列
DECLARE_WAITQUEUE(name, tsk);
定义并初始化一个名为name的等待队列(wait_queue_t);
linux/wait.h
defined in linux/wait.h
51 spinlock_t lock;
52 struct list_head task_list;
53 };
54 typedef struct __wait_queue_head wait_queue_head_t;
(2) 初始化"等待队列头"
defined in linux/wait.c header file
14 { 15 spin_lock_init(&q->lock); 16 INIT_LIST_HEAD(&q->task_list); 17 }
linux/wait.h
70 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \ 71 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \ 72 .task_list = { &(name).task_list, &(name).task_list } }
74 #define DECLARE_WAIT_QUEUE_HEAD(name) \ 75 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
linux/wait.h
32 struct __wait_queue { 33 unsigned int flags; 34 #define WQ_FLAG_EXCLUSIVE 0x01 35 void *private; 36 wait_queue_func_t func; 37 struct list_head task_list; 38 }; 28 typedef struct __wait_queue wait_queue_t;
62 #define __WAITQUEUE_INITIALIZER(name, tsk) { \ 63 .private = tsk, \ 64 .func = default_wake_function, \ 65 .task_list = { NULL, NULL } } 66 67 #define DECLARE_WAITQUEUE(name, tsk) \ 68 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk) 2 specific analysis wait_event_interruptible()。该函数修改task的状态为TASK_INTERRUPTIBLE,意味着改进程将不会继续运行直到被唤醒,然后被添加到等待队列wq中。 在wait_event_interruptible()中首先判断condition是不是已经满足,如果是则直接返回0,否则调用__wait_event_interruptible(),并用__ret来存放返回值 --------------------------------------------------------------- #define wait_event_interruptible(wq, condition)\ ({ \ int __ret = 0; \ if (!(condition)) \ __wait_event_interruptible(wq, condition, __ret);\ __ret;