您的位置:首页 > 大数据 > 人工智能

wait_event_timeout的理解

2017-01-10 22:28 519 查看
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变为真,返回剩余的时间

点击(此处)折叠或打开

/**

 * wait_event_timeout - sleep until a condition gets true or a
timeout elapses

 * @wq: 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_UNINTERRUPTIBLE) until the

 * @condition evaluates to true. The
@condition is checked each time

 * the waitqueue @wq is woken up.

 *

 * wake_up() has to be
called after changing any variable that could

 * change the result of the wait condition.

 *

 * The function returns 0 if the @timeout elapsed, or the
remaining

 * jiffies (at least 1) if the
@condition evaluated to %true before

 * the @timeout elapsed.

 */

查看源码,wait_event_timeout( )的返回值,是调用schedule_timeout()返回的

schedule_timeout()表示的进程睡眠直到时间超时,函数就会立即返回,除非进程状态被设置

有以下两种情况:

TASK_UNINTERRUPTIBLE:此时函数就会返回0,需要等待超时时间到才行

TASK_INTERRUPTIBLE:如果一个信号唤醒这个进程,函数就会提前返回,返回值为剩余的jiffies值;也可能是返回0,此时是超时时间刚好到

点击(此处)折叠或打开

/**

 * schedule_timeout - sleep until timeout

 * @timeout: timeout value in jiffies

 *

 * Make the current task sleep until @timeout jiffies have

 * elapsed. The routine will return immediately unless

 * the current task state has been set (see set_current_state()).

 *

 * You can set the task state as follows -

 *

 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to

 * pass before the routine returns. The routine will return 0

 *

 * %TASK_INTERRUPTIBLE - the routine may return early if a
signal is

 * delivered to the current task. In this case the
remaining time

 * in jiffies will be returned, or 0 if the
timer expired in time

 *

 * The current task state is guaranteed to be TASK_RUNNING when this

 * routine returns.

 *

 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule

 * the CPU away without a bound on the timeout. In this case the
return

 * value will be %MAX_SCHEDULE_TIMEOUT.

 *

 * In all cases the return value is guaranteed to be
non-negative

转自:http://blog.chinaunix.net/uid-30313399-id-5203964.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: