說明
- 下面註釋裏說的自增和自減表示的是在原子變量舊值的基礎上
- 這裏列舉的原子操作是以32位爲例的,如果是64位,那麼把前綴
atomic
替換成atomic64
即可
數據類型
原型 | 說明 |
---|---|
atomic_t | 數據位寬是32位 |
atomic64_t | 數據位寬是64位 |
atomic_long_t | 在64位上等於atomic64_t, 在32位系統上等於atomic_t |
初始化
#define ATOMIC_INIT(i) { (i) }
#define ATOMIC64_INIT(i) { (i) }
示例:
atomic_t cnt = ATOMIC_INIT(0);
atomic64_t v = ATOMIC64_INIT(v0);
賦值操作
原型 | 說明 | 返回值 |
---|---|---|
void atomic_set(atomic_t *v, int i) |
將i 賦值給原子變量v |
無 |
讀操作
原型 | 說明 | 返回值 |
---|---|---|
int atomic_read(const atomic_t *v) |
原子變量v 的數值 |
|
linux還提供了帶條件的讀取方式,即如果條件不滿足,就一直讀取:
#define atomic_cond_read_acquire(v, c) smp_cond_load_acquire(&(v)->counter, (c))
#define atomic_cond_read_relaxed(v, c) smp_cond_load_relaxed(&(v)->counter, (c))
#define atomic64_cond_read_acquire(v, c) smp_cond_load_acquire(&(v)->counter, (c))
#define atomic64_cond_read_relaxed(v, c) smp_cond_load_relaxed(&(v)->counter, (c))
其中smp_cond_load_acquire或者smp_cond_load_relaxed中有一個死循環操作,如果c
表示的condition不成立,那麼會在循環中一直讀取地址的值,知道條件成立。
#define smp_cond_load_acquire(ptr, cond_expr) \
({ \
typeof(ptr) __PTR = (ptr); \
__unqual_scalar_typeof(*ptr) VAL; \
for (;;) { \
VAL = smp_load_acquire(__PTR); \
if (cond_expr) \
break; \
__cmpwait_relaxed(__PTR, VAL); \
} \
(typeof(*ptr))VAL; \
})
加操作
原型 | 說明 | 返回值 |
---|---|---|
void atomic_inc(atomic_t *v) |
自增1 |
無 |
int atomic_inc_return(atomic_t *v) |
自增1 |
返回新值 |
int atomic_fetch_inc(atomic_t *v) |
自增1 |
返回舊值 |
void atomic_add(int i, atomic_t *v) |
自增i |
無 |
int atomic_add_return(int i, atomic_t *v) |
自增i |
返回新值 |
int atomic_fetch_add(int i, atomic_t *v) |
自增i |
返回舊值 |
bool atomic_inc_and_test(atomic_t *v) |
自增1 |
如果新值爲0,返回true ;否則返回false |
bool atomic_add_negative(int i, atomic_t *v) |
自增i |
如果新值爲負數,返回true ;否則返回false |
int atomic_fetch_add_unless(atomic_t *v, int a, int u) |
unless 表示if not 。如果舊值跟u 不等,自增a ,返回舊值;如果舊值等於 u ,只返回舊值,不會進行自增操作 |
返回舊值 |
bool atomic_add_unless(atomic_t *v, int a, int u) |
如果舊值跟u 不等,自增a ,返回true ;如果舊值等於 u ,返回舊值false ,不會進行自增操作 |
表示是否進行了自增 |
bool atomic_inc_not_zero(atomic_t *v) |
如果舊值不等於0,那麼自增1 ,返回true ;如果舊值等於0,返回false |
表示是否進行了自增 |
bool atomic_inc_unless_negative(atomic_t *v) |
如果舊值不是負數,那麼自增1 ,返回true ;如果舊值爲負數,返回false |
表示是否進行了自增 |
減操作
原型 | 說明 | 返回值 |
---|---|---|
void atomic_dec(atomic_t *v) |
自減1 |
無 |
int atomic_dec_return(atomic_t *v) |
自減1 |
返回新值 |
int atomic_fetch_dec(atomic_t *v) |
||
void atomic_sub(int i, atomic_t *v) |
自減i |
無 |
int atomic_sub_return(int i, atomic_t *v) |
自減i |
返回新值 |
int atomic_fetch_sub(int i, atomic_t *v) |
自減i |
返回舊值 |
bool atomic_sub_and_test(int i, atomic_t *v) |
自減i |
如果新值爲0,返回true ;否則返回false |
bool atomic_dec_and_test(atomic_t *v) |
自減1 |
如果新值爲0,返回true ;否則返回false |
bool atomic_dec_unless_positive(atomic_t *v) |
如果舊值不是正數(<=0),那麼自減1 ,返回true ;如果舊值是正數(>0),返回false |
表示是否進行了自減操作 |
交換
原型 | 說明 | 返回值 |
---|---|---|
int atomic_xchg(atomic_t *v, int i) |
將i 賦值給原子變量 |
返回舊值 |
比較交換
原型 | 說明 | 返回值 |
---|---|---|
int atomic_cmpxchg(atomic_t *v, int old, int new) |
如果舊值跟old 相等,將new 賦值給原子變量,返回舊值;如果舊值跟 old 不等,返回new |
返回值爲整形 |
bool atomic_try_cmpxchg(atomic_t *v, int *old, int new) |
如果舊值跟*old 相等,將new 賦值給原子變量,返回true ;如果舊值跟 *old 不等,將new 賦值給給*old ,返回false |
返回的是布爾類型,表示原子變量是否成功賦值 |
此外,在某些場景下需要可能需要同時比較交換兩個指針的值,如果操作成功,返回true。內核提供了下面的宏:
#define cmpxchg_double(ptr, ...) \
({ \
typeof(ptr) __ai_ptr = (ptr); \
instrument_atomic_write(__ai_ptr, 2 * sizeof(*__ai_ptr)); \
arch_cmpxchg_double(__ai_ptr, __VA_ARGS__); \
})
示例:
邏輯操作
原型 | 說明 | 返回值 |
---|---|---|
void atomic_and(int i, atomic_t *v) |
邏輯與 |
無 |
int atomic_fetch_and(int i, atomic_t *v) |
邏輯與 |
返回舊值 |
void atomic_andnot(int i, atomic_t *v) |
跟~i 進行邏輯與 |
無 |
int atomic_fetch_andnot(int i, atomic_t *v) |
跟~i 進行邏輯與 |
返回舊值 |
void atomic_or(int i, atomic_t *v) |
邏輯或 | 無 |
int atomic_fetch_or(int i, atomic_t *v) |
邏輯或 | 返回舊值 |
void atomic_xor(int i, atomic_t *v) |
異或 | 無 |
int atomic_fetch_xor(int i, atomic_t *v) |
異或 | 返回舊值 |