diff options
author | Tom <tomut@yahoo.com> | 2019-10-12 11:17:34 -0600 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-10-12 19:30:59 +0200 |
commit | b0773a8ea649fd524af83504c2f9dff02ce46ee8 (patch) | |
tree | c8c843abb1dad372e346731e2843a53dbc26bbce /Kernel | |
parent | 2530378f59ff8152a3da94c33569f2c246c9f761 (diff) | |
download | serenity-b0773a8ea649fd524af83504c2f9dff02ce46ee8.zip |
AK: Add Atomic.h
Use gcc built-in atomics
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/Lock.cpp | 30 | ||||
-rw-r--r-- | Kernel/Lock.h | 14 |
2 files changed, 16 insertions, 28 deletions
diff --git a/Kernel/Lock.cpp b/Kernel/Lock.cpp index 7344685307..511d4b3e72 100644 --- a/Kernel/Lock.cpp +++ b/Kernel/Lock.cpp @@ -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; } } diff --git a/Kernel/Lock.h b/Kernel/Lock.h index 755edf503e..77893f2819 100644 --- a/Kernel/Lock.h +++ b/Kernel/Lock.h @@ -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 }; |