Kernel: Refactor includes to reduce include dependencies

Now modifying Scheduler.h or Thread.h doesnt trigger practically a full
kernel rebuild. This required moving Mutex and RWLock of of line but
that should be fine :^)
This commit is contained in:
2026-08-19 21:32:36 +03:00
parent 8cd9e6dfa8
commit bf0bd19289
62 changed files with 321 additions and 214 deletions
+134
View File
@@ -0,0 +1,134 @@
#include <kernel/Lock/Mutex.h>
#include <kernel/Thread.h>
namespace Kernel
{
bool Mutex::try_lock()
{
const auto tid = Thread::current_tid();
if (tid == m_locker)
ASSERT(m_lock_depth > 0);
else
{
pid_t expected = -1;
if (!m_locker.compare_exchange(expected, tid))
return false;
ASSERT(m_lock_depth == 0);
if (tid)
Thread::current().add_mutex();
}
m_lock_depth++;
return true;
}
void Mutex::lock()
{
const auto tid = Thread::current_tid();
if (tid == m_locker)
ASSERT(m_lock_depth > 0);
else
{
ASSERT(!tid || !Thread::current().has_spinlock());
pid_t expected = -1;
while (!m_locker.compare_exchange(expected, tid))
{
ASSERT(Processor::get_interrupt_state() == InterruptState::Enabled);
Processor::yield();
expected = -1;
}
ASSERT(m_lock_depth == 0);
if (tid)
Thread::current().add_mutex();
}
m_lock_depth++;
}
void Mutex::unlock()
{
const auto tid = Thread::current_tid();
ASSERT(m_locker == tid);
ASSERT(m_lock_depth > 0);
if (--m_lock_depth == 0)
{
m_locker = -1;
if (tid)
Thread::current().remove_mutex();
}
}
bool Mutex::is_locked_by_current_thread() const
{
return m_locker == Thread::current_tid();
}
bool PriorityMutex::try_lock()
{
const auto tid = Thread::current_tid();
if (tid == m_locker)
ASSERT(m_lock_depth > 0);
else
{
bool has_priority = tid ? !Thread::current().is_userspace() : true;
pid_t expected = -1;
if (!(has_priority || m_queue_length == 0) || !m_locker.compare_exchange(expected, tid))
return false;
if (has_priority)
m_queue_length++;
ASSERT(m_lock_depth == 0);
if (tid)
Thread::current().add_mutex();
}
m_lock_depth++;
return true;
}
void PriorityMutex::lock()
{
const auto tid = Thread::current_tid();
if (tid == m_locker)
ASSERT(m_lock_depth > 0);
else
{
ASSERT(!tid || !Thread::current().has_spinlock());
bool has_priority = tid ? !Thread::current().is_userspace() : true;
if (has_priority)
m_queue_length++;
pid_t expected = -1;
while (!(has_priority || m_queue_length == 0) || !m_locker.compare_exchange(expected, tid))
{
ASSERT(Processor::get_interrupt_state() == InterruptState::Enabled);
Processor::yield();
expected = -1;
}
ASSERT(m_lock_depth == 0);
if (tid)
Thread::current().add_mutex();
}
m_lock_depth++;
}
void PriorityMutex::unlock()
{
const auto tid = Thread::current_tid();
ASSERT(m_locker == tid);
ASSERT(m_lock_depth > 0);
if (--m_lock_depth == 0)
{
bool has_priority = tid ? !Thread::current().is_userspace() : true;
if (has_priority)
m_queue_length--;
m_locker = -1;
if (tid)
Thread::current().remove_mutex();
}
}
bool PriorityMutex::is_locked_by_current_thread() const
{
return m_locker == Thread::current_tid();
}
}
+58
View File
@@ -0,0 +1,58 @@
#include <kernel/Lock/BlockableSpinLock.h>
#include <kernel/Lock/RWLock.h>
#include <kernel/Thread.h>
namespace Kernel
{
void RWLock::rd_lock()
{
SpinLockGuard _(m_lock);
while (m_writers_waiting > 0 || m_writer != -1)
{
BlockableSpinLock block(m_lock);
m_thread_blocker.block_indefinite(&block);
}
m_readers_active++;
}
void RWLock::rd_unlock()
{
SpinLockGuard _(m_lock);
if (--m_readers_active == 0)
m_thread_blocker.unblock();
}
void RWLock::wr_lock()
{
if (m_writer == Thread::current_tid())
{
m_writer_depth++;
return;
}
SpinLockGuard _(m_lock);
m_writers_waiting++;
while (m_readers_active > 0 || m_writer != -1)
{
BlockableSpinLock block(m_lock);
m_thread_blocker.block_indefinite(&block);
}
m_writers_waiting--;
m_writer = Thread::current_tid();
m_writer_depth = 1;
}
void RWLock::wr_unlock()
{
if (--m_writer_depth != 0)
return;
SpinLockGuard _(m_lock);
m_writer = -1;
m_thread_blocker.unblock();
}
}