https://www.kernel.org/doc/Documentation/atomic_ops.txt
int atomic_cmpxchg(atomic_t *v, int old, int new);
这个函数对原子变量v执行一个比较并交换的操作,使用给定的旧值和新值。与所有的atomic_xxx函数一样,只有在所有对*v的访问都通过atomic_xxx函数进行时,atomic_cmpxchg才能满足其原子性语义。
atomic_cmpxchg需要显式地在操作周围设置内存屏障。
vs
https://www.kernel.org/doc/Documentation/memory-barriers.txt
Any atomic operation that modifies some state in memory and returns information about the state (old or new) implies an SMP-conditional general memory barrier (smp_mb()) on each side of the actual operation (with the exception of explicit lock operations, described later). These include:
<...> atomic_xchg(); atomic_cmpxchg(); <...>
These are used for such things as implementing LOCK-class and UNLOCK-class operations and adjusting reference counters towards object destruction, and as such the implicit memory barrier effects are necessary.
那么,是否应该手动在 atomic_xchg()
周围放置内存屏障?