zoukankan      html  css  js  c++  java
  • linux内核--wait_event_interruptible_timeout()函数分析(转)

    原文: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;
    })
    ---------------------
    作者:HelloWuyp
    来源:CSDN
    原文:https://blog.csdn.net/wuyongpeng0912/article/details/45723657
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    js加法计算器
    js基础语句
    箭头点击左右滚动-18
    返回头部,滚动显示-17
    图片定位一个地方
    最值一看专题图片轮播图-16
    右侧常用浮动导航,返回顶部-15
    产业带多种轮播效果,头部效果-14
    分辨率判断-13
    图片自动滚动,鼠标滑过悬停-12
  • 原文地址:https://www.cnblogs.com/lh03061238/p/11221969.html
Copyright © 2011-2022 走看看