AK: Add Atomic.h

Use gcc built-in atomics
This commit is contained in:
Tom 2019-10-12 11:17:34 -06:00 committed by Andreas Kling
parent 2530378f59
commit b0773a8ea6
Notes: sideshowbarker 2024-07-19 11:43:29 +09:00
6 changed files with 606 additions and 53 deletions

238
AK/Atomic.h Normal file
View file

@ -0,0 +1,238 @@
#pragma once
namespace AK {
enum MemoryOrder
{
memory_order_relaxed = __ATOMIC_RELAXED,
memory_order_consume = __ATOMIC_CONSUME,
memory_order_acquire = __ATOMIC_ACQUIRE,
memory_order_release = __ATOMIC_RELEASE,
memory_order_acq_rel = __ATOMIC_ACQ_REL,
memory_order_seq_cst = __ATOMIC_SEQ_CST
};
template <typename T>
class Atomic
{
T m_value { 0 };
public:
Atomic() noexcept = default;
Atomic(const Atomic&) = delete;
Atomic& operator=(const Atomic&) volatile = delete;
Atomic(T val) noexcept:
m_value(val)
{
}
T exchange(T desired, MemoryOrder order = memory_order_seq_cst) volatile noexcept
{
return __atomic_exchange_n(&m_value, desired, order);
}
bool compare_exchange_strong(T& expected, T desired, MemoryOrder order = memory_order_seq_cst) volatile noexcept
{
if (order == memory_order_acq_rel || order == memory_order_release)
return __atomic_compare_exchange_n(&m_value, &expected, desired, false, memory_order_release, memory_order_acquire);
else
return __atomic_compare_exchange_n(&m_value, &expected, desired, false, order, order);
}
T operator++() volatile noexcept
{
return fetch_add(1) + 1;
}
T operator++(int) volatile noexcept
{
return fetch_add(1);
}
T operator+=(T val) volatile noexcept
{
return fetch_add(val) + val;
}
T fetch_add(T val, MemoryOrder order = memory_order_seq_cst) volatile noexcept
{
return __atomic_fetch_add(&m_value, val, order);
}
T operator--() volatile noexcept
{
return fetch_sub(1) - 1;
}
T operator--(int) volatile noexcept
{
return fetch_sub(1);
}
T operator-=(T val) volatile noexcept
{
return fetch_sub(val) - val;
}
T fetch_sub(T val, MemoryOrder order = memory_order_seq_cst) volatile noexcept
{
return __atomic_fetch_sub(&m_value, val, order);
}
T operator&=(T val) volatile noexcept
{
return fetch_and(val) & val;
}
T fetch_and(T val, MemoryOrder order = memory_order_seq_cst) volatile noexcept
{
return __atomic_fetch_and(&m_value, val, order);
}
T operator|=(T val) volatile noexcept
{
return fetch_or(val) | val;
}
T fetch_or(T val, MemoryOrder order = memory_order_seq_cst) volatile noexcept
{
return __atomic_fetch_or(&m_value, val, order);
}
T operator^=(T val) volatile noexcept
{
return fetch_xor(val) ^ val;
}
T fetch_xor(T val, MemoryOrder order = memory_order_seq_cst) volatile noexcept
{
return __atomic_fetch_xor(&m_value, val, order);
}
operator T() const volatile noexcept
{
return load();
}
T load(MemoryOrder order = memory_order_seq_cst) const volatile noexcept
{
return __atomic_load_n(&m_value, order);
}
T operator=(T desired) volatile noexcept
{
store(desired);
return desired;
}
void store(T desired, MemoryOrder order = memory_order_seq_cst) volatile noexcept
{
__atomic_store_n(&m_value, desired, order);
}
bool is_lock_free() const volatile noexcept
{
return __atomic_is_lock_free(sizeof(m_value), &m_value);
}
};
template <typename T>
class Atomic<T*>
{
T* m_value { nullptr };
public:
Atomic() noexcept = default;
Atomic(const Atomic&) = delete;
Atomic& operator=(const Atomic&) volatile = delete;
Atomic(T* val) noexcept:
m_value(val)
{
}
T* exchange(T* desired, MemoryOrder order = memory_order_seq_cst) volatile noexcept
{
return __atomic_exchange_n(&m_value, desired, order);
}
bool compare_exchange_strong(T*& expected, T* desired, MemoryOrder order = memory_order_seq_cst) volatile noexcept
{
if (order == memory_order_acq_rel || order == memory_order_release)
return __atomic_compare_exchange_n(&m_value, &expected, desired, false, memory_order_release, memory_order_acquire);
else
return __atomic_compare_exchange_n(&m_value, &expected, desired, false, order, order);
}
T* operator++() volatile noexcept
{
return fetch_add(1) + 1;
}
T* operator++(int) volatile noexcept
{
return fetch_add(1);
}
T* operator+=(ptrdiff_t val) volatile noexcept
{
return fetch_add(val) + val;
}
T* fetch_add(ptrdiff_t val, MemoryOrder order = memory_order_seq_cst) volatile noexcept
{
return __atomic_fetch_add(&m_value, val * sizeof(*m_value), order);
}
T* operator--() volatile noexcept
{
return fetch_sub(1) - 1;
}
T* operator--(int) volatile noexcept
{
return fetch_sub(1);
}
T* operator-=(ptrdiff_t val) volatile noexcept
{
return fetch_sub(val) - val;
}
T* fetch_sub(ptrdiff_t val, MemoryOrder order = memory_order_seq_cst) volatile noexcept
{
return __atomic_fetch_sub(&m_value, val * sizeof(*m_value), order);
}
operator T*() const volatile noexcept
{
return load();
}
T* load(MemoryOrder order = memory_order_seq_cst) const volatile noexcept
{
return __atomic_load_n(&m_value, order);
}
T* operator=(T* desired) volatile noexcept
{
store(desired);
return desired;
}
void store(T* desired, MemoryOrder order = memory_order_seq_cst) volatile noexcept
{
__atomic_store_n(&m_value, desired, order);
}
bool is_lock_free() const volatile noexcept
{
return __atomic_is_lock_free(sizeof(m_value), &m_value);
}
};
}
using AK::Atomic;

