poll、poll_wait函数与等待队列DECLARE_WAIT_QUEUE_HEAD、wake_up_interruptible()、wait_event_interruptible()使用

一、 poll()函数

应用层使用select()和poll()系统函数查询是否可对设备进行无阻塞的访问时,会引发设备驱动中的poll()函数被执行。看一个例子:
应用层代码如下:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>

/*
 * ./keyTest /dev/my_key0
 *
 */
int main(int argc, char **argv)
{
	int fd;
	int val;
	struct pollfd fds[1];
	int timeout_ms = 5000;
	int ret;
	
	/* 1. 判断参数 */
	if (argc != 2) 
	{
		printf("Usage: %s <dev>\n", argv[0]);
		return -1;
	}

	/* 2. 打开文件 */
	fd = open(argv[1], O_RDWR);
	if (fd == -1)
	{
		printf("can not open file %s\n", argv[1]);
		return -1;
	}

	fds[0].fd = fd;
	fds[0].events = POLLIN;
	

	while (1)
	{
		/* 3. 读文件 */
		ret = poll(fds, 1, timeout_ms);
		if ((ret == 1) && (fds[0].revents & POLLIN))
		{
			read(fd, &val, 4);
			printf("get button : 0x%x\r\n", val);
		}
		else
		{
			printf("timeout\r\n");
		}
	}
	
	close(fd);
	
	return 0;
}

当应用层执行到**ret = poll(fds, 1, timeout_ms)**这句时最终会调用对应驱动文件中的poll函数。
by 面朝大海0902

二、驱动文件中poll()函数及poll_wait()函数

我们还是以一个例子说明下,驱动文件代码片段如下:

static unsigned int key_drv_poll(struct file *fp, poll_table * wait)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	poll_wait(fp, &key_wait, wait);//非阻塞函数
	
	return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
}

static struct file_operations key_drv =
{
	.owner = THIS_MODULE,
	.read  = key_drv_read,
	.poll  = key_drv_poll,
};

这里file_operations结构体中的poll成员就是上面我们说的驱动文件的poll函数,poll函数一个核心就是**poll_wait()**函数,这个函数是一个非阻塞函数,这点非常重要,很多人看到函数里面有个wait关键字以为执行到这里会堵塞住,其实不对。

static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
{
	if (p && p->_qproc && wait_address)
		p->_qproc(filp, wait_address, p);
}

使用这个函数一般要配合等待队列使用,poll_wait()函数的作用是将对应的等待队列头添加到poll_table,有人会问如果驱动的poll()函数不调用poll_wait(),即屏蔽掉该行会怎么样?答案是应用层poll函数会一直休眠下去直到它超时,可以参考下面第四节实验验证部分。
原因这里可以简单说下,以按键驱动为例,按键按下触发中断,按键值被更新,屏蔽掉poll_wait()后,应用进程还是一直在休眠状态无法立即从休眠状态变为可执行态并被调度执行,直到poll函数超时。
by 面朝大海0902

三、等待队列

DECLARE_WAIT_QUEUE_HEAD() —创建等待队列头;
wake_up_interruptible() —将等待队列中的进程变为可运行态,具体进程什么时候被执行,取决于调度程序;
wait_event_interruptible(wq, condition) —使调用进程在等待队列上睡眠,直到满足给定condition条件为止;

static DECLARE_WAIT_QUEUE_HEAD(key_wait);

驱动程序开始我们会创建等待队列

static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{
	int value;
	struct gpio_key *ptr_gpio_key_temp = dev_id;
	value = gpiod_get_value(ptr_gpio_key_temp->gpiod);
	g_key_value = (ptr_gpio_key_temp->gpio << 8) | value;
	printk(KERN_INFO "g_key_value is %d \r\n", g_key_value);
	put_key(g_key_value);
	wake_up_interruptible(&key_wait);
	return IRQ_HANDLED;
}

当按键的GPIO被按下以后中断函数被执行,我们通过wake_up_interruptible()将等待队列中的进程变为可运行态,这样刚才调用poll_wait()函数其对应的应用进程才会被调度执行。

static int key_drv_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	wait_event_interruptible(key_wait, !is_key_buf_empty());
	g_key_value = get_key();
	copy_to_user(buf, &g_key_value, 4);
	return 4;
}

驱动read函数读取按键值,我们使用wait_event_interruptible()使进程在不满足条件condition时休眠
by 面朝大海0902
完整驱动demo我们贴出来,demo里面有2个key键,不同开发板key键的GPIO配置不相同(环形缓冲区部分与本文讨论的内容没有关系,参考了成熟代码)

#include <linux/module.h>
#include <linux/poll.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>

struct gpio_key{
	int gpio;
	struct gpio_desc *gpiod;
	int flag;
	int irq;
};

static struct gpio_key *ptr_gpio_key;

static int major =0;
static struct class *key_class;
static int g_key_value = 0;


/* 环形缓冲区 */
#define BUF_LEN 128
static int g_keys[BUF_LEN];
static int r, w;

