Compare commits

..

No commits in common. "38bd3d3eef10cb28db2088a25240bfd179228304" and "79078ff21e80c389dc239d964af601a58bd146ea" have entirely different histories.

9 changed files with 42 additions and 39 deletions

View File

@ -18,6 +18,7 @@ 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

@ -1,11 +1,6 @@
#include <luna/DebugLog.h> #include "thread/Spinlock.h"
#include <luna/Spinlock.h> #include "Log.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()
{ {
@ -13,7 +8,7 @@ void Spinlock::lock()
while (!m_lock.compare_exchange_strong(expected, 1)) while (!m_lock.compare_exchange_strong(expected, 1))
{ {
expected = 0; expected = 0;
pause(); CPU::pause();
} }
} }
@ -28,7 +23,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))
{ {
dbgln("Spinlock::unlock() called on an unlocked lock with value %d", expected); kwarnln("Spinlock::unlock() called on an unlocked lock with value %d", expected);
} }
} }
@ -39,7 +34,7 @@ ScopeLock::ScopeLock(Spinlock& lock) : m_lock(lock)
ScopeLock::~ScopeLock() ScopeLock::~ScopeLock()
{ {
if (!m_taken_over) m_lock.unlock(); m_lock.unlock();
} }
const u32 RETRIES = 5000000; const u32 RETRIES = 5000000;
@ -47,7 +42,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) { pause(); } while (!lock.try_lock() && --tries_left) { CPU::pause(); }
if (tries_left) m_success = true; if (tries_left) m_success = true;
} }

View File

@ -1,4 +1,6 @@
#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>
@ -28,15 +30,8 @@ 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
@ -112,11 +107,25 @@ 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

@ -16,8 +16,6 @@ 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

@ -138,16 +138,26 @@ template <typename T> class Option
private: private:
struct Storage struct Storage
{ {
alignas(T) u8 buffer[sizeof(T)]; u8 buffer[sizeof(T)];
T* fetch_ptr()
{
return (T*)buffer;
}
T& fetch_reference() T& fetch_reference()
{ {
return *__builtin_launder(reinterpret_cast<T*>(&buffer)); return *fetch_ptr();
}
const T* fetch_ptr() const
{
return (const T*)buffer;
} }
const T& fetch_reference() const const T& fetch_reference() const
{ {
return *__builtin_launder(reinterpret_cast<const T*>(&buffer)); return *fetch_ptr();
} }
void store_reference(const T& ref) void store_reference(const T& ref)

View File

@ -6,7 +6,6 @@
#include <luna/LinkedList.h> #include <luna/LinkedList.h>
#include <luna/SafeArithmetic.h> #include <luna/SafeArithmetic.h>
#include <luna/ScopeGuard.h> #include <luna/ScopeGuard.h>
#include <luna/Spinlock.h>
#include <luna/SystemError.h> #include <luna/SystemError.h>
#ifdef USE_FREESTANDING #ifdef USE_FREESTANDING
@ -46,10 +45,9 @@ static_assert(sizeof(HeapBlock) == 48UL);
static const isize HEAP_BLOCK_SIZE = 48; static const isize HEAP_BLOCK_SIZE = 48;
static LinkedList<HeapBlock> heap; static LinkedList<HeapBlock> heap;
static Spinlock g_heap_lock;
// If we're allocating a large amount of memory, map enough pages for it, but otherwise just use the default amount // If we're allocating a large amount of memory, map enough pages for it, but otherwise just use the default amount of
// of pages. // pages.
static usize get_pages_for_allocation(usize bytes) static usize get_pages_for_allocation(usize bytes)
{ {
usize pages = get_blocks_from_size(bytes, PAGE_SIZE); usize pages = get_blocks_from_size(bytes, PAGE_SIZE);
@ -180,8 +178,6 @@ Result<void*> malloc_impl(usize size, bool should_scrub)
{ {
if (!size) return (void*)BLOCK_MAGIC; if (!size) return (void*)BLOCK_MAGIC;
ScopeLock lock(g_heap_lock);
size = align_up<16>(size); size = align_up<16>(size);
Option<HeapBlock*> block = heap.first(); Option<HeapBlock*> block = heap.first();
@ -235,8 +231,6 @@ Result<void> free_impl(void* ptr)
if (ptr == (void*)BLOCK_MAGIC) return {}; // This pointer was returned from a call to malloc(0) if (ptr == (void*)BLOCK_MAGIC) return {}; // This pointer was returned from a call to malloc(0)
if (!ptr) return {}; if (!ptr) return {};
ScopeLock lock(g_heap_lock);
HeapBlock* block = get_heap_block_for_pointer(ptr); HeapBlock* block = get_heap_block_for_pointer(ptr);
if (block->magic != BLOCK_MAGIC) if (block->magic != BLOCK_MAGIC)
@ -292,8 +286,6 @@ Result<void*> realloc_impl(void* ptr, usize size)
return (void*)BLOCK_MAGIC; return (void*)BLOCK_MAGIC;
} }
ScopeLock lock(g_heap_lock);
HeapBlock* const block = get_heap_block_for_pointer(ptr); HeapBlock* const block = get_heap_block_for_pointer(ptr);
if (block->magic != BLOCK_MAGIC) if (block->magic != BLOCK_MAGIC)
@ -335,8 +327,6 @@ Result<void*> realloc_impl(void* ptr, usize size)
usize old_size = block->req_size; usize old_size = block->req_size;
lock.take_over().unlock();
void* const new_ptr = TRY(malloc_impl(size, false)); void* const new_ptr = TRY(malloc_impl(size, false));
memcpy(new_ptr, ptr, old_size > size ? size : old_size); memcpy(new_ptr, ptr, old_size > size ? size : old_size);
TRY(free_impl(ptr)); TRY(free_impl(ptr));