atomic_add 在 ARM 原子中? [英] atomic_add in ARM atomic?

查看:32
本文介绍了atomic_add 在 ARM 原子中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 http://lxr.free-electrons.com/source/arch/arm/include/asm/atomic.h#L31

 static inline void atomic_add(int i, atomic_t *v)
 41 {
 42         unsigned long tmp;
 43         int result;
 44 
 45         prefetchw(&v->counter);
 46         __asm__ __volatile__("@ atomic_add\n"
 47 "1:     ldrex   %0, [%3]\n"
 48 "       add     %0, %0, %4\n"
 49 "       strex   %1, %0, [%3]\n"
 50 "       teq     %1, #0\n"
 51 "       bne     1b"
 52         : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
 53         : "r" (&v->counter), "Ir" (i)
 54         : "cc");
 55 }

可以抢占的情况下怎么叫原子化

how can it be called as atomic when it can be preempted

推荐答案

您似乎完全误解了原子操作是什么.

You seem to be totally misunderstanding what an atomic operation is.

应该很明显,如果你看一个值x,看到值是13,然后调用一个atomic_add函数将它增加5,新的结果可以是任何东西,因为另一个线程可能在atomic_add之前改变了这个值被称为.同样,如果您检查结果,它也可能是任何结果,因为另一个线程可能会更改 atomic_add 和您的检查之间的结果.

It should be obviously that if you look at a value x, see the value is 13, then call an atomic_add function that increases it by 5, the new result could be anything, because another thread could have changed the value before atomic_add was called. Likewise, if you check the result, it could again be anything because another thread could change the result between the atomic_add and your checking.

atomic_add 函数保证将值增加该数量.这就是它的作用.这是如何实现的并不重要.如果一百个线程调用 atomic_add (5, &x),那么 x 最终会增加 500.这才是最重要的.

An atomic_add function guarantees to leave the value increased by that amount. And that's what it does. How this is achieved doesn't matter. If hundred threads call atomic_add (5, &x), then x will end up increased by 500. That's what matters.

这是在 ARM 和 PowerPC 等处理器上执行原子操作的典型方法,这些处理器具有保留内存位置的指令和检查保留仍然存在的存储指令.

That's the typical method how atomic operations are performed on processors like ARM and PowerPC that have an instruction that reserves a memory location and a store instruction checking that a reservation still exists.

这篇关于atomic_add 在 ARM 原子中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