首页 > 其他分享 >wait_event_interruptible_timeout() 函数

wait_event_interruptible_timeout() 函数

时间:2024-09-29 16:33:53浏览次数:1  
标签:timeout interruptible wq event condition wait

 

原文链接:https://blog.csdn.net/wuyongpeng0912/article/details/45723657

 

网上有关于此函数的分析,但大都是同一篇文章转载来转载去,没有进一步的分析。做个小结:

了解函数功能,除了直接看代码逻辑,最有效的当是注释内容了。
如下:

函数原型:wait_event_interruptible_timeout(wq, condition, timeout)

* 函数作用:~睡眠~,直到condition为真,或timeout超时;
* @wq: 要等待的等待队列
* @condition: 等待事件发生的条件(一个C表达式 )
* @timeout: 超时时间

程序是用来睡眠的(TASK_INTERRUPTIBLE状态下),直到@condition为真,或者收到一个信号。
每次等待队列@wq被唤醒时,检查@condition;
若有可以改变等待条件的任何更改操作,调用wake_up();
如果@timeout超时,函数返回0;如果是被信号中断,函数返回-ERESTARTSYS;
在超时之前condition 为true,否则继续剩余的jiffies 。
 

理解所需:
1,TASK_INTERRUPTIBLE是可以被信号和wake_up()唤醒的,当信号到来时,进程会被设置为可运行;而TASK_UNINTERRUPTIBLE只能被wake_up()唤醒。
2,信号是在软件层次上对中断机制的一种模拟,为软中断。
3,signal_pending(current) :检查当前进程是否有信号处理,返回值不为0则表示有信号需要处理。
返回 -ERESTARTSYS 表示信号函数处理完毕后重新执行信号函数前的某个系统调用。(此函数只检查是否有信号,不处理信号)
条件不满足,则产生信号、开始等待。若返回非0,则表示没有信号需要处理,继续循环重新开始系统调用;若返回0,则表示有信号需要处理,直接处理数据,系统调用正常结束。
4,schedule_timeout()用来让出CPU,在指定的时间用完以后或者其它事件到达并唤醒进程时(比如接收了一个信号量),该进程才可以继续运行.
[函数返回0表示timeout用完后被唤醒;返回整数表示timeout未用完时就被唤醒,此时可能接收到了一个信号量]

wait_event系列函数(作用:等待事件,置于休眠。区别从字面意思即可看出):

wait_event(queue, conditon);
wait_event_interruptible(queue, condition);
wait_event_timeout(queue, condition, timeout);
wait_event_interruptible_timeout(queue, condition, timeout);

*queue:作为等待队列头的等待队列被唤醒
*conditon:必须满足,否则阻塞
*【timeout和conditon相比,有更高优先级】
 

参考内核源码理解(在wait.h文件中)

#define __wait_event_interruptible_timeout(wq, condition, ret)
do {                                    
    DEFINE_WAIT(__wait);                        

    for (;;) {                          
        prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);  
        if (condition)                      
            break;                      
        if (!signal_pending(current)) {             
            ret = schedule_timeout(ret);            
            if (!ret)                   
                break;                  
            continue;                   
        }                           
        ret = -ERESTARTSYS;                 
        break;                          
    }                               
    finish_wait(&wq, &__wait);                  
} while (0)

/**
 * wait_event_interruptible_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_INTERRUPTIBLE) until the
 * @condition evaluates to true or a signal is received.
 * 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, -ERESTARTSYS if it
 * was interrupted by a signal, and the remaining jiffies otherwise
 * if the condition evaluated to true before the timeout elapsed.
 */

#define wait_event_interruptible_timeout(wq, condition, timeout)    
({                                  
    long __ret = timeout;                       
    if (!(condition))                       
        __wait_event_interruptible_timeout(wq, condition, __ret); 
    __ret;                              
})

标签:timeout,interruptible,wq,event,condition,wait
From: https://www.cnblogs.com/zxdplay/p/18440320

