先看看内核源码的注释,这往往是在研究内核函数必读的一段文字,这将会给我们理解内核代码执行逻辑带来很大的帮助,一定要花时间好好理解;

函数在内核代码被定义为一个宏:

#define __wait_event_interruptible_timeout(wq_head, condition, timeout)         \
        ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
                      TASK_INTERRUPTIBLE, 0, timeout,                           \
                      __ret = schedule_timeout(__ret))

/**

  • wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
  • @wq_head: the waitqueue to wait on
  • @condition: a C expression for the event to wait for
  • @timeout: timeout, in jiffies
  • The process is put to sleep (TASK_INTERRUPTIBLE) until the
  • @condition evaluates to true or a signal is received.
  • The @condition is checked each time the waitqueue @wq_head is woken up.
  • wake_up() has to be called after changing any variable that could
  • change the result of the wait condition.
  • Returns:
  • 0 if the @condition evaluated to %false after the @timeout elapsed,
  • 1 if the @condition evaluated to %true after the @timeout elapsed,
  • the remaining jiffies (at least 1) if the @condition evaluated
  • to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
  • interrupted by a signal.
    */

参数一:等待队列头,struct wait_queue_head_t
参数二:等待条件
参数三:延时时间(10*HZ表示10s)

等待进程被唤醒条件:一直睡眠直到条件成立或者是定时时间到

代码注释详细解释了不同情况下函数的返回值:

返回值state
0condition在timeout之后为状态为false
1condition在timeout之后为状态为ture
jiffiescondition在timeout之前为被置为ture
-ERESTARTSYS(-512)进程被信号中断(kill)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

基本的使用方法:

1.声明等待队列头
 wait_queue_head_t wait_queue
2.初始化等待队列头
 init_waitqueue_head(&wait_queue)
3.条件不成立让wait queue进入等待队列
 wait_event_interruptible_timeout(wait_queue,condition,timeout)
4.条件满足或者是延时时间到
 wake_up_interruptible(&wait_queue)
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