From 054b41383f25b5dd4ad229faf778601de0618c20 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 1 Mar 2024 15:48:08 +0200 Subject: [PATCH] Kernel: Implement SpinLock unsafe that does not keep track of locker This will be used in scheduler where thread ids are changing --- kernel/include/kernel/Lock/SpinLock.h | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/kernel/include/kernel/Lock/SpinLock.h b/kernel/include/kernel/Lock/SpinLock.h index f7ce17215a..0974a6820f 100644 --- a/kernel/include/kernel/Lock/SpinLock.h +++ b/kernel/include/kernel/Lock/SpinLock.h @@ -40,6 +40,37 @@ namespace Kernel uint32_t m_lock_depth { 0 }; }; + class SpinLockUnsafe + { + BAN_NON_COPYABLE(SpinLockUnsafe); + BAN_NON_MOVABLE(SpinLockUnsafe); + + public: + SpinLockUnsafe() = default; + + InterruptState lock() + { + auto state = get_interrupt_state(); + set_interrupt_state(InterruptState::Disabled); + + while (!m_locked.compare_exchange(false, true)) + __builtin_ia32_pause(); + + return state; + } + + void unlock(InterruptState state) + { + m_locked.store(false); + set_interrupt_state(state); + } + + bool is_locked() const { return m_locked; } + + private: + BAN::Atomic m_locked; + }; + template class SpinLockGuard {