View file

@ -1,4 +1,4 @@
PROGRAMS = TestString TestQueue TestVector TestHashMap TestJSON TestWeakPtr TestNonnullRefPtr TestRefPtr TestFixedArray TestFileSystemPath TestURL TestStringView TestUtf8
PROGRAMS = TestAtomic TestString TestQueue TestVector TestHashMap TestJSON TestWeakPtr TestNonnullRefPtr TestRefPtr TestFixedArray TestFileSystemPath TestURL TestStringView TestUtf8
CXXFLAGS = -std=c++17 -Wall -Wextra -ggdb3 -O2 -I../ -I../../
@ -25,6 +25,9 @@ endef
all: $(PROGRAMS)
$(foreach x,$(PROGRAMS),$(call execute-command,./$(x)))
TestAtomic: TestAtomic.o $(SHARED_TEST_OBJS)
$(PRE_CXX) $(CXX) $(CXXFLAGS) -o $@ TestAtomic.o $(SHARED_TEST_OBJS)
TestString: TestString.o $(SHARED_TEST_OBJS)
$(PRE_CXX) $(CXX) $(CXXFLAGS) -o $@ TestString.o $(SHARED_TEST_OBJS)

338
AK/Tests/TestAtomic.cpp Normal file
View file

@ -0,0 +1,338 @@
#include <AK/TestSuite.h>
#include <AK/Atomic.h>
TEST_CASE(construct_empty)
{
EXPECT(Atomic<bool>().load() == false);
EXPECT(Atomic<u32>().load() == 0);
EXPECT(Atomic<u16>().load() == 0);
EXPECT(Atomic<u8>().load() == 0);
EXPECT(Atomic<u16*>().load() == nullptr);
}
TEST_CASE(construct_with_value)
{
EXPECT(Atomic<bool>(false).load() == false);
EXPECT(Atomic<bool>(true).load() == true);
EXPECT(Atomic<u32>(2).load() == 2);
EXPECT(Atomic<u16>(3).load() == 3);
EXPECT(Atomic<u8>(4).load() == 4);
u16 v_u16 = 0;
EXPECT(Atomic<u16*>(&v_u16).load() == &v_u16);
}
TEST_CASE(do_exchange)
{
Atomic<bool> a_bool(false);
EXPECT(a_bool.exchange(true) == false);
EXPECT(a_bool.load() == true && static_cast<bool>(a_bool) == true);
Atomic<u32> a_u32(2);
EXPECT(a_u32.exchange(22) == 2);
EXPECT(a_u32.load() == 22 && static_cast<u8>(a_u32) == 22);
Atomic<u16> a_u16(3);
EXPECT(a_u16.exchange(33) == 3);
EXPECT(a_u16.load() == 33 && static_cast<u8>(a_u16) == 33);
Atomic<u8> a_u8(4);
EXPECT(a_u8.exchange(44) == 4);
EXPECT(a_u8.load() == 44 && static_cast<u8>(a_u8) == 44);
u16 v_u16[6];
Atomic<u16*> a_pu16(&v_u16[2]);
EXPECT(a_pu16.load() == &v_u16[2] && static_cast<u16*>(a_pu16) == &v_u16[2]);
}
TEST_CASE(do_compare_exchange)
{
Atomic<bool> a_bool(false);
bool e_bool = true;
EXPECT(a_bool.compare_exchange_strong(e_bool, true) == false);
EXPECT(e_bool == false);
EXPECT(a_bool.load() == false && static_cast<bool>(a_bool) == false);
e_bool = false;
EXPECT(a_bool.compare_exchange_strong(e_bool, true) == true);
EXPECT(a_bool.load() == true && static_cast<bool>(a_bool) == true);
Atomic<u32> a_u32(2);
u32 e_u32 = 99;
EXPECT(a_u32.compare_exchange_strong(e_u32, 22) == false);
EXPECT(e_u32 == 2);
EXPECT(a_u32.load() == 2 && static_cast<u32>(a_u32) == 2);
e_u32 = 2;
EXPECT(a_u32.compare_exchange_strong(e_u32, 22) == true);
EXPECT(a_u32.load() == 22 && static_cast<u32>(a_u32) == 22);
Atomic<u16> a_u16(3);
u16 e_u16 = 99;
EXPECT(a_u16.compare_exchange_strong(e_u16, 33) == false);
EXPECT(e_u16 == 3);
EXPECT(a_u16.load() == 3 && static_cast<u16>(a_u16) == 3);
e_u16 = 3;
EXPECT(a_u16.compare_exchange_strong(e_u16, 33) == true);
EXPECT(a_u16.load() == 33 && static_cast<u16>(a_u16) == 33);
Atomic<u8> a_u8(4);
u8 e_u8 = 99;
EXPECT(a_u8.compare_exchange_strong(e_u8, 44) == false);
EXPECT(e_u8 == 4);
EXPECT(a_u8.load() == 4 && static_cast<u16>(a_u8) == 4);
e_u8 = 4;
EXPECT(a_u8.compare_exchange_strong(e_u8, 44) == true);
EXPECT(a_u8.load() == 44 && static_cast<u16>(a_u8) == 44);
}
TEST_CASE(fetch_add)
{
Atomic<u32> a_u32(5);
EXPECT(a_u32.fetch_add(2) == 5);
EXPECT(a_u32.load() == 7 && static_cast<u32>(a_u32) == 7);
Atomic<u16> a_u16(5);
EXPECT(a_u16.fetch_add(2) == 5);
EXPECT(a_u16.load() == 7 && static_cast<u16>(a_u16) == 7);
Atomic<u8> a_u8(5);
EXPECT(a_u8.fetch_add(2) == 5);
EXPECT(a_u8.load() == 7 && static_cast<u8>(a_u8) == 7);
u32 v_u32[6];
Atomic<u32*> a_pu32(&v_u32[2]);
EXPECT(a_pu32.load() == &v_u32[2] && static_cast<u32*>(a_pu32) == &v_u32[2]);
EXPECT(a_pu32.fetch_add(2) == &v_u32[2]);
EXPECT(a_pu32.load() == &v_u32[4] && static_cast<u32*>(a_pu32) == &v_u32[4]);
EXPECT(a_pu32.fetch_add(-3) == &v_u32[4]);
EXPECT(a_pu32.load() == &v_u32[1] && static_cast<u32*>(a_pu32) == &v_u32[1]);
u16 v_u16[6];
Atomic<u16*> a_pu16(&v_u16[2]);
EXPECT(a_pu16.load() == &v_u16[2] && static_cast<u16*>(a_pu16) == &v_u16[2]);
EXPECT(a_pu16.fetch_add(2) == &v_u16[2]);
EXPECT(a_pu16.load() == &v_u16[4] && static_cast<u16*>(a_pu16) == &v_u16[4]);
EXPECT(a_pu16.fetch_add(-3) == &v_u16[4]);
EXPECT(a_pu16.load() == &v_u16[1] && static_cast<u16*>(a_pu16) == &v_u16[1]);
u8 v_u8[6];
Atomic<u8*> a_pu8(&v_u8[2]);
EXPECT(a_pu8.load() == &v_u8[2] && static_cast<u8*>(a_pu8) == &v_u8[2]);
EXPECT(a_pu8.fetch_add(2) == &v_u8[2]);
EXPECT(a_pu8.load() == &v_u8[4] && static_cast<u8*>(a_pu8) == &v_u8[4]);
EXPECT(a_pu8.fetch_add(-3) == &v_u8[4]);
EXPECT(a_pu8.load() == &v_u8[1] && static_cast<u8*>(a_pu8) == &v_u8[1]);
}
TEST_CASE(fetch_sub)
{
Atomic<u32> a_u32(5);
EXPECT(a_u32.fetch_sub(2) == 5);
EXPECT(a_u32.load() == 3 && static_cast<u32>(a_u32) == 3);
Atomic<u16> a_u16(5);
EXPECT(a_u16.fetch_sub(2) == 5);
EXPECT(a_u16.load() == 3 && static_cast<u16>(a_u16) == 3);
Atomic<u8> a_u8(5);
EXPECT(a_u8.fetch_sub(2) == 5);
EXPECT(a_u8.load() == 3 && static_cast<u8>(a_u8) == 3);
u32 v_u32[6];
Atomic<u32*> a_pu32(&v_u32[2]);
EXPECT(a_pu32.load() == &v_u32[2] && static_cast<u32*>(a_pu32) == &v_u32[2]);
EXPECT(a_pu32.fetch_sub(2) == &v_u32[2]);
EXPECT(a_pu32.load() == &v_u32[0] && static_cast<u32*>(a_pu32) == &v_u32[0]);
EXPECT(a_pu32.fetch_sub(-3) == &v_u32[0]);
EXPECT(a_pu32.load() == &v_u32[3] && static_cast<u32*>(a_pu32) == &v_u32[3]);
u16 v_u16[6];
Atomic<u16*> a_pu16(&v_u16[2]);
EXPECT(a_pu16.load() == &v_u16[2] && static_cast<u16*>(a_pu16) == &v_u16[2]);
EXPECT(a_pu16.fetch_sub(2) == &v_u16[2]);
EXPECT(a_pu16.load() == &v_u16[0] && static_cast<u16*>(a_pu16) == &v_u16[0]);
EXPECT(a_pu16.fetch_sub(-3) == &v_u16[0]);
EXPECT(a_pu16.load() == &v_u16[3] && static_cast<u16*>(a_pu16) == &v_u16[3]);
u8 v_u8[6];
Atomic<u8*> a_pu8(&v_u8[2]);
EXPECT(a_pu8.load() == &v_u8[2] && static_cast<u8*>(a_pu8) == &v_u8[2]);
EXPECT(a_pu8.fetch_sub(2) == &v_u8[2]);
EXPECT(a_pu8.load() == &v_u8[0] && static_cast<u8*>(a_pu8) == &v_u8[0]);
EXPECT(a_pu8.fetch_sub(-3) == &v_u8[0]);
EXPECT(a_pu8.load() == &v_u8[3] && static_cast<u8*>(a_pu8) == &v_u8[3]);
}
TEST_CASE(fetch_inc)
{
Atomic<u32> a_u32(5);
EXPECT(a_u32++ == 5);
EXPECT(a_u32.load() == 6 && a_u32 == 6);
EXPECT(++a_u32 == 7);
EXPECT(a_u32.load() == 7 && a_u32 == 7);
EXPECT((a_u32 += 2) == 9);
EXPECT(a_u32.load() == 9 && a_u32 == 9);
Atomic<u16> a_u16(5);
EXPECT(a_u16++ == 5);
EXPECT(a_u16.load() == 6 && a_u16 == 6);
EXPECT(++a_u16 == 7);
EXPECT(a_u16.load() == 7 && a_u16 == 7);
EXPECT((a_u16 += 2) == 9);
EXPECT(a_u16.load() == 9 && a_u16 == 9);
Atomic<u8> a_u8(5);
EXPECT(a_u8++ == 5);
EXPECT(a_u8.load() == 6 && a_u8 == 6);
EXPECT(++a_u8 == 7);
EXPECT(a_u8.load() == 7 && a_u8 == 7);
EXPECT((a_u8 += 2) == 9);
EXPECT(a_u8.load() == 9 && a_u8 == 9);
u32 v_u32[8];
Atomic<u32*> a_pu32(&v_u32[2]);
EXPECT(a_pu32++ == &v_u32[2]);
EXPECT(a_pu32.load() == &v_u32[3] && a_pu32 == &v_u32[3]);
EXPECT(++a_pu32 == &v_u32[4]);
EXPECT(a_pu32.load() == &v_u32[4] && a_pu32 == &v_u32[4]);
EXPECT((a_pu32 += 2) == &v_u32[6]);
EXPECT(a_pu32.load() == &v_u32[6] && a_pu32 == &v_u32[6]);
u16 v_u16[8];
Atomic<u16*> a_pu16(&v_u16[2]);
EXPECT(a_pu16++ == &v_u16[2]);
EXPECT(a_pu16.load() == &v_u16[3] && a_pu16 == &v_u16[3]);
EXPECT(++a_pu16 == &v_u16[4]);
EXPECT(a_pu16.load() == &v_u16[4] && a_pu16 == &v_u16[4]);
EXPECT((a_pu16 += 2) == &v_u16[6]);
EXPECT(a_pu16.load() == &v_u16[6] && a_pu16 == &v_u16[6]);
u8 v_u8[8];
Atomic<u8*> a_pu8(&v_u8[2]);
EXPECT(a_pu8++ == &v_u8[2]);
EXPECT(a_pu8.load() == &v_u8[3] && a_pu8 == &v_u8[3]);
EXPECT(++a_pu8 == &v_u8[4]);
EXPECT(a_pu8.load() == &v_u8[4] && a_pu8 == &v_u8[4]);
EXPECT((a_pu8 += 2) == &v_u8[6]);
EXPECT(a_pu8.load() == &v_u8[6] && a_pu8 == &v_u8[6]);
}
TEST_CASE(fetch_dec)
{
Atomic<u32> a_u32(5);
EXPECT(a_u32-- == 5);
EXPECT(a_u32.load() == 4 && a_u32 == 4);
EXPECT(--a_u32 == 3);
EXPECT(a_u32.load() == 3 && a_u32 == 3);
EXPECT((a_u32 -= 2) == 1);
EXPECT(a_u32.load() == 1 && a_u32 == 1);
Atomic<u16> a_u16(5);
EXPECT(a_u16-- == 5);
EXPECT(a_u16.load() == 4 && a_u16 == 4);
EXPECT(--a_u16 == 3);
EXPECT(a_u16.load() == 3 && a_u16 == 3);
EXPECT((a_u16 -= 2) == 1);
EXPECT(a_u16.load() == 1 && a_u16 == 1);
Atomic<u8> a_u8(5);
EXPECT(a_u8-- == 5);
EXPECT(a_u8.load() == 4 && a_u8 == 4);
EXPECT(--a_u8 == 3);
EXPECT(a_u8.load() == 3 && a_u8 == 3);
EXPECT((a_u8 -= 2) == 1);
EXPECT(a_u8.load() == 1 && a_u8 == 1);
u32 v_u32[8];
Atomic<u32*> a_pu32(&v_u32[7]);
EXPECT(a_pu32-- == &v_u32[7]);
EXPECT(a_pu32.load() == &v_u32[6] && a_pu32 == &v_u32[6]);
EXPECT(--a_pu32 == &v_u32[5]);
EXPECT(a_pu32.load() == &v_u32[5] && a_pu32 == &v_u32[5]);
EXPECT((a_pu32 -= 2) == &v_u32[3]);
EXPECT(a_pu32.load() == &v_u32[3] && a_pu32 == &v_u32[3]);
u16 v_u16[8];
Atomic<u16*> a_pu16(&v_u16[7]);
EXPECT(a_pu16-- == &v_u16[7]);
EXPECT(a_pu16.load() == &v_u16[6] && a_pu16 == &v_u16[6]);
EXPECT(--a_pu16 == &v_u16[5]);
EXPECT(a_pu16.load() == &v_u16[5] && a_pu16 == &v_u16[5]);
EXPECT((a_pu16 -= 2) == &v_u16[3]);
EXPECT(a_pu16.load() == &v_u16[3] && a_pu16 == &v_u16[3]);
u8 v_u8[8];
Atomic<u8*> a_pu8(&v_u8[7]);
EXPECT(a_pu8-- == &v_u8[7]);
EXPECT(a_pu8.load() == &v_u8[6] && a_pu8 == &v_u8[6]);
EXPECT(--a_pu8 == &v_u8[5]);
EXPECT(a_pu8.load() == &v_u8[5] && a_pu8 == &v_u8[5]);
EXPECT((a_pu8 -= 2) == &v_u8[3]);
EXPECT(a_pu8.load() == &v_u8[3] && a_pu8 == &v_u8[3]);
}
TEST_CASE(fetch_and)
{
Atomic<u32> a_u32(0xdeadbeef);
EXPECT(a_u32.fetch_and(0x8badf00d) == 0xdeadbeef);
EXPECT(a_u32.load() == 0x8aadb00d && static_cast<u32>(a_u32) == 0x8aadb00d);
a_u32 = 0xdeadbeef;
EXPECT((a_u32 &= 0x8badf00d) == 0x8aadb00d);
Atomic<u16> a_u16(0xbeef);
EXPECT(a_u16.fetch_and(0xf00d) == 0xbeef);
EXPECT(a_u16.load() == 0xb00d && static_cast<u16>(a_u16) == 0xb00d);
a_u16 = 0xbeef;
EXPECT((a_u16 &= 0xf00d) == 0xb00d);
Atomic<u8> a_u8(0xef);
EXPECT(a_u8.fetch_and(0x0d) == 0xef);
EXPECT(a_u8.load() == 0x0d && static_cast<u8>(a_u8) == 0x0d);
a_u8 = 0xef;
EXPECT((a_u8 &= 0x0d) == 0x0d);
}
TEST_CASE(fetch_or)
{
Atomic<u32> a_u32(0xaadb00d);
EXPECT(a_u32.fetch_or(0xdeadbeef) == 0xaadb00d);
EXPECT(a_u32.load() == 0xdeadbeef && static_cast<u32>(a_u32) == 0xdeadbeef);
a_u32 = 0xaadb00d;
EXPECT((a_u32 |= 0xdeadbeef) == 0xdeadbeef);
Atomic<u16> a_u16(0xb00d);
EXPECT(a_u16.fetch_or(0xbeef) == 0xb00d);
EXPECT(a_u16.load() == 0xbeef && static_cast<u16>(a_u16) == 0xbeef);
a_u16 = 0xb00d;
EXPECT((a_u16 |= 0xbeef) == 0xbeef);
Atomic<u8> a_u8(0x0d);
EXPECT(a_u8.fetch_or(0xef) == 0x0d);
EXPECT(a_u8.load() == 0xef && static_cast<u8>(a_u8) == 0xef);
a_u8 = 0x0d;
EXPECT((a_u8 |= 0xef) == 0xef);
}
TEST_CASE(fetch_xor)
{
Atomic<u32> a_u32(0x55004ee2);
EXPECT(a_u32.fetch_xor(0xdeadbeef) == 0x55004ee2);
EXPECT(a_u32.load() == 0x8badf00d && static_cast<u32>(a_u32) == 0x8badf00d);
a_u32 = 0x55004ee2;
EXPECT((a_u32 ^= 0xdeadbeef) == 0x8badf00d);
Atomic<u16> a_u16(0x4ee2);
EXPECT(a_u16.fetch_xor(0xbeef) == 0x4ee2);
EXPECT(a_u16.load() == 0xf00d && static_cast<u16>(a_u16) == 0xf00d);
a_u16 = 0x4ee2;
EXPECT((a_u16 ^= 0xbeef) == 0xf00d);
Atomic<u8> a_u8(0xe2);
EXPECT(a_u8.fetch_xor(0xef) == 0xe2);
EXPECT(a_u8.load() == 0x0d && static_cast<u8>(a_u8) == 0x0d);
a_u8 = 0xe2;
EXPECT((a_u8 ^= 0xef) == 0x0d);
}
TEST_MAIN(Atomic)

