diff --git a/kernel/include/kernel/SpinLock.h b/kernel/include/kernel/SpinLock.h index 82bd113867..c3fb8cafb7 100644 --- a/kernel/include/kernel/SpinLock.h +++ b/kernel/include/kernel/SpinLock.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -19,7 +20,7 @@ namespace Kernel bool is_locked() const; private: - volatile pid_t m_locker = -1; + BAN::Atomic m_locker = -1; }; class RecursiveSpinLock @@ -34,8 +35,8 @@ namespace Kernel bool is_locked() const; private: - pid_t m_locker = -1; - uint32_t m_lock_depth = 0; + BAN::Atomic m_locker = -1; + BAN::Atomic m_lock_depth = 0; SpinLock m_lock; }; diff --git a/kernel/kernel/SpinLock.cpp b/kernel/kernel/SpinLock.cpp index 905fa02160..e79c47f876 100644 --- a/kernel/kernel/SpinLock.cpp +++ b/kernel/kernel/SpinLock.cpp @@ -1,6 +1,5 @@ #include #include -#include namespace Kernel { @@ -8,31 +7,18 @@ namespace Kernel void SpinLock::lock() { pid_t tid = Scheduler::current_tid(); - while (true) - { - { - CriticalScope _; - ASSERT(m_locker != tid); - if (m_locker == -1) - { - m_locker = tid; - break; - } - } + while (!m_locker.compare_exchange(-1, tid)) Scheduler::get().reschedule(); - } } void SpinLock::unlock() { - CriticalScope _; ASSERT(m_locker == Scheduler::current_tid()); m_locker = -1; } bool SpinLock::is_locked() const { - CriticalScope _; return m_locker != -1; }