为什么我们需要在poll中调用poll_wait?

dem*_*guy 10 linux epoll system-calls linux-device-driver linux-kernel

在LDD3中,我看到了这样的代码

static unsigned int scull_p_poll(struct file *filp, poll_table *wait)
{
    struct scull_pipe *dev = filp->private_data;
    unsigned int mask = 0;

    /*
     * The buffer is circular; it is considered full
     * if "wp" is right behind "rp" and empty if the
     * two are equal.
     */
    down(&dev->sem);
    poll_wait(filp, &dev->inq,  wait);
    poll_wait(filp, &dev->outq, wait);
    if (dev->rp != dev->wp)
        mask |= POLLIN | POLLRDNORM;    /* readable */
    if (spacefree(dev))
        mask |= POLLOUT | POLLWRNORM;   /* writable */
    up(&dev->sem);
    return mask;
}
Run Code Online (Sandbox Code Playgroud)

但它说poll_wait不会等待并立即返回.那为什么我们需要打电话呢?为什么我们不能只返回面具?

Gil*_*ton 13

poll_wait将您的设备(由"struct file"表示)添加到可以唤醒进程的列表中.

这个想法是该过程可以使用poll(或select或epoll等)将一堆文件描述符添加到它希望等待的列表中.调用每个驱动程序的轮询条目.每个人都将自己(通过poll_wait)添加到服务员列表中.

然后核心内核在一个地方阻止进程.这样,任何一个设备都可以唤醒进程.如果返回非零掩码位,这意味着这些"准备"属性(读/写/等)申请现在.

所以,在伪代码中,它大致是这样的:

foreach fd:
    find device corresponding to fd
    call device poll function to setup wait queues (with poll_wait) and to collect its "ready-now" mask

while time remaining in timeout and no devices are ready:
    sleep

return from system call (either due to timeout or to ready devices)
Run Code Online (Sandbox Code Playgroud)