http://blog.csdn.net/a254373829/article/details/8461178
头文件<include/linux/completion.h>
/*
* struct completion - structure used to maintain state for a "completion"*
* This is the opaque structure used to maintain the state for a "completion".
* Completions currently use a FIFO to queue threads that have to wait for
* the "completion" event.
*
* See also: complete(), wait_for_completion() (and friends _timeout,
* _interruptible, _interruptible_timeout, and _killable), init_completion(),
* and macros DECLARE_COMPLETION(), DECLARE_COMPLETION_ONSTACK(), and
* INIT_COMPLETION().
*/
相关结构和接口函数定义:
struct completion {unsigned int done;
wait_queue_head_t wait; // 内部实现依赖于等待队列
};
定义一个completion:
DECLARE_COMPLETION(comp);
/**
* completion_done - Test to see if a completion has any waiters
* @x: completion structure
*
* Returns: 0 if there are waiters (wait_for_completion() in progress)
* 1 if there are no waiters.
*
*/
extern bool completion_done(struct completion *x);
//以下wait类函数在返回后会将completion结构里的done字段减一。
extern void wait_for_completion(struct completion *); //不可中断版本,KILL 信号也杀不掉.....
extern int wait_for_completion_interruptible(struct completion *x); //可被信号中断版本
extern int wait_for_completion_killable(struct completion *x); //只响应KILL信号的中断
extern unsigned long wait_for_completion_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); //KILL信号&&超时版本
/**
* try_wait_for_completion - try to decrement a completion without blocking
* @x: completion structure
*
* Returns: 0 if a decrement cannot be done without blocking
* 1 if a decrement succeeded.
*
* If a completion is being used as a counting completion,
* attempt to decrement the counter without blocking. This
* enables us to avoid waiting if the resource the completion
* is protecting is not available.
*/
extern bool try_wait_for_completion(struct completion *x);
/**
* complete: - signals a single thread waiting on this completion
* @x: holds the state of this particular completion
*
* This will wake up a single thread waiting on this completion. Threads will be
* awakened in the same order in which they were queued.
*
* See also complete_all(), wait_for_completion() and related routines.
*
* It may be assumed that this function implies a write memory barrier before
* changing the task state if and only if any tasks are woken up.
*/
extern void complete(struct completion *); //唤醒单线程版本,会增加completion结构体的done字段 + 1
/**
* complete_all: - signals all threads waiting on this completion
* @x: holds the state of this particular completion
*
* This will wake up all threads waiting on this particular completion event.
*
* It may be assumed that this function implies a write memory barrier before
* changing the task state if and only if any tasks are woken up.
*/
extern void complete_all(struct completion *); //唤醒所有等待线程,会将completion结构体的done字段置为 UINT_MAX/2.
PS:
类似于生产者和消费者模型,当等待一个completion时,必须由某一个生产者线程调用compete( )后,消费者线程才能返回。
当某一个线程调用多次complete( )后,另一个线程调用wait_for_completion( )会马上返回,直到调用相应次数后才会等待另一个线程调用complete( )