kernel, luna: Move Spinlock.h to luna

This commit is contained in:
apio 2023-02-14 20:05:00 +01:00
parent 6eff41e7af
commit 4e88a7482c
Signed by: apio
GPG Key ID: B8A7D06E42258954
7 changed files with 24 additions and 27 deletions

View File

@ -18,7 +18,6 @@ set(SOURCES
src/arch/Serial.cpp src/arch/Serial.cpp
src/arch/Timer.cpp src/arch/Timer.cpp
src/arch/PCI.cpp src/arch/PCI.cpp
src/thread/Spinlock.cpp
src/thread/Thread.cpp src/thread/Thread.cpp
src/thread/Scheduler.cpp src/thread/Scheduler.cpp
src/sys/Syscall.cpp src/sys/Syscall.cpp

View File

@ -2,10 +2,10 @@
#include "arch/CPU.h" #include "arch/CPU.h"
#include "arch/Serial.h" #include "arch/Serial.h"
#include "arch/Timer.h" #include "arch/Timer.h"
#include "thread/Spinlock.h"
#include "video/TextConsole.h" #include "video/TextConsole.h"
#include <luna/Format.h> #include <luna/Format.h>
#include <luna/SourceLocation.h> #include <luna/SourceLocation.h>
#include <luna/Spinlock.h>
static bool g_debug_enabled = true; static bool g_debug_enabled = true;
static bool g_serial_enabled = true; static bool g_serial_enabled = true;

View File

@ -1,7 +1,7 @@
#include "memory/KernelVM.h" #include "memory/KernelVM.h"
#include "arch/MMU.h" #include "arch/MMU.h"
#include "thread/Spinlock.h"
#include <luna/Bitmap.h> #include <luna/Bitmap.h>
#include <luna/Spinlock.h>
static const u64 KERNEL_VM_RANGE_START = 0xffffffffc0000000; static const u64 KERNEL_VM_RANGE_START = 0xffffffffc0000000;

View File

@ -2,10 +2,10 @@
#include "arch/MMU.h" #include "arch/MMU.h"
#include "memory/KernelVM.h" #include "memory/KernelVM.h"
#include "memory/MemoryMap.h" #include "memory/MemoryMap.h"
#include "thread/Spinlock.h"
#include <luna/Alignment.h> #include <luna/Alignment.h>
#include <luna/Bitmap.h> #include <luna/Bitmap.h>
#include <luna/ScopeGuard.h> #include <luna/ScopeGuard.h>
#include <luna/Spinlock.h>
#include <luna/SystemError.h> #include <luna/SystemError.h>
#include <luna/Types.h> #include <luna/Types.h>

View File

@ -16,6 +16,8 @@ set(FREESTANDING_SOURCES
src/TarStream.cpp src/TarStream.cpp
src/DebugLog.cpp src/DebugLog.cpp
src/Heap.cpp src/Heap.cpp
src/Spinlock.cpp
src/UBSAN.cpp
) )
set(SOURCES set(SOURCES

View File

@ -1,6 +1,4 @@
#pragma once #pragma once
#include "Log.h"
#include "arch/CPU.h"
#include <luna/Atomic.h> #include <luna/Atomic.h>
#include <luna/Option.h> #include <luna/Option.h>
@ -30,8 +28,15 @@ class ScopeLock
ScopeLock(const ScopeLock&) = delete; ScopeLock(const ScopeLock&) = delete;
ScopeLock(ScopeLock&&) = delete; ScopeLock(ScopeLock&&) = delete;
Spinlock& take_over()
{
m_taken_over = true;
return m_lock;
}
private: private:
Spinlock& m_lock; Spinlock& m_lock;
bool m_taken_over { false };
}; };
class SafeScopeLock class SafeScopeLock
@ -107,25 +112,11 @@ template <typename T> class LockedValue
{ {
} }
#ifndef LOCKED_VALUE_DEBUG
LockedValueGuard lock() LockedValueGuard lock()
{ {
m_lock.lock(); m_lock.lock();
return { *this }; 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<LockedValueGuard> try_lock() Option<LockedValueGuard> try_lock()
{ {

View File

@ -1,6 +1,11 @@
#include "thread/Spinlock.h" #include <luna/DebugLog.h>
#include "Log.h" #include <luna/Spinlock.h>
#include "arch/CPU.h"
#ifdef ARCH_X86_64
#define pause() asm volatile("pause")
#else
#error "Unsupported architecture"
#endif
void Spinlock::lock() void Spinlock::lock()
{ {
@ -8,7 +13,7 @@ void Spinlock::lock()
while (!m_lock.compare_exchange_strong(expected, 1)) while (!m_lock.compare_exchange_strong(expected, 1))
{ {
expected = 0; expected = 0;
CPU::pause(); pause();
} }
} }
@ -23,7 +28,7 @@ void Spinlock::unlock()
int expected = 1; int expected = 1;
if (!m_lock.compare_exchange_strong(expected, 0)) 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() ScopeLock::~ScopeLock()
{ {
m_lock.unlock(); if (!m_taken_over) m_lock.unlock();
} }
const u32 RETRIES = 5000000; const u32 RETRIES = 5000000;
@ -42,7 +47,7 @@ const u32 RETRIES = 5000000;
SafeScopeLock::SafeScopeLock(Spinlock& lock) : m_lock(lock) SafeScopeLock::SafeScopeLock(Spinlock& lock) : m_lock(lock)
{ {
u32 tries_left = RETRIES; 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; if (tries_left) m_success = true;
} }