【发布时间】:2020-04-30 02:41:40
【问题描述】:
linux中有一个同步轮询多个设备文件的概念,我正在尝试了解它是如何工作的。
在 linux 2.6.23 源驱动程序/char/random.c 中,我看到以下代码
static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);
static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);
static unsigned int
random_poll(struct file *file, poll_table * wait)
{
unsigned int mask;
poll_wait(file, &random_read_wait, wait);
poll_wait(file, &random_write_wait, wait);
mask = 0;
if (input_pool.entropy_count >= random_read_wakeup_thresh)
mask |= POLLIN | POLLRDNORM;
if (input_pool.entropy_count < random_write_wakeup_thresh)
mask |= POLLOUT | POLLWRNORM;
return mask;
}
poll_table 在 include/linux/poll.h 中定义如下
typedef void (*poll_queue_proc)(struct file *, wait_queue_head_t *, struct poll_table_struct *);
typedef struct poll_table_struct {
poll_queue_proc qproc;
} poll_table;
我在一本书(第 5 章,Essential Linux Device Drivers,Venkateswaran)中看到“poll_table 是轮询数据的设备驱动程序所拥有的等待队列表”。但消息来源说它只是一个函数指针。而且我找不到这个函数 qproc 在做什么。 下面是在include/linux/poll.h中定义的函数poll_wait。
static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
{
if (p && wait_address)
p->qproc(filp, wait_address, p);
}
在书中它说(关于鼠标的示例字符驱动程序),“mouse_poll() 使用库函数 poll_wait() 将等待队列 (mouse_wait) 添加到内核 poll_table 并进入睡眠状态。 "所以 poll_wait 可以休眠,但是在上面的 random_poll() 函数中,我们看到了两个连续的 poll_wait 函数。那么 random_poll 是否按顺序轮询读写可用性并将掩码发送到应用程序?如果有人可以向我展示 poll_queue_proc 函数的示例,我将不胜感激。我在linux驱动源码中找不到(应该只出现在应用程序中吗?)。
【问题讨论】:
标签: linux linux-device-driver polling device-driver