mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
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:
parent
b59e45e65c
commit
4a5cf8c789
Notes:
sideshowbarker
2024-07-18 21:42:13 +09:00
Author: https://github.com/supercomputer7 Commit: https://github.com/SerenityOS/serenity/commit/4a5cf8c789a Pull-request: https://github.com/SerenityOS/serenity/pull/5529 Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/awesomekling Reviewed-by: https://github.com/tomuta
1 changed files with 12 additions and 0 deletions
12
AK/Atomic.h
12
AK/Atomic.h
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue