目录
1. 等待队列
2. 完成量completion
等待队列(wait queue)用于使进程等待某一特定事件发生,而无须频繁轮询。进程在等待期间睡眠,在事件发生时内核自动唤醒。
1、数据结构:
每个等待队列都有一个队列头
//wait.h (include\linux)
struct __wait_queue_head {
spinlock_t lock;
struct list_head task_list;
};
typedef struct __wait_queue_head wait_queue_head_t;
队列中的成员使用
初始化方法:
静态初始化
DECLARE_WAIT_QUEUE_HEAD(name)
动态初始化
wait_queue_head_t my_queue;
init_waitqueue_head(&my_queue);
#define wait_event_timeout(wq_head, condition, timeout) \
({ \
long __ret = timeout; \
might_sleep(); \
if (!___wait_cond_timeout(condition)) \
__ret = __wait_event_timeout(wq_head, condition, timeout); \
__ret; \
})
#define __wait_event_interruptible(wq_head, condition) \
___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
schedule())
#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
wait_event_timeout(wq, condition, timeout)的返回值:
函数返回0:表示timeout超时
返回一个正数:表示还没有超时,但condition变为真,返回剩余的时间
完成量(completion)机制基于等待队列,内核利用该机制等待某一操作完成。
#include
主要API:
DECLARE_COMPLETION(work)
静态初始化一个完成量
#define DECLARE_COMPLETION(work) struct completion work = COMPLETION_INITIALIZER(work)
举例:
static DECLARE_COMPLETION(release_done);
void init_completion(struct completion *x)
动态初始化一个完成量
举例:
struct completion emsgs_comp;
init_completion(&emsgs_comp);
void reinit_completion(struct completion *x)
void wait_for_completion(struct completion *)
void complete(struct completion *)
void complete_all(struct completion *)
extern void wait_for_completion_io(struct completion *);
extern int wait_for_completion_interruptible(struct completion *x);
extern int wait_for_completion_killable(struct completion *x);
extern unsigned long wait_for_completion_timeout(struct completion *x, unsigned long timeout);
extern unsigned long wait_for_completion_io_timeout(struct completion *x, unsigned long timeout);
extern long wait_for_completion_interruptible_timeout(struct completion *x, unsigned long timeout);
extern long wait_for_completion_killable_timeout(struct completion *x, unsigned long timeout);
extern bool try_wait_for_completion(struct completion *x);
extern bool completion_done(struct completion *x);