关闭

驱动中的同步互斥阻塞之原子量

标签: armc语言编程测试芯片
57人阅读 评论(0) 收藏 举报
分类:

目的:只能有一个app打开驱动程序


1.原子操作

原子操作指的是在执行过程中不会被别的代码路径所中断的操作

常用原子操作函数举例

atomic_v  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


范例代码如下

#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<linux/cdev.h>
#include <linux/poll.h>
int key_id;
struct cdev cdev;
dev_t devno;


static atomic_t canopen=ATOMIC_INIT(1); //定义原子变量,并初始化为1
static DECLARE_WAIT_QUEUE_HEAD(key_qaq);
/*等待队列头*/
/*中断事件标志,中断服务程序将它置为1,read函数将它置为0*/
unsigned int wait_val=0;
struct class *my_class;




static irqreturn_t key_func(int irq,void *dev_id)
{
key_id=(int)dev_id;


/*唤醒*/
wait_val=1;
wake_up_interruptible(&key_qaq);



return 0;
}


static int key_open(struct inode *inode,struct file *file)
{




if(!atomic_dec_and_test(&canopen))
{
atomic_inc(&canopen);
return -EBUSY;
}


request_irq(IRQ_EINT(16),key_func,IRQF_TRIGGER_FALLING,"k1",1);
request_irq(IRQ_EINT(17),key_func,IRQF_TRIGGER_FALLING,"k2",2);
request_irq(IRQ_EINT(18),key_func,IRQF_TRIGGER_FALLING,"k3",3);
request_irq(IRQ_EINT(19),key_func,IRQF_TRIGGER_FALLING,"k4",4);
request_irq(IRQ_EINT(24),key_func,IRQF_TRIGGER_FALLING,"k5",5);
request_irq(IRQ_EINT(25),key_func,IRQF_TRIGGER_FALLING,"k6",6);
request_irq(IRQ_EINT(26),key_func,IRQF_TRIGGER_FALLING,"k7",7);
request_irq(IRQ_EINT(27),key_func,IRQF_TRIGGER_FALLING,"k8",8);


return 0;
}


static int key_close(struct inode *inode,struct file *file)
{


atomic_inc(&canopen);
free_irq(IRQ_EINT(16),1);
free_irq(IRQ_EINT(17),2);
free_irq(IRQ_EINT(18),3);
free_irq(IRQ_EINT(19),4);
free_irq(IRQ_EINT(24),5);
free_irq(IRQ_EINT(25),6);
free_irq(IRQ_EINT(26),7);
free_irq(IRQ_EINT(27),8);


return 0;
}


ssize_t key_read(struct file *filp, char __user *buf, size_t size,loff_t *pos)
{
/*如果没有按键按下,休眠,让出CPU*/
wait_event_interruptible(key_qaq,wait_val);
/*如果有按键按下*/
copy_to_user(buf,&key_id,sizeof(key_id)); 
wait_val=0;


return 0;
}




static unsigned key_poll(struct file *file, poll_table *wait)
{
unsigned int mask=0;
poll_wait(file,&key_qaq,wait);/*不会立即休眠,只是挂在那个队列上面去*/
if(wait_val)
mask |= POLLIN | POLLRDNORM;


return mask;
}


static struct file_operations key_fops =
{
.owner = THIS_MODULE,
.open = key_open,
.release = key_close,
.read = key_read,
.poll = key_poll,
};




static int __init key_atomic_init(void)
{
cdev_init(&cdev,&key_fops);
alloc_chrdev_region(&devno, 0 , 1 , "mykey_atomic");
cdev_add(&cdev, devno, 1);
my_class = class_create(THIS_MODULE, "key_atomic_class");
if(IS_ERR(my_class))
{
printk("Err: failed in creating class.\n");
return -1;
}
device_create(my_class, NULL, devno,NULL,"key_atomic_driver");
return 0;
}


static void key_atomic_exit(void)
{
device_destroy(my_class,devno);
class_destroy(my_class);
cdev_del(&cdev);
unregister_chrdev_region(devno,1);
}


module_init(key_atomic_init);
module_exit(key_atomic_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("EIGHT_FIVE");






测试程序如下

#include<stdio.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>


int main(int argv,char **argc)
{
int fd,buf;
fd=open("/dev/key_atomic_driver",0x666);
if(fd<0)
{
printf("can't open\n");
return -1;
}
while(1)
{


read(fd,&buf,4);
printf("key_val =%d\n",buf);
}


close(fd);
return 0;


}





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5709次
    • 积分:753
    • 等级:
    • 排名:千里之外
    • 原创:72篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    最新评论