atomic_inc

 

atomic_inc(&v)对变量v用锁定总线的单指令进行不可分解的"原子"级增量操作,避免v的值由于中断或多处理器同时操作造成不确定状态。

X86 用LOCK 指令实现,  不懂

ARM代码如下,  ldrex :

 ARMv6_Architecture.pdf    解释如下   还是不大明白

• LDREX{<cond>} <Rd>, [<Rn>]
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.

• LDREX{<cond>} <Rd>, [<Rn>]
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.

• LDREX{<cond>} <Rd>, [<Rn>]
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");

        return result;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值