From 4e88a7482ce73b9bca242362698a88a6e485b370 Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 14 Feb 2023 20:05:00 +0100 Subject: [PATCH] kernel, luna: Move Spinlock.h to luna --- kernel/CMakeLists.txt | 1 - kernel/src/Log.cpp | 2 +- kernel/src/memory/KernelVM.cpp | 2 +- kernel/src/memory/MemoryManager.cpp | 2 +- luna/CMakeLists.txt | 2 ++ .../thread => luna/include/luna}/Spinlock.h | 23 ++++++------------- {kernel/src/thread => luna/src}/Spinlock.cpp | 19 +++++++++------ 7 files changed, 24 insertions(+), 27 deletions(-) rename {kernel/src/thread => luna/include/luna}/Spinlock.h (86%) rename {kernel/src/thread => luna/src}/Spinlock.cpp (66%) diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index f71b3d48..7cc661f6 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -18,7 +18,6 @@ set(SOURCES src/arch/Serial.cpp src/arch/Timer.cpp src/arch/PCI.cpp - src/thread/Spinlock.cpp src/thread/Thread.cpp src/thread/Scheduler.cpp src/sys/Syscall.cpp diff --git a/kernel/src/Log.cpp b/kernel/src/Log.cpp index d28a5e2d..20ecc26a 100644 --- a/kernel/src/Log.cpp +++ b/kernel/src/Log.cpp @@ -2,10 +2,10 @@ #include "arch/CPU.h" #include "arch/Serial.h" #include "arch/Timer.h" -#include "thread/Spinlock.h" #include "video/TextConsole.h" #include #include +#include static bool g_debug_enabled = true; static bool g_serial_enabled = true; diff --git a/kernel/src/memory/KernelVM.cpp b/kernel/src/memory/KernelVM.cpp index 48c4b80a..1773ba14 100644 --- a/kernel/src/memory/KernelVM.cpp +++ b/kernel/src/memory/KernelVM.cpp @@ -1,7 +1,7 @@ #include "memory/KernelVM.h" #include "arch/MMU.h" -#include "thread/Spinlock.h" #include +#include static const u64 KERNEL_VM_RANGE_START = 0xffffffffc0000000; diff --git a/kernel/src/memory/MemoryManager.cpp b/kernel/src/memory/MemoryManager.cpp index 3c010cda..96818eb7 100644 --- a/kernel/src/memory/MemoryManager.cpp +++ b/kernel/src/memory/MemoryManager.cpp @@ -2,10 +2,10 @@ #include "arch/MMU.h" #include "memory/KernelVM.h" #include "memory/MemoryMap.h" -#include "thread/Spinlock.h" #include #include #include +#include #include #include diff --git a/luna/CMakeLists.txt b/luna/CMakeLists.txt index 1b01daf5..b3eba9a3 100644 --- a/luna/CMakeLists.txt +++ b/luna/CMakeLists.txt @@ -16,6 +16,8 @@ set(FREESTANDING_SOURCES src/TarStream.cpp src/DebugLog.cpp src/Heap.cpp + src/Spinlock.cpp + src/UBSAN.cpp ) set(SOURCES diff --git a/kernel/src/thread/Spinlock.h b/luna/include/luna/Spinlock.h similarity index 86% rename from kernel/src/thread/Spinlock.h rename to luna/include/luna/Spinlock.h index 2b8f2e5a..0b284c17 100644 --- a/kernel/src/thread/Spinlock.h +++ b/luna/include/luna/Spinlock.h @@ -1,6 +1,4 @@ #pragma once -#include "Log.h" -#include "arch/CPU.h" #include #include @@ -30,8 +28,15 @@ class ScopeLock ScopeLock(const ScopeLock&) = delete; ScopeLock(ScopeLock&&) = delete; + Spinlock& take_over() + { + m_taken_over = true; + return m_lock; + } + private: Spinlock& m_lock; + bool m_taken_over { false }; }; class SafeScopeLock @@ -107,25 +112,11 @@ template class LockedValue { } -#ifndef LOCKED_VALUE_DEBUG LockedValueGuard lock() { m_lock.lock(); return { *this }; } -#else - LockedValueGuard lock() - { - if (m_lock.try_lock()) { return { *this }; } - - kwarnln("Spinning on a locked LockedValue. This might lead to a deadlock..."); - - CPU::print_stack_trace(); - - m_lock.lock(); - return { *this }; - } -#endif Option try_lock() { diff --git a/kernel/src/thread/Spinlock.cpp b/luna/src/Spinlock.cpp similarity index 66% rename from kernel/src/thread/Spinlock.cpp rename to luna/src/Spinlock.cpp index b1744f09..2896aebe 100644 --- a/kernel/src/thread/Spinlock.cpp +++ b/luna/src/Spinlock.cpp @@ -1,6 +1,11 @@ -#include "thread/Spinlock.h" -#include "Log.h" -#include "arch/CPU.h" +#include +#include + +#ifdef ARCH_X86_64 +#define pause() asm volatile("pause") +#else +#error "Unsupported architecture" +#endif void Spinlock::lock() { @@ -8,7 +13,7 @@ void Spinlock::lock() while (!m_lock.compare_exchange_strong(expected, 1)) { expected = 0; - CPU::pause(); + pause(); } } @@ -23,7 +28,7 @@ void Spinlock::unlock() int expected = 1; if (!m_lock.compare_exchange_strong(expected, 0)) { - kwarnln("Spinlock::unlock() called on an unlocked lock with value %d", expected); + dbgln("Spinlock::unlock() called on an unlocked lock with value %d", expected); } } @@ -34,7 +39,7 @@ ScopeLock::ScopeLock(Spinlock& lock) : m_lock(lock) ScopeLock::~ScopeLock() { - m_lock.unlock(); + if (!m_taken_over) m_lock.unlock(); } const u32 RETRIES = 5000000; @@ -42,7 +47,7 @@ const u32 RETRIES = 5000000; SafeScopeLock::SafeScopeLock(Spinlock& lock) : m_lock(lock) { u32 tries_left = RETRIES; - while (!lock.try_lock() && --tries_left) { CPU::pause(); } + while (!lock.try_lock() && --tries_left) { pause(); } if (tries_left) m_success = true; }