: "+m" (v->counter)
: "ir" (i));
}
比如:
atomic_add(2, &v); //原子地给v加2
atomic_sub(3, &v); //原子地给v减3
### 2.5 atomic\_sub\_and\_test
/**
* atomic_sub_and_test - subtract value from variable and test result
* @i: integer value to subtract
* @v: pointer of type atomic_t
*
* Atomically subtracts @i from @v and returns
* true if the result is zero, or false for all
* other cases.
*/
static inline int atomic_sub_and_test(int i, atomic_t *v)
{
unsigned char c;
asm volatile(LOCK_PREFIX "subl %2,%0; sete %1"
: "+m" (v->counter), "=qm" (c)
: "ir" (i) : "memory");
return c;
}
比如:
将原子变量减2,如果等于0,返回真;否则返回假。
// include/linux/stddef.h
enum {
false = 0,
true = 1
};
//等价于:
if(atomic_sub_and_test(2, &v))
return true; //–>v == 2
else
return false; // -->v != 2
### 2.6 atomic increment and decrement
/**
* atomic_inc - increment atomic variable
* @v: pointer of type atomic_t
*
* Atomically increments @v by 1.
*/
static inline void atomic_inc(atomic_t *v)
{
asm volatile(LOCK_PREFIX “incl %0”
: “+m” (v->counter));
}
/**
* atomic_dec - decrement atomic variable
* @v: pointer of type atomic_t
*
* Atomically decrements @v by 1.
*/
static inline void atomic_dec(atomic_t *v)
{
asm volatile(LOCK_PREFIX “decl %0”
: “+m” (v->counter));
}
比如:
atomic_inc(&v) //原子地给v加1
atomic_dec(&v) //原子地给v减1
### 2.7 atomic inc or dec and test
/**
* atomic_dec_and_test - decrement and test
* @v: pointer of type atomic_t
*
* Atomically decrements @v by 1 and
* returns true if the result is 0, or false for all other
* cases.
*/
static inline int atomic_dec_and_test(atomic_t *v)
{
unsigned char c;
asm volatile(LOCK_PREFIX "decl %0; sete %1"
: "+m" (v->counter), "=qm" (c)
: : "memory");
return c != 0;
}
/**
* atomic_inc_and_test - increment and test
* @v: pointer of type atomic_t
*
* Atomically increments @v by 1
* and returns true if the result is zero, or false for all
* other cases.
*/
static inline int atomic_inc_and_test(atomic_t *v)
{
unsigned char c;
asm volatile(LOCK_PREFIX "incl %0; sete %1"
: "+m" (v->counter), "=qm" (c)
: : "memory");
return c != 0;
}
//等价于:
if(atomic_dec_and_test(&v))
return true; //–>v == 1
else
return false; // -->v != 1
if( atomic_inc_and_test(&v))
return true; //–>v == -1
else
return false; // -->v != -1
### 2.8 atomic add or sub and return
/**
* atomic_add_return - add integer and return
* @i: integer value to add
* @v: pointer of type atomic_t
*
* Atomically adds @i to @v and returns @i + @v
*/
static inline int atomic_add_return(int i, atomic_t *v)
{
return i + xadd(&v->counter, i);
}
/**
* atomic_sub_return - subtract integer and return
* @v: pointer of type atomic_t
* @i: integer value to subtract
*
* Atomically subtracts @i from @v and returns @v - @i
*/
static inline int atomic_sub_return(int i, atomic_t *v)
{
return atomic_add_return(-i, v);
}
#define atomic_inc_return(v) (atomic_add_return(1, v))
#define atomic_dec_return(v) (atomic_sub_return(1, v))
## 三、api演示
include <linux/init.h>
include <linux/module.h>
include <linux/kernel.h>
include <asm/atomic.h>
static int __init lkm_init(void)
{
atomic_t v = ATOMIC_INIT(2);
printk("v = %d\n", atomic\_read(&v));
if(atomic\_sub\_and\_test(2, &v))
printk("true\n");
else
printk("false\n");
printk("v = %d\n", atomic\_read(&v));
atomic\_set(&v, 3);
printk("v = %d\n", atomic\_read(&v));
atomic\_inc(&v); //原子地给v加1
printk("v = %d\n", atomic\_read(&v));
atomic\_dec(&v); //原子地给v减1
printk("v = %d\n", atomic\_read(&v));
atomic\_add(2, &v); //原子地给v加2
printk("v = %d\n", atomic\_read(&v));
atomic\_sub(3, &v); //原子地给v减3
printk("v = %d\n", atomic\_read(&v));
atomic\_set(&v, 1);
printk("v = %d\n", atomic\_read(&v));
if(atomic\_dec\_and\_test(&v))
printk("true\n"); //-->v == 1
else
printk("false\n"); // -->v != 1
printk("v = %d\n", atomic\_read(&v));
atomic\_set(&v, -1);
printk("v = %d\n", atomic\_read(&v));
if( atomic\_inc\_and\_test(&v))
printk("true\n"); //-->v == -1
else
printk("false\n"); // -->v != -1
printk("v = %d\n", atomic\_read(&v));
atomic\_set(&v, 2);
printk("v = %d\n", atomic\_read(&v));
printk("ret = %d\n", atomic\_add\_return(3, &v));
printk("v = %d\n", atomic\_read(&v));
printk("ret = %d\n", atomic\_sub\_return(2, &v));
printk("v = %d\n", atomic\_read(&v));
printk("v = %d\n", atomic\_inc\_return(&v));
printk("v = %d\n", atomic\_dec\_return(&v));
return -1;
}
module_init(lkm_init);
MODULE_LICENSE(“GPL”);
Makefile
obj-m:=atomic.o
all:
make -C /lib/modules/
(
s
h
e
l
l
u
n
a
m
e
−
r
)
/
b
u
i
l
d
M
=
(shell uname -r)/build M=
(shelluname−r)/buildM=(PWD) modules
clean:
make -C /lib/modules/
(
s
h
e
l
l
u
n
a
m
e
−
r
)
/
b
u
i
l
d
M
=
(shell uname -r)/build M=
(shelluname−r)/buildM=(PWD) clean
查看结果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/e15ca0e33515472f934cfa357eb24b15.png)
## 总结
该文章主要介绍原子整数操作一系列API的使用。
## 参考资料
Linux内核源码 3.10.0
Linux内核设计与实现
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**