View file

@ -9,15 +9,15 @@ void Lock::lock()
hang();
}
for (;;) {
if (CAS(&m_lock, 1, 0) == 0) {
bool expected = false;
if (m_lock.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) {
if (!m_holder || m_holder == current) {
m_holder = current;
++m_level;
memory_barrier();
m_lock = 0;
m_lock.store(false, AK::memory_order_release);
return;
}
m_lock = 0;
m_lock.store(false, AK::memory_order_release);
}
Scheduler::donate_to(m_holder, m_name);
}
@ -26,18 +26,17 @@ void Lock::lock()
void Lock::unlock()
{
for (;;) {
if (CAS(&m_lock, 1, 0) == 0) {
bool expected = false;
if (m_lock.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) {
ASSERT(m_holder == current);
ASSERT(m_level);
--m_level;
if (m_level) {
memory_barrier();
m_lock = 0;
m_lock.store(false, AK::memory_order_release);
return;
}
m_holder = nullptr;
memory_barrier();
m_lock = 0;
m_lock.store(false, AK::memory_order_release);
return;
}
Scheduler::donate_to(m_holder, m_name);
@ -47,25 +46,24 @@ void Lock::unlock()
bool Lock::unlock_if_locked()
{
for (;;) {
if (CAS(&m_lock, 1, 0) == 0) {
bool expected = false;
if (m_lock.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) {
if (m_level == 0) {
m_lock = 0;
m_lock.store(false, AK::memory_order_release);
return false;
}
if (m_holder != current) {
m_lock = 0;
m_lock.store(false, AK::memory_order_release);
return false;
}
ASSERT(m_level);
--m_level;
if (m_level) {
memory_barrier();
m_lock = 0;
m_lock.store(false, AK::memory_order_release);
return false;
}
m_holder = nullptr;
memory_barrier();
m_lock = 0;
m_lock.store(false, AK::memory_order_release);
return true;
}
}

View file

@ -2,6 +2,7 @@
#include <AK/Assertions.h>
#include <AK/Types.h>
#include <AK/Atomic.h>
#include <Kernel/Arch/i386/CPU.h>
#include <Kernel/KSyms.h>
#include <Kernel/Scheduler.h>
@ -9,17 +10,6 @@
class Thread;
extern Thread* current;
static inline u32 CAS(volatile u32* mem, u32 newval, u32 oldval)
{
u32 ret;
asm volatile(
"cmpxchgl %2, %1"
: "=a"(ret), "+m"(*mem)
: "r"(newval), "0"(oldval)
: "cc", "memory");
return ret;
}
class Lock {
public:
Lock(const char* name = nullptr)
@ -35,7 +25,7 @@ public:
const char* name() const { return m_name; }
private:
volatile u32 m_lock { 0 };
Atomic<bool> m_lock { false };
u32 m_level { 0 };
Thread* m_holder { nullptr };
const char* m_name { nullptr };

View file

@ -4,22 +4,9 @@
#include <AK/Assertions.h>
#include <AK/Types.h>
#include <AK/Atomic.h>
#include <unistd.h>
#define memory_barrier() asm volatile("" :: \
: "memory")
static inline u32 CAS(volatile u32* mem, u32 newval, u32 oldval)
{
u32 ret;
asm volatile(
"cmpxchgl %2, %1"
: "=a"(ret), "+m"(*mem)
: "r"(newval), "0"(oldval)
: "cc", "memory");
return ret;
}
namespace LibThread {
class Lock {
@ -31,7 +18,7 @@ public:
void unlock();
private:
volatile u32 m_lock { 0 };
AK::Atomic<bool> m_lock { false };
u32 m_level { 0 };
int m_holder { -1 };
};
@ -55,15 +42,15 @@ private:
{
int tid = gettid();
for (;;) {
if (CAS(&m_lock, 1, 0) == 0) {
bool expected = false;
if (m_lock.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) {
if (m_holder == -1 || m_holder == tid) {
m_holder = tid;
++m_level;
memory_barrier();
m_lock = 0;
m_lock.store(false, AK::memory_order_release);
return;
}
m_lock = 0;
m_lock.store(false, AK::memory_order_release);
}
donate(m_holder);
}
@ -72,18 +59,17 @@ private:
inline void Lock::unlock()
{
for (;;) {
if (CAS(&m_lock, 1, 0) == 0) {
bool expected = false;
if (m_lock.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) {
ASSERT(m_holder == gettid());
ASSERT(m_level);
--m_level;
if (m_level) {
memory_barrier();
m_lock = 0;
m_lock.store(false, AK::memory_order_release);
return;
}
m_holder = -1;
memory_barrier();
m_lock = 0;
m_lock.store(false, AK::memory_order_release);
return;
}
donate(m_holder);