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:
https://docs.microsoft.com/en-us/cpp/intrinsics/readbarrier?view=msvc-160
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() \
_STL_DISABLE_DEPRECATED_WARNING _ReadWriteBarrier() \
_STL_RESTORE_DEPRECATED_WARNING

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

(https://github.com/microsoft/STL/blob/master/stl/inc/atomic).

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

Paolo

Join rfc@edk2.groups.io to automatically receive all group messages.