Re: MemoryFence()

Paolo Bonzini <pbonzini@...>

On 08/02/21 18:40, Laszlo Ersek wrote:
(3) The article recommends _ReadWriteBarrier, _ReadBarrier and
_WriteBarrier, for compiler fences. I think _ReadWriteBarrier should
suffice for edk2's purposes.
However, the following reference deprecates those intrinsics:
while offering*only* C++ language replacements.
In theory, atomic_thread_fence (and in this case, more specifically, atomic_signal_fence) is available also on C11. But if it is possible to assume that _RWB is present on Microsoft toolchains (even though it's deprecated) I would go for it as it seems to be the best match for CompilerFence.

In fact, even Microsoft's own STL does

#define _Compiler_barrier() \

extern "C" inline void
atomic_signal_fence(const memory_order _Order) noexcept
if (_Order != memory_order_relaxed) {


For fences that require an assembly instruction, it is possible to use intrinsics too: _mm_mfence() for x86, __dmb(_ARM_BARRIER_SY) for ARM.


Join to automatically receive all group messages.