mirror of
https://github.com/SerenityOS/serenity
synced 2024-09-06 08:56:40 +00:00
AK: Fixes for atomic pointers
This commit is contained in:
parent
6af3fff0c2
commit
137e1dc7bd
53
AK/Atomic.h
53
AK/Atomic.h
|
@ -46,6 +46,18 @@ static inline T atomic_exchange(volatile T* var, T desired, MemoryOrder order =
|
|||
return __atomic_exchange_n(var, desired, order);
|
||||
}
|
||||
|
||||
template<typename T, typename V = typename RemoveVolatile<T>::Type>
|
||||
static inline V* atomic_exchange(volatile T** var, V* desired, MemoryOrder order = memory_order_seq_cst) noexcept
|
||||
{
|
||||
return __atomic_exchange_n(var, desired, order);
|
||||
}
|
||||
|
||||
template<typename T, typename V = typename RemoveVolatile<T>::Type>
|
||||
static inline V* atomic_exchange(volatile T** var, std::nullptr_t, MemoryOrder order = memory_order_seq_cst) noexcept
|
||||
{
|
||||
return __atomic_exchange_n(const_cast<V**>(var), nullptr, order);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static inline bool atomic_compare_exchange_strong(volatile T* var, T& expected, T desired, MemoryOrder order = memory_order_seq_cst) noexcept
|
||||
{
|
||||
|
@ -55,6 +67,25 @@ static inline bool atomic_compare_exchange_strong(volatile T* var, T& expected,
|
|||
return __atomic_compare_exchange_n(var, &expected, desired, false, order, order);
|
||||
}
|
||||
|
||||
template<typename T, typename V = typename RemoveVolatile<T>::Type>
|
||||
static inline bool atomic_compare_exchange_strong(volatile T** var, V*& expected, V* desired, MemoryOrder order = memory_order_seq_cst) noexcept
|
||||
{
|
||||
if (order == memory_order_acq_rel || order == memory_order_release)
|
||||
return __atomic_compare_exchange_n(var, &expected, desired, false, memory_order_release, memory_order_acquire);
|
||||
else
|
||||
return __atomic_compare_exchange_n(var, &expected, desired, false, order, order);
|
||||
}
|
||||
|
||||
template<typename T, typename V = typename RemoveVolatile<T>::Type>
|
||||
static inline bool atomic_compare_exchange_strong(volatile T** var, V*& expected, std::nullptr_t, MemoryOrder order = memory_order_seq_cst) noexcept
|
||||
{
|
||||
if (order == memory_order_acq_rel || order == memory_order_release)
|
||||
return __atomic_compare_exchange_n(const_cast<V**>(var), &expected, nullptr, false, memory_order_release, memory_order_acquire);
|
||||
else
|
||||
return __atomic_compare_exchange_n(const_cast<V**>(var), &expected, nullptr, false, order, order);
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
static inline T atomic_fetch_add(volatile T* var, T val, MemoryOrder order = memory_order_seq_cst) noexcept
|
||||
{
|
||||
|
@ -91,12 +122,30 @@ static inline T atomic_load(volatile T* var, MemoryOrder order = memory_order_se
|
|||
return __atomic_load_n(var, order);
|
||||
}
|
||||
|
||||
template<typename T, typename V = typename RemoveVolatile<T>::Type>
|
||||
static inline V* atomic_load(volatile T** var, MemoryOrder order = memory_order_seq_cst) noexcept
|
||||
{
|
||||
return __atomic_load_n(const_cast<V**>(var), order);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static inline void atomic_store(volatile T* var, T desired, MemoryOrder order = memory_order_seq_cst) noexcept
|
||||
{
|
||||
__atomic_store_n(var, desired, order);
|
||||
}
|
||||
|
||||
template<typename T, typename V = typename RemoveVolatile<T>::Type>
|
||||
static inline void atomic_store(volatile T** var, V* desired, MemoryOrder order = memory_order_seq_cst) noexcept
|
||||
{
|
||||
__atomic_store_n(var, desired, order);
|
||||
}
|
||||
|
||||
template<typename T, typename V = typename RemoveVolatile<T>::Type>
|
||||
static inline void atomic_store(volatile T** var, std::nullptr_t, MemoryOrder order = memory_order_seq_cst) noexcept
|
||||
{
|
||||
__atomic_store_n(const_cast<V**>(var), nullptr, order);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
class Atomic {
|
||||
T m_value { 0 };
|
||||
|
@ -111,7 +160,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
volatile T* ptr()
|
||||
volatile T* ptr() noexcept
|
||||
{
|
||||
return &m_value;
|
||||
}
|
||||
|
@ -240,7 +289,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
volatile T** ptr()
|
||||
volatile T** ptr() noexcept
|
||||
{
|
||||
return &m_value;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue