atomic_inc(&v)对变量v用锁定总线的单指令进行不可分解的"原子"级增量操作,避免v的值由于中断或多处理器同时操作造成不确定状态。
X86 用LOCK 指令实现, 不懂
ARM代码如下, ldrex :
ARMv6_Architecture.pdf 解释如下 还是不大明白
This performs a load, then sets a monitor to “watch” the address
• STREX {<cond>} <Rd>, <Rm>, [<Rn>]
This performs a store and returns “success” in Rd if no intervening access
detected by the monitor.
This performs a load, then sets a monitor to “watch” the address
• STREX {<cond>} <Rd>, <Rm>, [<Rn>]
This performs a store and returns “success” in Rd if no intervening access
detected by the monitor.
This performs a load, then sets a monitor to “watch” the address
• STREX {<cond>} <Rd>, <Rm>, [<Rn>]
This performs a store and returns “success” in Rd if no intervening access
detected by the monitor.
static inline int atomic_add_return(int i, atomic_t *v)
{
unsigned long tmp;
int result;
__asm__ __volatile__("@ atomic_add_return/n"
"1: ldrex %0, [%2]/n"
" add %0, %0, %3/n"
" strex %1, %0, [%2]/n"
" teq %1, #0/n"
" bne 1b"
: "=&r" (result), "=&r" (tmp)
: "r" (&v->counter), "Ir" (i)
: "cc");