#define NEXT_POS(x) ((x+1) % BUF_LEN)

static int is_key_buf_empty(void)
{
	return (r == w);
}

static int is_key_buf_full(void)
{
	return (r == NEXT_POS(w));
}

static void put_key(int key)
{
	if (!is_key_buf_full())
	{
		g_keys[w] = key;
		w = NEXT_POS(w);
	}
}

static int get_key(void)
{
	int key = 0;
	if (!is_key_buf_empty())
	{
		key = g_keys[r];
		r = NEXT_POS(r);
	}
	return key;
}

static DECLARE_WAIT_QUEUE_HEAD(key_wait);

static int key_drv_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	wait_event_interruptible(key_wait, !is_key_buf_empty());
	g_key_value = get_key();
	copy_to_user(buf, &g_key_value, 4);
    printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	return 4;
}


static unsigned int key_drv_poll(struct file *fp, poll_table * wait)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	poll_wait(fp, &key_wait, wait);//非阻塞函数
	
	return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
}

static struct file_operations key_drv =
{
	.owner = THIS_MODULE,
	.read  = key_drv_read,
	.poll  = key_drv_poll,
};

static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{
	int value;
	struct gpio_key *ptr_gpio_key_temp = dev_id;
	value = gpiod_get_value(ptr_gpio_key_temp->gpiod);
	g_key_value = (ptr_gpio_key_temp->gpio << 8) | value;
	printk(KERN_INFO "g_key_value is %d \r\n", g_key_value);
	put_key(g_key_value);
	wake_up_interruptible(&key_wait);
	return IRQ_HANDLED;
}

static int key_probe(struct platform_device *pdev)
{
	int count = 0;
	int i=0;
	enum of_gpio_flags flag;
	struct device_node *node = pdev->dev.of_node;
	
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	count = of_gpio_count(node);
	ptr_gpio_key = kzalloc(sizeof(struct gpio_key)*count, GFP_KERNEL);
	
	for(i=0;i<count;i++)
	{
		ptr_gpio_key[i].gpio = of_get_gpio_flags(node, i, &flag);
		if(ptr_gpio_key[i].gpio < 0)
		{
			printk(KERN_ERR "of_get_gpio_flags is err\r\n");
		}
		ptr_gpio_key[i].gpiod = gpio_to_desc(ptr_gpio_key[i].gpio);
		ptr_gpio_key[i].flag = flag & OF_GPIO_ACTIVE_LOW;
		ptr_gpio_key[i].irq = gpio_to_irq(ptr_gpio_key[i].gpio);
		request_irq(ptr_gpio_key[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "gpio_key", &ptr_gpio_key[i]);
	}
	
	major = register_chrdev(0, "my_keydrv", &key_drv);
	key_class = class_create(THIS_MODULE, "my_key_class");
	if(IS_ERR(key_class))
	{
		printk(KERN_ERR "class_create is err\r\n");
	}
	
	device_create(key_class, NULL, MKDEV(major, 0), NULL, "my_key%d", 0);
	
	return 0;	
}



static int key_remove(struct platform_device *pdev)
{
	int count = 0;
	int i = 0;
	struct device_node *node = pdev->dev.of_node;

	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	count = of_gpio_count(node);

	device_destroy(key_class, MKDEV(major, 0));
	class_destroy(key_class);
	unregister_chrdev(major, "my_keydrv");
	for(i=0;i<count;i++)
	{
		free_irq(ptr_gpio_key[i].irq, &ptr_gpio_key[i]);
	}

	kfree(ptr_gpio_key);
	return 0;	
}


static const struct of_device_id my_key[] =
{
	{.compatible = "my,key_driver"},
	{},
};


static struct platform_driver key_driver =
{
	.probe  = key_probe,
	.remove = key_remove,
	.driver = 
	{
		.name = "key_gpio",
		.of_match_table = my_key,
	},
};


static int __init my_key_init(void)
{
	int result;
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	result = platform_driver_register(&key_driver);
	return result;
}


static void __exit my_key_exit(void)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	platform_driver_unregister(&key_driver);
}

module_init(my_key_init);
module_exit(my_key_exit);

MODULE_LICENSE("GPL");

四、实验验证

1、 正常驱动程序运行

运行应用./keyTest /dev/my_key0,等待一会儿再按键

[ 3818.372815] /home/book/code/test/mygpio-key.c key_drv_poll line is 92
[ 3823.375302] /home/book/code/test/mygpio-key.c key_drv_poll line is 92
timeout
[ 3823.383494] /home/book/code/test/mygpio-key.c key_drv_poll line is 92

[ 3828.388671] /home/book/code/test/mygpio-key.c key_drv_poll line is 92
timeout
[ 3828.397540] /home/book/code/test/mygpio-key.c key_drv_poll line is 92

[ 3830.476218] g_key_value is 33024
[ 3830.479938] /home/book/code/test/mygpio-key.c key_drv_poll line is 92
[ 3830.488389] /home/book/code/test/mygpio-key.c key_drv_read line is 85
get button : 0x8100
[ 3830.496950] /home/book/code/test/mygpio-key.c key_drv_poll line is 92

