/include/linux/types.h
typedef struct {
int counter;
} atomic_t;
#define ATOMIC_INIT(i) { (i) }
#ifdef CONFIG_64BIT
typedef struct {
s64 counter;
} atomic64_t;
#endif
#define ATOMIC64_INIT(i) { (i) }
/include/linux/atomic/atomic-long.h
#ifdef CONFIG_64BIT
typedef atomic64_t atomic_long_t;
#define ATOMIC_LONG_INIT(i) ATOMIC64_INIT(i)
#define atomic_long_cond_read_acquire atomic64_cond_read_acquire
#define atomic_long_cond_read_relaxed atomic64_cond_read_relaxed
#else
typedef atomic_t atomic_long_t;
#define ATOMIC_LONG_INIT(i) ATOMIC_INIT(i)
#define atomic_long_cond_read_acquire atomic_cond_read_acquire
#define atomic_long_cond_read_relaxed atomic_cond_read_relaxed
#endif
原子操作需要硬件的支持,因此是架构相关的
s64实际上是long long类型。
long long不同于long和int。
int类型无论在32位系统,还是在64位系统,所占的空间都是4个字节。
而long在32位系统中和int一样,都是占4个字节,但在64位系统中占8个字节。
在C99中增加了long long类型。该数据类型无论在32位系统,还是在64位系统,都占8个字节。
在32位系统中如果想表示8个字节的整数,需要使用long long。
(参考 https://win9zz.blog.csdn.net/article/details/69081816)
下文就不列 atomic64_t 和 atomic_long_t 的代码了
【读】
static __always_inline int
atomic_read(const atomic_t *v)
{
instrument_atomic_read(v, sizeof(*v));
return arch_atomic_read(v);
}
static __always_inline int
atomic_read_acquire(const atomic_t *v)
{
instrument_atomic_read(v, sizeof(*v));
return arch_atomic_read_acquire(v);
}
【设置】
static __always_inline void
atomic_set(atomic_t *v, int i)
{
instrument_atomic_write(v, sizeof(*v));
arch_atomic_set(v, i);
}
static __always_inline void
atomic_set_release(atomic_t *v, int i)
{
instrument_atomic_write(v, sizeof(*v));
arch_atomic_set_release(v, i);
}
【加】
static __always_inline void
atomic_add(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
arch_atomic_add(i, v);
}
static __always_inline int
atomic_add_return(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_add_return(i, v);
}
static __always_inline int
atomic_add_return_acquire(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_add_return_acquire(i, v);
}
static __always_inline int
atomic_add_return_release(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_add_return_release(i, v);
}
static __always_inline int
atomic_add_return_relaxed(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_add_return_relaxed(i, v);
}
static __always_inline int
atomic_fetch_add(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_add(i, v);
}
static __always_inline int
atomic_fetch_add_acquire(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_add_acquire(i, v);
}
static __always_inline int
atomic_fetch_add_release(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_add_release(i, v);
}
static __always_inline int
atomic_fetch_add_relaxed(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_add_relaxed(i, v);
}
【减】
static __always_inline void
atomic_sub(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
arch_atomic_sub(i, v);
}
static __always_inline int
atomic_sub_return(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_sub_return(i, v);
}
static __always_inline int
atomic_sub_return_acquire(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_sub_return_acquire(i, v);
}
static __always_inline int
atomic_sub_return_release(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_sub_return_release(i, v);
}
static __always_inline int
atomic_sub_return_relaxed(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_sub_return_relaxed(i, v);
}
static __always_inline int
atomic_fetch_sub(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_sub(i, v);
}
static __always_inline int
atomic_fetch_sub_acquire(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_sub_acquire(i, v);
}
static __always_inline int
atomic_fetch_sub_release(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_sub_release(i, v);
}
static __always_inline int
atomic_fetch_sub_relaxed(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_sub_relaxed(i, v);
}
【自加】
static __always_inline void
atomic_inc(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
arch_atomic_inc(v);
}
【自加,返回加之后的值】
static __always_inline int
atomic_inc_return(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_inc_return(v);
}
static __always_inline int
atomic_inc_return_acquire(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_inc_return_acquire(v);
}
static __always_inline int
atomic_inc_return_release(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_inc_return_release(v);
}
static __always_inline int
atomic_inc_return_relaxed(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_inc_return_relaxed(v);
}
【自加,返回加之前的值】
static __always_inline int
atomic_fetch_inc(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_inc(v);
}
【自加,返回加之前的值】
static __always_inline int
atomic_fetch_inc_acquire(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_inc_acquire(v);
}
static __always_inline int
atomic_fetch_inc_release(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_inc_release(v);
}
static __always_inline int
atomic_fetch_inc_relaxed(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_inc_relaxed(v);
}
【自减】
static __always_inline void
atomic_dec(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
arch_atomic_dec(v);
}
static __always_inline int
atomic_dec_return(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_dec_return(v);
}
static __always_inline int
atomic_dec_return_acquire(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_dec_return_acquire(v);
}
static __always_inline int
atomic_dec_return_release(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_dec_return_release(v);
}
static __always_inline int
atomic_dec_return_relaxed(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_dec_return_relaxed(v);
}
【自减,返回减之前的值】
static __always_inline int
atomic_fetch_dec(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_dec(v);
}
static __always_inline int
atomic_fetch_dec_acquire(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_dec_acquire(v);
}
static __always_inline int
atomic_fetch_dec_release(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_dec_release(v);
}
static __always_inline int
atomic_fetch_dec_relaxed(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_dec_relaxed(v);
}
【与】
static __always_inline void
atomic_and(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
arch_atomic_and(i, v);
}
static __always_inline int
atomic_fetch_and(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_and(i, v);
}
static __always_inline int
atomic_fetch_and_acquire(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_and_acquire(i, v);
}
static __always_inline int
atomic_fetch_and_release(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_and_release(i, v);
}
static __always_inline int
atomic_fetch_and_relaxed(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_and_relaxed(i, v);
}
【与非】
static __always_inline void
atomic_andnot(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
arch_atomic_andnot(i, v);
}
static __always_inline int
atomic_fetch_andnot(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_andnot(i, v);
}
static __always_inline int
atomic_fetch_andnot_acquire(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_andnot_acquire(i, v);
}
static __always_inline int
atomic_fetch_andnot_release(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_andnot_release(i, v);
}
static __always_inline int
atomic_fetch_andnot_relaxed(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_andnot_relaxed(i, v);
}
【或】
static __always_inline void
atomic_or(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
arch_atomic_or(i, v);
}
static __always_inline int
atomic_fetch_or(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_or(i, v);
}
static __always_inline int
atomic_fetch_or_acquire(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_or_acquire(i, v);
}
static __always_inline int
atomic_fetch_or_release(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_or_release(i, v);
}
static __always_inline int
atomic_fetch_or_relaxed(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_or_relaxed(i, v);
}
【异或】
static __always_inline void
atomic_xor(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
arch_atomic_xor(i, v);
}
static __always_inline int
atomic_fetch_xor(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_xor(i, v);
}
static __always_inline int
atomic_fetch_xor_acquire(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_xor_acquire(i, v);
}
static __always_inline int
atomic_fetch_xor_release(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_xor_release(i, v);
}
static __always_inline int
atomic_fetch_xor_relaxed(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_xor_relaxed(i, v);
}
【交换】
static __always_inline int
atomic_xchg(atomic_t *v, int i)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_xchg(v, i);
}
static __always_inline int
atomic_xchg_acquire(atomic_t *v, int i)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_xchg_acquire(v, i);
}
static __always_inline int
atomic_xchg_release(atomic_t *v, int i)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_xchg_release(v, i);
}
static __always_inline int
atomic_xchg_relaxed(atomic_t *v, int i)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_xchg_relaxed(v, i);
}
static __always_inline int
atomic_cmpxchg(atomic_t *v, int old, int new)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_cmpxchg(v, old, new);
}
static __always_inline int
atomic_cmpxchg_acquire(atomic_t *v, int old, int new)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_cmpxchg_acquire(v, old, new);
}
static __always_inline int
atomic_cmpxchg_release(atomic_t *v, int old, int new)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_cmpxchg_release(v, old, new);
}
static __always_inline int
atomic_cmpxchg_relaxed(atomic_t *v, int old, int new)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_cmpxchg_relaxed(v, old, new);
}
【试图交换】
static __always_inline bool
atomic_try_cmpxchg(atomic_t *v, int *old, int new)
{
instrument_atomic_read_write(v, sizeof(*v));
instrument_atomic_read_write(old, sizeof(*old));
return arch_atomic_try_cmpxchg(v, old, new);
}
static __always_inline bool
atomic_try_cmpxchg_acquire(atomic_t *v, int *old, int new)
{
instrument_atomic_read_write(v, sizeof(*v));
instrument_atomic_read_write(old, sizeof(*old));
return arch_atomic_try_cmpxchg_acquire(v, old, new);
}
static __always_inline bool
atomic_try_cmpxchg_release(atomic_t *v, int *old, int new)
{
instrument_atomic_read_write(v, sizeof(*v));
instrument_atomic_read_write(old, sizeof(*old));
return arch_atomic_try_cmpxchg_release(v, old, new);
}
static __always_inline bool
atomic_try_cmpxchg_relaxed(atomic_t *v, int *old, int new)
{
instrument_atomic_read_write(v, sizeof(*v));
instrument_atomic_read_write(old, sizeof(*old));
return arch_atomic_try_cmpxchg_relaxed(v, old, new);
}
【减-减完i后判断结果是否为0,为0则返回真】
static __always_inline bool
atomic_sub_and_test(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_sub_and_test(i, v);
}
【减-自减1完判断结果是否为0,为0则返回真】
static __always_inline bool
atomic_dec_and_test(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_dec_and_test(v);
}
【加-自加1完判断结果是否为0,为0则返回真】
static __always_inline bool
atomic_inc_and_test(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_inc_and_test(v);
}
【加-加完i后判断结果是否为负,为负则返回真】
static __always_inline bool
atomic_add_negative(int i, atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_add_negative(i, v);
}
static __always_inline int
atomic_fetch_add_unless(atomic_t *v, int a, int u)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_fetch_add_unless(v, a, u);
}
【加,除非*v==u,*v等于v的话就不加a】
static __always_inline bool
atomic_add_unless(atomic_t *v, int a, int u)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_add_unless(v, a, u);
}
【加,如果*v!=0,*v==0的话就不自加1】
static __always_inline bool
atomic_inc_not_zero(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_inc_not_zero(v);
}
【自加,除非*v为负,*v为负就不加】
static __always_inline bool
atomic_inc_unless_negative(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_inc_unless_negative(v);
}
【自减,除非*v为正,*v为正就不自减】
static __always_inline bool
atomic_dec_unless_positive(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_dec_unless_positive(v);
}
【正就减,否则不减,返回值确是减去之后的值】
static __always_inline int
atomic_dec_if_positive(atomic_t *v)
{
instrument_atomic_read_write(v, sizeof(*v));
return arch_atomic_dec_if_positive(v);
}
//===================================================
【基本示例】
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/printk.h>
#include <linux/atomic.h>
static int __init my_demo_init(void)
{
int ret = 0;
bool ret1 = false;
atomic_t test = ATOMIC_INIT(0);
printk("%s, ===> begin\n", __func__);
printk("%s, atomic read test %d\n", __func__, atomic_read(&test));
printk("%s, ===> set\n", __func__);
atomic_set(&test, 1);
printk("%s, atomic read test %d\n", __func__, atomic_read(&test));
printk("%s, ===> add\n", __func__);
atomic_add(1, &test);
printk("%s, atomic read test %d\n", __func__, atomic_read(&test));
ret = atomic_add_return(1, &test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret);
ret = atomic_fetch_add(1, &test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret);
printk("%s, ===> sub\n", __func__);
atomic_sub(1, &test);
printk("%s, atomic read test %d\n", __func__, atomic_read(&test));
ret = atomic_sub_return(1, &test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret);
ret = atomic_fetch_sub(1, &test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret);
printk("%s, ===> inc\n", __func__);
atomic_inc(&test);
printk("%s, atomic read test %d\n", __func__, atomic_read(&test));
ret = atomic_inc_return(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret);
ret = atomic_fetch_inc(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret);
printk("%s, ===> dec\n", __func__);
atomic_dec(&test);
printk("%s, atomic read test %d\n", __func__, atomic_read(&test));
ret = atomic_dec_return(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret);
ret = atomic_fetch_dec(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret);
printk("%s, ===> sub_and_test\n", __func__);
atomic_set(&test, 1);
ret1 = atomic_sub_and_test(1, &test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // 0 1
atomic_set(&test, 1);
ret1 = atomic_sub_and_test(2, &test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // -1 0
printk("%s, ===> dec_and_test\n", __func__);
atomic_set(&test, 1);
ret1 = atomic_dec_and_test(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // 0 1
atomic_set(&test, 0);
ret1 = atomic_dec_and_test(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // -1 0
printk("%s, ===> inc_and_test\n", __func__);
atomic_set(&test, -1);
ret1 = atomic_inc_and_test(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // 0 1
atomic_set(&test, 0);
ret1 = atomic_inc_and_test(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // 1 0
printk("%s, ===> add_negative\n", __func__);
atomic_set(&test, -2);
ret1 = atomic_add_negative(1, &test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // -1 1
atomic_set(&test, 0);
ret1 = atomic_add_negative(1, &test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // 1 0
printk("%s, ===> add_unless\n", __func__);
atomic_set(&test, -2);
ret1 = atomic_add_unless(&test, 3, -2);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // -2 0
atomic_set(&test, 0);
ret1 = atomic_add_unless(&test, 3, -4);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); //3 1
printk("%s, ===> inc_not_zero\n", __func__);
atomic_set(&test, -2);
ret1 = atomic_inc_not_zero(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // -1 1
atomic_set(&test, 0);
ret1 = atomic_inc_not_zero(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // 0 0
printk("%s, ===> inc_unless_negative\n", __func__);
atomic_set(&test, -2);
ret1 = atomic_inc_unless_negative(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // -2 0
atomic_set(&test, 0);
ret1 = atomic_inc_unless_negative(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // 1 1
atomic_set(&test, 2);
ret1 = atomic_inc_unless_negative(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // 3 1
printk("%s, ===> dec_unless_positive\n", __func__);
atomic_set(&test, -2);
ret1 = atomic_dec_unless_positive(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // -3 1
atomic_set(&test, 0);
ret1 = atomic_dec_unless_positive(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // -1 1
atomic_set(&test, 2);
ret1 = atomic_dec_unless_positive(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret1); // 2 0
printk("%s, ===> dec_if_positive\n", __func__);
atomic_set(&test, -2);
ret = atomic_dec_if_positive(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret); // -2 -3
atomic_set(&test, 0);
ret = atomic_dec_if_positive(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret); // 0 -1
atomic_set(&test, 2);
ret = atomic_dec_if_positive(&test);
printk("%s, atomic read test %d, ret %d\n", __func__, atomic_read(&test), ret); // 1 1
printk("%s, <=== end\n", __func__);
return 0;
}
static void my_demo_exit(void)
{
return;
}
module_init(my_demo_init);
module_exit(my_demo_exit);
MODULE_LICENSE ("GPL");
MODULE_AUTHOR ("kiss1994");
MODULE_DESCRIPTION ("atomic");
[ 4234.373532] my_demo_init, ===> begin
[ 4234.373539] my_demo_init, atomic read test 0
[ 4234.373540] my_demo_init, ===> set
[ 4234.373542] my_demo_init, atomic read test 1
[ 4234.373543] my_demo_init, ===> add
[ 4234.373544] my_demo_init, atomic read test 2
[ 4234.373546] my_demo_init, atomic read test 3, ret 3
[ 4234.373548] my_demo_init, atomic read test 4, ret 3
[ 4234.373549] my_demo_init, ===> sub
[ 4234.373550] my_demo_init, atomic read test 3
[ 4234.373551] my_demo_init, atomic read test 2, ret 2
[ 4234.373553] my_demo_init, atomic read test 1, ret 2
[ 4234.373554] my_demo_init, ===> inc
[ 4234.373555] my_demo_init, atomic read test 2
[ 4234.373557] my_demo_init, atomic read test 3, ret 3
[ 4234.373558] my_demo_init, atomic read test 4, ret 3
[ 4234.373559] my_demo_init, ===> dec
[ 4234.373561] my_demo_init, atomic read test 3
[ 4234.373562] my_demo_init, atomic read test 2, ret 2
[ 4234.373563] my_demo_init, atomic read test 1, ret 2
[ 4234.373565] my_demo_init, ===> sub_and_test
[ 4234.373566] my_demo_init, atomic read test 0, ret 1
[ 4234.373568] my_demo_init, atomic read test -1, ret 0
[ 4234.373569] my_demo_init, ===> dec_and_test
[ 4234.373571] my_demo_init, atomic read test 0, ret 1
[ 4234.373572] my_demo_init, atomic read test -1, ret 0
[ 4234.373573] my_demo_init, ===> inc_and_test
[ 4234.373575] my_demo_init, atomic read test 0, ret 1
[ 4234.373576] my_demo_init, atomic read test 1, ret 0
[ 4234.373577] my_demo_init, ===> add_negative
[ 4234.373579] my_demo_init, atomic read test -1, ret 1
[ 4234.373580] my_demo_init, atomic read test 1, ret 0
[ 4234.373581] my_demo_init, ===> add_unless
[ 4234.373583] my_demo_init, atomic read test -2, ret 0
[ 4234.373585] my_demo_init, atomic read test 3, ret 1
[ 4234.373586] my_demo_init, ===> inc_not_zero
[ 4234.373587] my_demo_init, atomic read test -1, ret 1
[ 4234.373589] my_demo_init, atomic read test 0, ret 0
[ 4234.373590] my_demo_init, ===> inc_unless_negative
[ 4234.373592] my_demo_init, atomic read test -2, ret 0
[ 4234.373593] my_demo_init, atomic read test 1, ret 1
[ 4234.373595] my_demo_init, atomic read test 3, ret 1
[ 4234.373596] my_demo_init, ===> dec_unless_positive
[ 4234.373597] my_demo_init, atomic read test -3, ret 1
[ 4234.373599] my_demo_init, atomic read test -1, ret 1
[ 4234.373600] my_demo_init, atomic read test 2, ret 0
[ 4234.373602] my_demo_init, ===> dec_if_positive
[ 4234.373603] my_demo_init, atomic read test -2, ret -3
[ 4234.373605] my_demo_init, atomic read test 0, ret -1
[ 4234.373606] my_demo_init, atomic read test 1, ret 1
[ 4234.373607] my_demo_init, <=== end
想当然的东西实践后就想不当然了