我是靠谱客的博主 忧虑墨镜,最近开发中收集的这篇文章主要介绍Linux kernel 5.x wait_event_interruptible_timeout(),觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
先看看内核源码的注释,这往往是在研究内核函数必读的一段文字,这将会给我们理解内核代码执行逻辑带来很大的帮助,一定要花时间好好理解;
函数在内核代码被定义为一个宏:
#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 |
---|---|
0 | condition在timeout之后为状态为false |
1 | condition在timeout之后为状态为ture |
jiffies | condition在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)
最后
以上就是忧虑墨镜为你收集整理的Linux kernel 5.x wait_event_interruptible_timeout()的全部内容,希望文章能够帮你解决Linux kernel 5.x wait_event_interruptible_timeout()所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复