等待队列代码实现

    科技2024-10-27  16

    hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos) { DECLARE_WAITQUEUE(wait, current); /* 定义一个等待队列 */ unsigned long data; ssize_t retval; struct hpet_dev *devp; devp = file->private_data; /* 私有化数据 */ if (!devp->hd_ireqfreq) return -EIO; if (count < sizeof(unsigned long)) return -EINVAL; add_wait_queue(&devp->hd_waitqueue, &wait); /* 添加到等待队列头 */ for ( ; ; ) { set_current_state(TASK_INTERRUPTIBLE); /* 设置任务状态 */ spin_lock_irq(&hpet_lock); data = devp->hd_irqdata; devp->hd_irqdata = 0; spin_unlock_irq(&hpet_lock); if (data) break; else if (file->f_flags & O_NONBLOCK) { retval = -EAGAIN; goto out; } else if (signal_pending(current)) { /* 判断是否为信号引起的唤醒 */ retval = -ERESTARTSYS; goto out; } schedule(); /* 进行一次任务切换 */ } retval = put_user(data, (unsigned long __user *)buf); if (!retval) retval = sizeof(unsigned long); out: __set_current_state(TASK_RUNNING); remove_wait_queue(&devp->hd_waitqueue, &wait); return retval; }
    Processed: 0.026, SQL: 8