关于函数wait_event_interruptible(wq, condition)

来源:互联网 发布:淘宝女装进货渠道 编辑:程序博客网 时间:2024/05/19 17:11

wait_event_interruptible(wq, condition),该函数修改task的状态为TASK_INTERRUPTIBLE,意味着该进程将不会继续运行直到被唤醒,然后被添加到等待队列wq中。

在wait_event_interruptible()中首先判断condition是不是已经满足,如果条件满足则直接返回0,否则调用__wait_event_interruptible(),并用__ret来存放返回值
#define wait_event_interruptible(wq, condition)          \
({                                                       \
    int __ret = 0;                                       \
    if (!(condition))                                    \
        __wait_event_interruptible(wq, condition, __ret);\
    __ret;                                               \
})

在无限循环中,__wait_event_interruptible()将本进程置为可中断的挂起状态,反复检查condition是否成立,如果成立则退出,如果不成立则继续休眠;条件满足后,即把本进程运行状态置为运行态

,并将__wait从等待队列中清除掉,从而进程能够调度运行。如果进程当前有异步信号(POSIX的),则返回-ERESTARTSYS。
#define __wait_event_interruptible(wq, condition, ret)      \
do {                                                        \
    DEFINE_WAIT(__wait);                                    \
    for (;;) {                                              \
        prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);  \
        if (condition)                                      \
            break;                                          \
        if (!signal_pending(current)) {                     \
            schedule();                                     \
            continue;                                       \
        }                                                   \
        ret = -ERESTARTSYS;                                 \
        break;                                              \
    }                                                       \
    finish_wait(&wq, &__wait);                              \
} while (0)

 

以下是在linux下OV7725驱动(s3c2440camif.c)中的一个例子

定义等待队列
wait_queue_head_t cmdqueue;(s3c2440camif.h中定义的)
初始化等待队列
static int __init camif_probe(struct platform_device *dev)
{
 ......
 pdev->cmdcode = CAMIF_CMD_NONE;
 init_waitqueue_head(&pdev->cmdqueue);
 ......
}
调用等待队列
static int start_capture(struct s3c2440camif_dev * pdev, int stream)
{
 ......
 if (stream == 0)
 {
  pdev->cmdcode = CAMIF_CMD_STOP;
  ret = wait_event_interruptible(pdev->cmdqueue, pdev->cmdcode == CAMIF_CMD_NONE);
 }
 return ret;
 ......
}
在中断处理函数中唤醒等待队列
static irqreturn_t on_camif_irq_c(int irq, void * dev)
{
 ......
 pdev->cmdcode = CAMIF_CMD_NONE;
 wake_up(&pdev->cmdqueue);
 ......
}
static irqreturn_t on_camif_irq_p(int irq, void * dev)
{
 ......
 pdev->cmdcode = CAMIF_CMD_NONE;
 wake_up(&pdev->cmdqueue);
 ......
}
在下面函数中调用start_capture()
static ssize_t camif_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
{
 ......
 if(start_capture(pdev, 0) != 0)
 {
  return -ERESTARTSYS;
 }
 ......
}

 

参考资料:

http://www.ibm.com/developerworks/cn/linux/kernel/sync/index.html

http://www.360doc.com/content/10/1004/12/1345608_58325218.shtml

http://my.chinaunix.net/space.php?uid=25324849&do=blog&id=187679

0 0