Completion 有两个参与者,一个在等待某操作完成,另一个在操作完成时发出声明。
数据结构:
struct completion {
unsigned int done; //表示在进程开始等待之前,事件就已经完成。
wait_queue_head_t wait; //标准的等待队列,等待进程在队列上睡眠。
};
1. 初始化:
a. 动态分配一个completion实例:
b. 建立静态实例:static inline void init_completion(struct completion *x) { x->done = 0; init_waitqueue_head(&x->wait); }
2. 把进程添加到等待队列中. 进程在其中(以独占睡眠方式)等待,直到请求被内核的某些部分处理。#define DECLARE_COMPLETION(work) \ struct completion work = COMPLETION_INITIALIZER(work)
extern void wait_for_completion(struct completion *);
extern int wait_for_completion_interruptible(struct completion *x);
extern unsigned long wait_for_completion_timeout(struct completion *x,
unsigned long timeout);
extern unsigned long wait_for_completion_interruptible_timeout(
struct completion *x, unsigned long timeout);
在内核处理了请求之后,必须用complete()或complete_all(),来唤醒等待的进程。
extern void complete(struct completion *);
extern void complete_all(struct completion *);
或
NORET_TYPE void complete_and_exit(struct completion *comp, long code)
{
if (comp)
complete(comp);
do_exit(code);
}
EXPORT_SYMBOL(complete_and_exit);