wait_event_timeout的理解

来源:互联网 发布:行知外国语是区重点吗 编辑:程序博客网 时间:2024/06/02 05:51
wait_event_timeout(wq, condition, timeout)
表示的有以下两个意思:
(1)当condition为真的时候,会返回
(2)当timeout到达时也会返回,不管此时condition为真为假都会返回
此时接着执行wait_event_timeout之后的代码,只要退出wait_event_timeout,进程就被置为TASK_RUNNING(因为源码中,在退出函数时,会调用到__set_current_state(TASK_RUNNING);)


这个函数是将进程睡眠,置状态为TASK_UNINTERRUPTIBLE直到condition为真,每一次调用唤 醒函数wake_up时都会检查condition,condition为假就继续等待,每次改变任何会导致condition变化的变量时候,都会调用wake_up()
函数返回0:表示timeout超时
       返回一个正数:表示还没有超时,但condition变为真,返回剩余的时间

点击(此处)折叠或打开

  1. /**
  2.  * wait_event_timeout - sleep until a condition gets true or a timeout elapses
  3.  * @wq: the waitqueue to wait on
  4.  * @condition: a C expression for the event to wait for
  5.  * @timeout: timeout, in jiffies
  6.  *
  7.  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  8.  * @condition evaluates to true. The @condition is checked each time
  9.  * the waitqueue @wq is woken up.
  10.  *
  11.  * wake_up() has to be called after changing any variable that could
  12.  * change the result of the wait condition.
  13.  *
  14.  * The function returns 0 if the @timeout elapsed, or the remaining
  15.  * jiffies (at least 1) if the @condition evaluated to %true before
  16.  * the @timeout elapsed.
  17.  */
查看源码,wait_event_timeout( )的返回值,是调用schedule_timeout()返回的
schedule_timeout()表示的进程睡眠直到时间超时,函数就会立即返回,除非进程状态被设置
有以下两种情况:
TASK_UNINTERRUPTIBLE:此时函数就会返回0,需要等待超时时间到才行
TASK_INTERRUPTIBLE:如果一个信号唤醒这个进程,函数就会提前返回,返回值为剩余的jiffies值;也可能是返回0,此时是超时时间刚好到

点击(此处)折叠或打开

  1. /**
  2.  * schedule_timeout - sleep until timeout
  3.  * @timeout: timeout value in jiffies
  4.  *
  5.  * Make the current task sleep until @timeout jiffies have
  6.  * elapsed. The routine will return immediately unless
  7.  * the current task state has been set (see set_current_state()).
  8.  *
  9.  * You can set the task state as follows -
  10.  *
  11.  * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  12.  * pass before the routine returns. The routine will return 0
  13.  *
  14.  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  15.  * delivered to the current task. In this case the remaining time
  16.  * in jiffies will be returned, or 0 if the timer expired in time
  17.  *
  18.  * The current task state is guaranteed to be TASK_RUNNING when this
  19.  * routine returns.
  20.  *
  21.  * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  22.  * the CPU away without a bound on the timeout. In this case the return
  23.  * value will be %MAX_SCHEDULE_TIMEOUT.
  24.  *
  25.  * In all cases the return value is guaranteed to be non-negative

  26. 转自:http://blog.chinaunix.net/uid-30313399-id-5203964.html

0 0