AK: Add a full memory barrier function based on atomic operations

We use atomic_signal_fence and atomic_thread_fence together to prevent
reordering of memory accesses by the CPU and the compiler.

The usage of these functions was suggested by @tomuta so we can be sure
that important memory accesses happen in the expected order :)
This commit is contained in:
Liav A 2021-02-27 19:30:20 +02:00 committed by Andreas Kling
parent b59e45e65c
commit 4a5cf8c789
Notes: sideshowbarker 2024-07-18 21:42:13 +09:00

View file

@ -31,11 +31,22 @@
namespace AK {
static inline void atomic_signal_fence(MemoryOrder order) noexcept
{
return __atomic_signal_fence(order);
}
static inline void atomic_thread_fence(MemoryOrder order) noexcept
{
return __atomic_thread_fence(order);
}
static inline void full_memory_barrier() noexcept
{
atomic_signal_fence(AK::MemoryOrder::memory_order_acq_rel);
atomic_thread_fence(AK::MemoryOrder::memory_order_acq_rel);
}
template<typename T>
static inline T atomic_exchange(volatile T* var, T desired, MemoryOrder order = memory_order_seq_cst) noexcept
{
@ -376,3 +387,4 @@ public:
}
using AK::Atomic;
using AK::full_memory_barrier;