Does atomic_cmpxchg() imply memory barriers? Does atomic_cmpxchg() imply memory barriers? multithreading multithreading

Does atomic_cmpxchg() imply memory barriers?


I'm not aware yet about Linux kernel programming specifics, so here is a partial (general) answer.

On x86, this operation carries full memory fence with it, there is no need in mfence/lfence/sfence around cmpxchg op.

On other architectures with relaxed memory model, it can be coupled with other memory semantics, e.g. "release", depending on how atomic_cmpxchg() is translated to the op codes.

It's on the processor side of things. However, there is compiler which can also reorder the operations, so if compiler barrier is not implied by atomic_cmpxchg() (by e.g. __asm__ __volatile__), you would need one.