1>wait queue:<linux/wait.h> __3.0.4
2>description:
在处理推后的工作或阻塞等待某个条件时很有用,wait_queue_func_t func运行在进程上下文.
3>declare or init:
2>description:
在处理推后的工作或阻塞等待某个条件时很有用,wait_queue_func_t func运行在进程上下文.
3>declare or init:
struct __wait_queue_head {
spinlock_t lock;
struct list_head task_list;};
typedef struct __wait_queue_head wait_queue_head_t;
/* 一个wait queue head可以被定义并静态的初始化 */
DECLARE_WAIT_QUEUE_HEAD(name);
/* or 动态的初始化(在代码运行的时候) */
wait_queue_head_t my_queue;
init_waitqueue_head(&my_queue);4>wait a condition and sleep:wait_event(queue, condition)
wait_event_interruptible(queue, condition)
wait_event_timeout(queue, condition, timeout)
wait_event_interruptible_timeout(queue, condition, timeout)5>创建并初始化一个'wait queue entry':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;
};
/*
* 如果一个wait queue entry设置了WQ_FLAH_EXCLUSIVE标志,
* 那么当调用wake_up()唤醒一个等待队列时,
* 它会唤醒第一个设置WQ_FLAH_EXCLUSIVE标志的wait queue entry,然后返回.
*/
DEFINE_WAIT(my_wait);
wait_queue_t my_wait;
init_wait(&my_wait);
#define init_wait(wait) \
do { \
(wait)->private = current; \
(wait)->func = autoremove_wake_function; \
INIT_LIST_HEAD(&(wait)->task_list); \
(wait)->flags = 0; \
} while (0)6>add your ‘wait queue entry’ to the queue(waitqueue_head_t):/*
* queue和wait分别是:wait queue head and the process entry.
* state:将是进程的新状态(e.g TASK_INTERRUPTIBLE).
*/
void prepare_to_wait(wait_queue_head_t *queue,
wait_queue_t *wait,
int state);
void prepare_to_wait_exclusive(wait_queue_head_t *queue,
wait_queue_t *wait,
int state);
/* 也可以,这样将一个‘wait queue entry’添加到指定队列,但是需要单独设置进程的状态. */
void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
7>通常换出的代码在这里执行:/* prepare_to_wait()之后,finish_wait()之前. */
if(!condition)
schedule();8>Once schedule() returns,
it is cleanup time./* 一些清理工作,把自己移出等待队列 */
void finish_wait(wait_queue_head_t *queue, wait_queue_t *wait);9>唤醒:void wake_up(wait_queue_head_t *queue);
void wake_up_interruptible(wait_queue_head_t *queue);
/* 唤醒wait queue上前nr个独占等待(exclusive) */
wake_up_nr(wait_queue_head_t *queue, int nr);
wake_up_interruptible_nr(wait_queue_head_t *queue, int nr);
/* 唤醒所有等待,不管它们是否设置了WQ_FLAG_EXCLUSIVE独占标志 */
wake_up_all(wait_queue_head_t *queue);
wake_up_interruptible_all(wait_queue_head_t *queue);
/* 可以理解为一个唤醒的原子版本,*/
wake_up_interruptible_sync(wait_queue_head_t *queue);
本文深入探讨了Linux中waitqueue的功能、实现细节及其常用操作,包括waitqueue的声明与初始化、等待条件和睡眠、创建与添加等待队列元素、唤醒机制等。详细解释了如何在进程上下文中利用waitqueue进行高效同步与通信。
5311

被折叠的 条评论
为什么被折叠?