相关文章

  • C# 事件(Event)应用说明一
    一.C# 事件(Event)定义说明:C#事件(Event)是一种成员,用于将特定的事件通知发送给订阅者。事件通常用于实现观察者模式,它允许一个对象将状态的变化通知给其他对象,而不需要知道这些对象的具体细节。事件(Event) 基本上说是一个用户操作,或者是一些提示信息,如系统生成的通知、按键输......
  • C# 事件(Event)应用说明二
    简单示例如下一.界面显示: 二.源代码示例: //定义一个委托类型,用于事件处理程序publicdelegatevoidMyEventHandler(objectsender,EventArgse);//发布者类publicclassProcessBusinessClass{//声明事......
  • Event和Activity
    在JAINSLEE中,Event(事件)和Activity(活动)是两个核心概念,它们共同作用于系统的执行过程,但它们代表不同的含义和职责。让我们从最基础的层面来讲解它们的区别、联系,以及它们在JAINSLEE框架中的角色。1.Event(事件)1.1概念事件(Event)是JAINSLEE中的一个基本单元,用来......
  • 程序埋点(Event Tracking)
    程序埋点(EventTracking)是指在软件程序中嵌入记录用户行为或系统运行状态的代码,以便收集数据用于分析和监控。程序埋点通常用于分析用户行为、性能监控、问题排查、产品优化等目的。何时使用程序埋点:用户行为分析:了解用户在应用中的行为,例如点击量、页面访问次数等。性能监控:......
  • 易优CMS致命错误,联系技术支持:Call to undefined function eyPreventShell()-eyoucms
    当你遇到 core/helper.php 第146行左右出现致命错误,并且提示 CalltoundefinedfunctioneyPreventShell() 时,通常是因为某个自定义函数未被定义或未被正确引入。以下是一些具体的解决步骤:步骤1:检查函数定义定位 eyPreventShell 函数查找 eyPreventShell 函数的......
  • 【VUE】[Violation] Added non-passive event listener to a scroll-blocking...
    1.问题[Violation]Addednon-passiveeventlistenertoascroll-blocking<某些>事件.Considermarkingeventhandleras'passive'tomakethepagemoreresponsive.See<URL>译:[违规]向滚动阻止添加了非被动事件侦听器<某些>事件.请考虑将事件处理程序标记为“被......
  • WindowSystemEvent
    Qt中为WindowSystemEvent事件定义了处理函数Handler,通过宏定义和模版来声明定义----QT_DEFINE_QPA_EVENT_HANDLERMatches(25in1files)----qwindowsysteminterface.cpp(gui\kernel)line199:#defineQT_DEFINE_QPA_EVENT_HANDLER(ReturnType,HandlerName,...)\QT......
  • QtWidgetsApplication中的EventDispatcher的创建
    #include"QtWidgetsApplication1.h"#include<QtWidgets/QApplication>classGlobalEventFilter:publicQObject{public:virtualbooleventFilter(QObject*watched,QEvent*event)override{qDebug()<<"watched......
  • 【Azure Event Hub】关于Event Hub指标 ConsumerLag 的解释
    问题描述在使用AzureEventHub的过程中,需要监控消费端是否正常消费数据?而常规的指标只有IncomingMessage,OutgoingMessage,是否指标能表明当前EventHub消费滞后,即Incoming数量远远大于Outgoing呢?IncomingMessages :发布到事件中心的消息数。OutgoingMessages :从事件中心使......
  • 【Azure Event Hub】关于Event Hub指标 ConsumerLag 的解释
    问题描述在使用AzureEventHub的过程中,需要监控消费端是否正常消费数据?而常规的指标只有IncomingMessage,OutgoingMessage,是否指标能表明当前EventHub消费滞后,即Incoming数量远远大于Outgoing呢?IncomingMessages:发布到事件中心的消息数。OutgoingMessages:从事件中心......