一 what
有过嵌入式实时操作系统开发的同学,对这三个概念应该很容易理解,这里做一个简单的介绍
多任务访问临界区时,一定要做好隔离保护的工作,否则临界区内容会被别的任务修改,导致出现未知的错误。为保护临界区资源共享的问题,我们引出同步的概念,因此同步实际上就是保护共享资源的一种策略方法,当然实现同步的方法有好几种,我们看一下常见的三种
- 原子操作
在执行过程中不会被别的任务(进程/线程)打断的操作
atomic_t v = ATOMIC_INIT(0); //定义原子变量v并初始化为0
atomic_read(atomic_t *v); //返回原子变量的值
void atomic_inc(atomic_t *v); //原子变量加1
void atomic_dec(atomic_t *v); //原子变量减1
int atomic_dec_and_test(atomic_t *v); //自减操作后测试其是否为0,为0返回true,否则返回false
- 信号量
/* 定义信号量 */
struct semaphore sem;
/* 初始化信号量 */
void sema_int(struct semaphore *sem, int val);
void int_MUTEX(struct semaphore *sem); //初始化为0
static DECLARE_MUTEX(button_lock); //定义互斥锁
/* 获得信号量 */
void down(struct semaphore *sem);
int down_interruptible(struct semaphore *sem);
void down_trylock(struct semaphore *sem);
/* 释放信号量 */
void up(struct semaphore *sem);
- 阻塞
阻塞: 在执行设备操作时若不能获得资源则挂起进程,知道满足可操作的条件后再进行操作,被挂起的进程进入休眠状态,被从调度器的运行队列移走,直到等待的条件满足
非阻塞操作: 进程在不能进行设备操作时并不挂起,它或者放弃,或者不停地查询,直至可进行操作为止
fd = open("...", O_RDWR | O_NONBLOCK);
二 why
为了避免临界区被异常修改,我们需要对临界区资源做保护;对于字符设备驱动程序来说,我们只能允许某个时刻只打开一个设备驱动,不能同时打开好几个,所以我们需要这种同步机制
三 how
a. 原子操作
先定义一个原子操作
//定义原子变量canopen并初始化为1
static atomic_t canopen = ATOMIC_INIT(1);
在open函数中,判断该原子数的状态
static int sixth_drv_open(struct inode *inode, struct file *file)
{
if (!atomic_dec_and_test(&canopen))
{
printk("can not open\n");
atomic_inc(&canopen);
return -EBUSY;
}
/* 配置GPF0,2为输出引脚 */
/* 配置GPG3,11为输出引脚 */
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
return 0;
}
在close函数中,原子数加1操作
int sixth_drv_close(struct inode *inode, struct file *file)
{
atomic_inc(&canopen);
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT11, &pins_desc[2]);
free_irq(IRQ_EINT19, &pins_desc[3]);
return 0;
}
完整代码如下
#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 <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>
static struct class *sixthdrv_class;
static struct class_device *sixthdrv_class_dev;
volatile unsigned long *gpfcon;
volatile unsigned long *gpgcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgdat;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,sixth_drv_read将它清0 */
static volatile int ev_press = 0;
static unsigned char key_val;
//定义原子变量canopen并初始化为1
static atomic_t canopen = ATOMIC_INIT(1);
struct pin_desc
{
unsigned int pin;
unsigned int key_val;
};
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
struct pin_desc pins_desc[4] =
{
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04}
};
static struct fasync_struct *button_async;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
//printk("irq=%d\n", irq);
pinval = s3c2410_gpio_getpin(pindesc->pin);
if (pinval)
{
/* 松开 */
key_val = 0x80 | pindesc->key_val;
}
else
{
/* 按下 */
key_val = pindesc->key_val;
}
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
kill_fasync(&button_async, SIGIO, POLL_IN);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int sixth_drv_open(struct inode *inode, struct file *file)
{
if (!atomic_dec_and_test(&canopen))
{
printk("can not open\n");
atomic_inc(&canopen);
return -EBUSY;
}
/* 配置GPF0,2为输出引脚 */
/* 配置GPG3,11为输出引脚 */
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
return 0;
}
static int sixth_drv_read(struct file *filp, char __user *buf,
size_t size, loff_t *ppos)
{
/* 返回4个引脚电平状态 */
if (size != 1)
return -EINVAL;
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
ev_press = 0;
return (sizeof(key_val));
}
int sixth_drv_close(struct inode *inode, struct file *file)
{
atomic_inc(&canopen);
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT11, &pins_desc[2]);
free_irq(IRQ_EINT19, &pins_desc[3]);
return 0;
}
static unsigned int sixth_drv_poll(struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait); // 不会立即休眠
if (ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int sixth_drv_fasync(int fd, struct file *filp, int on)
{
printk("driver: app use it\n");
return fasync_helper(fd, filp, on, &button_async);
}
static struct file_operations sixth_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = sixth_drv_open,
.read = sixth_drv_read,
.release = sixth_drv_close,
.poll = sixth_drv_poll,
.fasync = sixth_drv_fasync,
};
static int major;
static int sixth_drv_init(void)
{
major = register_chrdev(0, "my_sixth_drv", &sixth_drv_fops);
sixthdrv_class = class_create(THIS_MODULE, "mysixthdrv");
sixthdrv_class_dev = class_device_create(sixthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/xyz */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
static void sixth_drv_exit(void)
{
unregister_chrdev(major, "my_sixth_drv");
class_device_unregister(sixthdrv_class_dev);
class_destroy(sixthdrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
module_init(sixth_drv_init);
module_exit(sixth_drv_exit);
MODULE_LICENSE("GPL");
b. 信号量
初始化信号量
static DECLARE_MUTEX(button_lock);
在open函数中获取信号量状态
static int sixth1_drv_open(struct inode *inode, struct file *file)
{
/* 获得信号量 */
down(&button_lock);
/* 配置GPF0,2为输出引脚 */
/* 配置GPG3,11为输出引脚 */
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
return 0;
}
在close函数中,释放信号量
int sixth1_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT11, &pins_desc[2]);
free_irq(IRQ_EINT19, &pins_desc[3]);
/* 释放信号量 */
up(&button_lock);
return 0;
}
完整代码如下
#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 <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>
static struct class *sixth1drv_class;
static struct class_device *sixth1drv_class_dev;
volatile unsigned long *gpfcon;
volatile unsigned long *gpgcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgdat;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,sixth1_drv_read将它清0 */
static volatile int ev_press = 0;
static unsigned char key_val;
//定义原子变量canopen并初始化为1
static DECLARE_MUTEX(button_lock);
struct pin_desc
{
unsigned int pin;
unsigned int key_val;
};
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
struct pin_desc pins_desc[4] =
{
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04}
};
static struct fasync_struct *button_async;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
//printk("irq=%d\n", irq);
pinval = s3c2410_gpio_getpin(pindesc->pin);
if (pinval)
{
/* 松开 */
key_val = 0x80 | pindesc->key_val;
}
else
{
/* 按下 */
key_val = pindesc->key_val;
}
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
kill_fasync(&button_async, SIGIO, POLL_IN);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int sixth1_drv_open(struct inode *inode, struct file *file)
{
/* 获得信号量 */
down(&button_lock);
/* 配置GPF0,2为输出引脚 */
/* 配置GPG3,11为输出引脚 */
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
return 0;
}
static int sixth1_drv_read(struct file *filp, char __user *buf,
size_t size, loff_t *ppos)
{
/* 返回4个引脚电平状态 */
if (size != 1)
return -EINVAL;
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
ev_press = 0;
return (sizeof(key_val));
}
int sixth1_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT11, &pins_desc[2]);
free_irq(IRQ_EINT19, &pins_desc[3]);
/* 释放信号量 */
up(&button_lock);
return 0;
}
static unsigned int sixth1_drv_poll(struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait); // 不会立即休眠
if (ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int sixth1_drv_fasync(int fd, struct file *filp, int on)
{
printk("driver: app use it\n");
return fasync_helper(fd, filp, on, &button_async);
}
static struct file_operations sixth1_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = sixth1_drv_open,
.read = sixth1_drv_read,
.release = sixth1_drv_close,
.poll = sixth1_drv_poll,
.fasync = sixth1_drv_fasync,
};
static int major;
static int sixth1_drv_init(void)
{
major = register_chrdev(0, "my_sixth1_drv", &sixth1_drv_fops);
sixth1drv_class = class_create(THIS_MODULE, "mysixth1drv");
sixth1drv_class_dev = class_device_create(sixth1drv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/xyz */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
static void sixth1_drv_exit(void)
{
unregister_chrdev(major, "my_sixth1_drv");
class_device_unregister(sixth1drv_class_dev);
class_destroy(sixth1drv_class);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
module_init(sixth1_drv_init);
module_exit(sixth1_drv_exit);
MODULE_LICENSE("GPL");
c. 阻塞
struct file结构体有一个字段表示文件操作类型,是非阻塞还是阻塞, file->flags,我们在open函数中对这个标志进行判断
static int sixth2_drv_open(struct inode *inode, struct file *file)
{
if (file->flags & O_NONBLOCK)
{
if (down_trylock(&button_lock))
return -EBUSY;
}
else
{
/* 获得信号量 */
down(&button_lock);
/* 配置GPF0,2为输出引脚 */
/* 配置GPG3,11为输出引脚 */
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
}
return 0;
}
read函数中,也需要做对应修改
static int sixth2_drv_read(struct file *filp, char __user *buf,
size_t size, loff_t *ppos)
{
/* 返回4个引脚电平状态 */
if (size != 1)
return -EINVAL;
if (file->flags & O_NONBLOCK)
{
if (!ev_press)
return -EAGAIN;
}
else
{
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
}
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
ev_press = 0;
return (sizeof(key_val));
}
完整代码如下
#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 <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>
static struct class *sixth2drv_class;
static struct class_device *sixth2drv_class_dev;
volatile unsigned long *gpfcon;
volatile unsigned long *gpgcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgdat;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,sixth2_drv_read将它清0 */
static volatile int ev_press = 0;
static unsigned char key_val;
//定义原子变量canopen并初始化为1
static DECLARE_MUTEX(button_lock);
struct pin_desc
{
unsigned int pin;
unsigned int key_val;
};
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
struct pin_desc pins_desc[4] =
{
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04}
};
static struct fasync_struct *button_async;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
//printk("irq=%d\n", irq);
pinval = s3c2410_gpio_getpin(pindesc->pin);
if (pinval)
{
/* 松开 */
key_val = 0x80 | pindesc->key_val;
}
else
{
/* 按下 */
key_val = pindesc->key_val;
}
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
kill_fasync(&button_async, SIGIO, POLL_IN);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int sixth2_drv_open(struct inode *inode, struct file *file)
{
if (file->flags & O_NONBLOCK)
{
if (down_trylock(&button_lock))
return -EBUSY;
}
else
{
/* 获得信号量 */
down(&button_lock);
/* 配置GPF0,2为输出引脚 */
/* 配置GPG3,11为输出引脚 */
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
}
return 0;
}
static int sixth2_drv_read(struct file *filp, char __user *buf,
size_t size, loff_t *ppos)
{
/* 返回4个引脚电平状态 */
if (size != 1)
return -EINVAL;
if (file->flags & O_NONBLOCK)
{
if (!ev_press)
return -EAGAIN;
}
else
{
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
}
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
ev_press = 0;
return (sizeof(key_val));
}
int sixth2_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT11, &pins_desc[2]);
free_irq(IRQ_EINT19, &pins_desc[3]);
/* 释放信号量 */
up(&button_lock);
return 0;
}
static unsigned int sixth2_drv_poll(struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait); // 不会立即休眠
if (ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int sixth2_drv_fasync(int fd, struct file *filp, int on)
{
printk("driver: app use it\n");
return fasync_helper(fd, filp, on, &button_async);
}
static struct file_operations sixth2_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = sixth2_drv_open,
.read = sixth2_drv_read,
.release = sixth2_drv_close,
.poll = sixth2_drv_poll,
.fasync = sixth2_drv_fasync,
};
static int major;
static int sixth2_drv_init(void)
{
major = register_chrdev(0, "my_sixth2_drv", &sixth2_drv_fops);
sixth2drv_class = class_create(THIS_MODULE, "mysixth2drv");
sixth2drv_class_dev = class_device_create(sixth2drv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/xyz */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
static void sixth2_drv_exit(void)
{
unregister_chrdev(major, "my_sixth2_drv");
class_device_unregister(sixth2drv_class_dev);
class_destroy(sixth2drv_class);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
module_init(sixth2_drv_init);
module_exit(sixth2_drv_exit);
MODULE_LICENSE("GPL");
四 test
测试程序可以参考《linux字符设备驱动之异步通知(四)》来设计,这里就不展开来介绍了