linux completion接口

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( )

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值