等待队列在Linux内核中用来阻塞或唤醒一个进程,也可以用来同步对系统资源的访问,还可以实现延迟功能
等待队列接口函数介绍:
#include <linux/wait.h> //头文件包含
1.定义、初始化等待队列(指向等待队列链表)
定义一个等待队列头
wait_queue_head_t my_queue;
初始一个等待队列头
init_waitqueue_head(&my_queue);
定义并初始化一个等待队列头
DECLARE_WAIT_QUEUE_HEAD(my_queue);
2.进程的睡眠操作——条件睡眠
判断condition条件,决定是否将当前进程推入等待队列
wait_event(wait_queue_head_t wq, int condition);
wait_event_interruptible /*可以被系统消息打断*/
(wait_queue_head_t wq,int condition);
wait_event_timeout(wait_queue_head_t wq, int
condition, long timeout);
wait_event_interruptiblble_timeout(wait_queue_head_t wq,
int condition, long timeout);
参数wq:表示等待队列头
参数condition:阻塞条件,为假(0)则进入休眠直到wake_up且condition为真条件成立才退出
参数timeout:表示睡眠指定时长(时钟滴答度量,eg.延时2秒=2*HZ)后,自动转入唤醒状态
进程的睡眠操作——无条件睡眠
//将当前进程推入等待队列将其睡眠,wake_up唤醒
sleep_on(wait_queue_head_t *q);
interruptible_sleep_on(wait_queue_head_t *q);
long sleep_on_timeout(wait_queue_head_t *q, long timeout);
long interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout);
参数wq:表示等待队列头
参数timeout:表示睡眠指定时长后,自动转入唤醒状态
3.进程唤醒函数
wake_up(wait_queue_head_t *wq);
wake_up_interruptible(wait_queue_head_t *wq);
注意事项:
1.唤醒函数和导致睡眠函数要配对使用,如果导致睡眠函数使用带interruptible的,则唤醒函数也要使用interruptible的。
2.在使用wake_up唤醒进程之前要将wait_event中的condition变量的值赋为真,否则该进程被唤醒后会立即再次进入睡眠
等待队列应用实例:
代码实例:
应用层代码:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
char *devname = "/dev/key1_eint";
int fd;
unsigned char key;
fd = open(devname, O_RDWR);
while(1){
read(fd, &key, sizeof(key));
printf("the key = %d\n",key);
}
close(fd);
}
驱动层:
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/gpio.h>
#include <mach/regs-gpio.h>
#define EINT_DEVICE_ID 1
#define DRIVER_NAME "key1_eint"
#define err(msg) printk(KERN_ERR "%s: " msg "\n", DRIVER_NAME)
#define __debug(fmt, arg...) printk(KERN_DEBUG fmt, ##arg)
#define GPH3CON (unsigned long)(S5PV210_GPH3_BASE+ 0x00)
#define GPH3DAT (unsigned long)(S5PV210_GPH3_BASE + 0x04)
#define GPH2UP (unsigned long)(S5PV210_GPH2_BASE + 0x08)
static int major = 0;
static int minor = 0;
struct class *key_class;
static struct device *key_device;
/*定义等待队列头,该等待队列头属于该驱动程序*/
static wait_queue_head_t wait_queue;
static unsigned char key;
irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
key = (unsigned int)dev_id;
wake_up_interruptible(&wait_queue); //唤醒等待队列
return IRQ_HANDLED;
}
static void key_io_port_init(void)
{
unsigned long reg_val;
reg_val = readl(GPH3CON);
reg_val &= ~((0x0f<<0) | (0x0f<<4));
reg_val |= ((0x01<<0) | (0x01<<4));
writel(reg_val, GPH3CON);
reg_val = readl(GPH3DAT);
reg_val &= ~((0x01<<0) | (0x01<<1));
writel(reg_val, GPH3DAT);
reg_val = readl(GPH2UP);
reg_val &= ~(0x03<<8);
reg_val |= 0x02<<8;
writel(reg_val, GPH2UP);
}
static ssize_t key_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
int key_num;
int cpy_len;
int retval;
/* 该函数体内部会定义struct wait_queue结构体变量,并将将当前进程
* 添加到队列中睡眠,(wait_queue_head_t为等待队列链表的头,struct
* wait_queue记录着链表节点信息)
*/
interruptible_sleep_on(&wait_queue);
key_num = key; //读取键值
cpy_len = min(sizeof(key_num), count);
retval = copy_to_user(buf, &key_num, cpy_len);
return (cpy_len - retval);
}
/* Driver Operation structure */
static struct file_operations key_fops = {
.owner = THIS_MODULE,
.read = key_read,
};
static int __init key_eint_init(void)
{
int retval;
key_io_port_init();
init_waitqueue_head(&wait_queue); //初始化等待队列头
retval = set_irq_type(IRQ_EINT(20),IRQ_TYPE_EDGE_FALLING);
if(retval){
err("IRQ_EINT20 set irq type failed");
goto error;
}
retval = request_irq(IRQ_EINT(20), buttons_interrupt, IRQF_DISABLED,
"KEY1", (void *)EINT_DEVICE_ID);
if(retval){
err("request eint20 failed");
goto error;
}
major = register_chrdev(major, DRIVER_NAME, &key_fops);
if(major < 0){
err("register char device fail");
retval = major;
goto error_register;
}
key_class=class_create(THIS_MODULE,DRIVER_NAME);
if(IS_ERR(key_class)){
err("class create failed!");
retval = PTR_ERR(key_class);
goto error_class;
}
key_device=device_create(key_class,NULL, MKDEV(major, minor), NULL,DRIVER_NAME);
if(IS_ERR(key_device)){
err("device create failed!");
retval = PTR_ERR(key_device);
goto error_device;
}
__debug("register myDriver OK! Major = %d\n", major);
return 0;
error_device:
class_destroy(key_class);
error_class:
unregister_chrdev(major, DRIVER_NAME);
error_register:
free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID);
error:
return retval;
}
static void __exit key_eint_exit(void)
{
free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID);
unregister_chrdev(major, DRIVER_NAME);
device_destroy(key_class,MKDEV(major, minor));
class_destroy(key_class);
return;
}
module_init(key_eint_init);
module_exit(key_eint_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("eric");
|