atomic_add_unless函数的实现在arch/arm/include/asm/atomic.h中
作用是将新值存入atomic_t类型的变量,并把变量的旧值返回,函数事先如下。
static inline int atomic_add_unless(atomic_t *v, int a, int u)
{
int c, old;
c = atomic_read(v);
while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c)
c = old;
return c != u;
}
函数会事先判断第二个参数是否与传入atomic_t中的值是否相同,如果不同的话需要把a和传入的atiomi_t的值相加。
由于cmpxchg函数可能并不会交换成功,所以需要反复检查是否存入并执行cmpxcg函数。函数返回想加前的值是否等于u。
static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
{
int ret;
unsigned long flags;
raw_local_irq_save(flags);
ret = v->counter;
if (likely(ret == old))
v->counter = new;
raw_local_irq_restore(flags);
return ret;
}
atomic_cmpxchg需要传入三个参数,当old与v相同的时候才把新的取值传入,并返回之前的取值。