[ 3830.509048] g_key_value is 33025
[ 3830.512819] /home/book/code/test/mygpio-key.c key_drv_poll line is 92
[ 3830.520937] /home/book/code/test/mygpio-key.c key_drv_read line is 85
get button : 0x8101
[ 3830.528997] /home/book/code/test/mygpio-key.c key_drv_poll line is 92

可以看到不按键时poll函数5s超时,按键按下后先执行gpio_key_isr,然后应用层进程变为可运行态会被调度执行,poll函数返回值POLLIN,if语句成立,调用read函数,进而驱动的read函数被调用,同时wait_event_interruptible()函数condition条件满足,将key值返回给应用层,应用层一次调用poll函数,驱动层poll函数可能被调用2次(刚开始执行一次,超时时执行一次)。
可以看下韦老师的分析:韦东山:Linux驱动基石之POLL机制

2、 屏蔽poll_wait()函数

屏蔽key_drv_poll()函数中的poll_wait(fp, &key_wait, wait)运行应用程序keyTest ,并按键

static unsigned int key_drv_poll(struct file *fp, poll_table * wait)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	//poll_wait(fp, &key_wait, wait);//非阻塞函数
	
	return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
}

打印如下:

[ 4144.480320] /home/book/code/test/mygpio-key.c key_drv_poll line is 92
[ 4149.485443] /home/book/code/test/mygpio-key.c key_drv_poll line is 92
timeout
[ 4149.493622] /home/book/code/test/mygpio-key.c key_drv_poll line is 92

[ 4151.599603] g_key_value is 33024
[ 4151.726422] g_key_value is 33025
[ 4154.494890] /home/book/code/test/mygpio-key.c key_drv_poll line is 92
[ 4154.501592] /home/book/code/test/mygpio-key.c key_drv_read line is 85
get button : 0x8100
[ 4154.515361] /home/book/code/test/mygpio-key.c key_drv_poll line is 92

[ 4154.523727] /home/book/code/test/mygpio-key.c key_drv_read line is 85
get button : 0x8101
[ 4154.531437] /home/book/code/test/mygpio-key.c key_drv_poll line is 92

可以看到按键按下后中断gpio_key_isr依然被执行,但是应用进程依然休眠,直到5s超时时poll函数返回POLLIN,if语句成立read函数才被执行,可以看到屏蔽poll_wait函数会导致应用进程无法第一时间被执行,屏蔽poll_wait函数后硬件中断到达后无法第一时间将应用进程变为可运行态,导致应用进程一直休眠直到poll函数超时。
by 面朝大海0902

3、屏蔽wake_up_interruptible()函数

static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{
... ...
	put_key(key);
	//wake_up_interruptible(&gpio_key_wait);
	
	return IRQ_HANDLED;
}

执行结果打印如下:

[ 4921.589542] /home/book/code/test/mygpio-key.c key_drv_poll line is 92
[ 4926.593101] /home/book/code/test/mygpio-key.c key_drv_poll line is 92
timeout
[ 4926.605410] /home/book/code/test/mygpio-key.c key_drv_poll line is 92

[ 4928.511399] g_key_value is 33024
[ 4928.652796] g_key_value is 33025
[ 4931.609713] /home/book/code/test/mygpio-key.c key_drv_poll line is 92
[ 4931.616786] /home/book/code/test/mygpio-key.c key_drv_read line is 85
get button : 0x8100
[ 4931.626061] /home/book/code/test/mygpio-key.c key_drv_poll line is 92
[ 4931.633066] /home/book/code/test/mygpio-key.c key_drv_read line is 85
get button : 0x8101
[ 4931.641407] /home/book/code/test/mygpio-key.c key_drv_poll line is 92

和情况2类似,应用程序无法第一时间得到调度。

4、wait_event_interruptible()函数condition不满足

将condition置为假,如下:

static ssize_t gpio_key_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
...
	wait_event_interruptible(gpio_key_wait, 0);
...
}

执行结果打印如下:

[ 5850.349077] /home/book/code/test/mygpio-key.c key_drv_poll line is 93
[ 5855.349645] /home/book/code/test/mygpio-key.c key_drv_poll line is 93
timeout
[ 5855.358149] /home/book/code/test/mygpio-key.c key_drv_poll line is 93

[ 5857.324008] g_key_value is 33024
[ 5857.327604] /home/book/code/test/mygpio-key.c key_drv_poll line is 93
[ 5857.462123] g_key_value is 33025
[ 5870.614619] g_key_value is 33024
[ 5870.757192] g_key_value is 33025
[ 5873.257836] g_key_value is 28160
[ 5873.530629] g_key_value is 28161

可以看到只要按键,应用进程执行到Read函数会一直被堵塞住,导致后面按键也没有用,仅打印中断函数执行的log。
by 面朝大海0902
按键程序我们也可以使用异步通知的方式进行,详见Linux异步通知—signal()、fcntl()函数介绍与使用

  • 11
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值