作用
多条代码通过内联汇编成一个原子变量,视为一个不可被分割的整体(使用汇编语言实现,因为C语言不能实现)。原子操作需要硬件的支持,和架构有关,以ARMv6为分界点,实现方式有所不同,在ARMv6(不支持SMP)以下的版本中,1 关中断 2 操作变量 3 恢复中断;在ARMv6及以上版本中,使用strex和sdrex多次尝试,原子操作主要用于实现资源计数,很多引用计数(refcnt)就是通过原子操作实现的。
静态初始化
atomic_t lock = ATOMIC_INIT(1) //初始值为1
atomic_t lock = ATOMIC_INIT(-1) //初始值为-1
获取原子操作
atomic_dec_and_test(&lock) //减1和0比较
atomic_inc_and_test(&lock) //加1和0比较
返回值:成功 = 1,失败 = !0
释放原子操作
atomic_inc(&lock) //自增1
atomic_dec(&lock) //自减1
示例
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#define CNAME "mycdev"
struct cdev cdev;
int major = 0;
int minor = 0;
const int count = 3;
struct class *cls;
struct device *dev;
char kbuf[128] = {0};
//一.定义并初始化原子操作
//atomic_t lock = ATOMIC_INIT(-1);
atomic_t lock = ATOMIC_INIT(1);
int mycdev_open(struct inode *inode, struct file *file){
//二.获取原子操作锁(减1和0比较)
//atomic_inc_and_test(&lock) (加1和0比较)
if(!atomic_dec_and_test(&lock)){
//参考互斥锁,这里检测自身的值
atomic_inc(&lock);
return -EBUSY;
}
printk("%s:%s(%d)\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *offs){
int ret = 0;
printk("%s:%s(%d)\n",__FILE__,__func__,__LINE__);
//校验大小
if(size > sizeof(kbuf)){
size = sizeof(kbuf);
}
//数据传输
ret = copy_to_user(ubuf, kbuf, size);
if(ret){
printk("copy_to_user error\n");
return -EIO;
}
return size;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *offs){
int ret;
printk("%s:%s(%d)\n",__FILE__,__func__,__LINE__);
//校验大小
if(size > sizeof(kbuf)) size = sizeof(kbuf);
//数据传输
ret = copy_from_user(kbuf,ubuf,size);
if(ret){
printk("copy_from_user error\n");
return -EIO;
}
return size;
}
int mycdev_close(struct inode*inode, struct file*file){
printk("%s:%s(%d)\n",__FILE__,__func__,__LINE__);
//三.释放原子操作锁
atomic_inc(&lock);
return 0;
}
const struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
};
static int __init mycdev_init(void){
int ret = 0;
dev_t devno = 0;
int i = 0;
//1.初始化cdev
cdev_init(&cdev,&fops);
cdev.owner = THIS_MODULE;
//2.申请设备号
if(major > 0){
ret = register_chrdev_region(MKDEV(major,minor),count,CNAME);
if(ret){
printk("register_chrdev_region error\n");
goto ERR1;
}
}else if(major == 0){
ret = alloc_chrdev_region(&devno,minor,count,CNAME);
if(ret){
printk("alloc_chrdev_region error\n");
goto ERR1;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
//3.注册
ret = cdev_add(&cdev,MKDEV(major,minor),count);
if(ret){
printk("cdev_add error\n");
goto ERR2;
}
//4.创建设备节点
cls = class_create(THIS_MODULE,CNAME);
if(IS_ERR(cls)){
printk("class_creat error\n");
goto ERR3;
}
for(i = 0; i < count; i++){
dev = device_create(cls, NULL, MKDEV(major,i), NULL, "mycdev%d",i);
if(IS_ERR(dev)){
printk("device_creat error\n");
goto ERR4;
}
}
return 0;
ERR4:
for(--i; i >= 0; i--){
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
ERR3:
cdev_del(&cdev);
ERR2:
unregister_chrdev_region(MKDEV(major,minor),count);
ERR1:
return ret;
}
static void __exit mycdev_exit(void){
int i = 0;
for(i = 0; i < count; i++){
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
cdev_del(&cdev);
unregister_chrdev_region(MKDEV(major, minor), count);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");