From ca8e7b40bc6817de79bf112b12263a6004526ad3 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 30 Jan 2024 01:07:06 +0200 Subject: [PATCH] Kernel: Implement SpinLock without CriticalScope This actually is not even spinlock since it yields the current thread. It will become one when I get to SMP though... --- kernel/include/kernel/SpinLock.h | 7 ++++--- kernel/kernel/SpinLock.cpp | 16 +--------------- 2 files changed, 5 insertions(+), 18 deletions(-) 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; }