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
+1
View File
@@ -4,6 +4,7 @@
#include <kernel/ACPI/AML/Namespace.h>
#include <kernel/ACPI/EmbeddedController.h>
#include <kernel/ACPI/Headers.h>
#include <kernel/Interruptable.h>
#include <kernel/Memory/Types.h>
#include <kernel/ThreadBlocker.h>
@@ -1,11 +1,12 @@
#pragma once
#include <BAN/Atomic.h>
#include <BAN/Optional.h>
#include <BAN/UniqPtr.h>
#include <kernel/ACPI/AML/Scope.h>
#include <kernel/Lock/Mutex.h>
#include <kernel/Thread.h>
#include <kernel/ThreadBlocker.h>
namespace Kernel::ACPI
{
+1
View File
@@ -2,6 +2,7 @@
#include <BAN/Vector.h>
#include <kernel/InterruptController.h>
#include <kernel/InterruptNumbers.h>
#include <kernel/Lock/SpinLock.h>
#include <kernel/Memory/Types.h>
@@ -1,5 +1,6 @@
#pragma once
#include <BAN/Optional.h>
#include <BAN/Vector.h>
namespace Kernel::HDAudio
+1
View File
@@ -3,6 +3,7 @@
#include <BAN/HashMap.h>
#include <kernel/FS/Inode.h>
#include <kernel/Lock/Mutex.h>
#include <kernel/ThreadBlocker.h>
#include <sys/epoll.h>
+2 -1
View File
@@ -1,7 +1,6 @@
#pragma once
#include <BAN/Vector.h>
#include <kernel/Device/Device.h>
#include <kernel/FS/TmpFS/FileSystem.h>
#include <kernel/Lock/Mutex.h>
#include <kernel/ThreadBlocker.h>
@@ -9,6 +8,8 @@
namespace Kernel
{
class Device;
class DevFileSystem final : public TmpFileSystem
{
public:
+1
View File
@@ -2,6 +2,7 @@
#include <kernel/FS/Inode.h>
#include <kernel/Lock/Mutex.h>
#include <kernel/ThreadBlocker.h>
namespace Kernel
{
+2 -1
View File
@@ -2,8 +2,9 @@
#include <BAN/HashMap.h>
#include <kernel/Device/Device.h>
#include <kernel/FS/FileSystem.h>
#include <kernel/FS/Ext2/Inode.h>
#include <kernel/FS/FileSystem.h>
#include <kernel/Memory/VirtualRange.h>
namespace Kernel
{
@@ -5,6 +5,7 @@
#include <kernel/FS/FAT/Definitions.h>
#include <kernel/FS/FAT/Inode.h>
#include <kernel/FS/FileSystem.h>
#include <kernel/Lock/Mutex.h>
namespace Kernel
{
+2 -1
View File
@@ -1,11 +1,12 @@
#pragma once
#include <kernel/Device/Device.h>
#include <kernel/FS/Inode.h>
namespace Kernel
{
class BlockDevice;
class FileSystem : public BAN::RefCounted<FileSystem>
{
public:
+2 -1
View File
@@ -2,11 +2,12 @@
#include <kernel/FS/TmpFS/FileSystem.h>
#include <kernel/FS/TmpFS/Inode.h>
#include <kernel/Process.h>
namespace Kernel
{
class Process;
class ProcFileSystem final : public TmpFileSystem
{
public:
+2 -1
View File
@@ -2,11 +2,12 @@
#include <kernel/FS/TmpFS/FileSystem.h>
#include <kernel/FS/TmpFS/Inode.h>
#include <kernel/Process.h>
namespace Kernel
{
class Process;
class ProcPidInode final : public TmpDirectoryInode
{
// FIXME: dynamically update ruid/rgid.
-15
View File
@@ -10,21 +10,6 @@
namespace Kernel
{
// IDT entries
// 0x00->0x1F (32): ISR
// 0x20->0x7F (96): PIC/IOAPIC
// 0x80->0xEF (112): MSI
// 0xF0->0xFE (15): internal
constexpr uint8_t IRQ_VECTOR_BASE = 0x20;
constexpr uint8_t IRQ_MSI_BASE = 0x80;
constexpr uint8_t IRQ_MSI_END = 0xF0;
#if ARCH(i686)
constexpr uint8_t IRQ_SYSCALL = 0xF0; // hard coded in kernel/API/Syscall.h
#endif
constexpr uint8_t IRQ_IPI = 0xF1;
constexpr uint8_t IRQ_TIMER = 0xF2;
#if ARCH(x86_64)
struct GateDescriptor
{
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include <kernel/Arch.h>
namespace Kernel
{
// IDT entries
// 0x00->0x1F (32): ISR
// 0x20->0x7F (96): PIC/IOAPIC
// 0x80->0xEF (112): MSI
// 0xF0->0xFE (15): internal
constexpr uint8_t IRQ_VECTOR_BASE = 0x20;
constexpr uint8_t IRQ_MSI_BASE = 0x80;
constexpr uint8_t IRQ_MSI_END = 0xF0;
#if ARCH(i686)
constexpr uint8_t IRQ_SYSCALL = 0xF0; // hard coded in kernel/API/Syscall.h
#endif
constexpr uint8_t IRQ_IPI = 0xF1;
constexpr uint8_t IRQ_TIMER = 0xF2;
}
+8 -118
View File
@@ -2,7 +2,6 @@
#include <BAN/Atomic.h>
#include <BAN/NoCopyMove.h>
#include <kernel/Thread.h>
#include <sys/types.h>
@@ -27,63 +26,14 @@ namespace Kernel
public:
Mutex() = default;
void lock() override
{
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++;
}
bool 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 unlock() override
{
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 try_lock();
void lock() override;
void unlock() override;
pid_t locker() const { return m_locker; }
bool is_locked() const { return m_locker != -1; }
uint32_t lock_depth() const override { return m_lock_depth; }
bool is_locked_by_current_thread() const override { return m_locker == Thread::current_tid(); }
bool is_locked_by_current_thread() const override;
private:
BAN::Atomic<pid_t> m_locker { -1 };
@@ -98,74 +48,14 @@ namespace Kernel
public:
PriorityMutex() = default;
void lock() override
{
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++;
}
bool 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 unlock() override
{
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 try_lock();
void lock() override;
void unlock() override;
pid_t locker() const { return m_locker; }
bool is_locked() const { return m_locker != -1; }
uint32_t lock_depth() const override { return m_lock_depth; }
bool is_locked_by_current_thread() const override { return m_locker == Thread::current_tid(); }
bool is_locked_by_current_thread() const override;
private:
BAN::Atomic<pid_t> m_locker { -1 };
+5 -49
View File
@@ -1,7 +1,7 @@
#pragma once
#include <kernel/Lock/BlockableSpinLock.h>
#include <kernel/Lock/SpinLock.h>
#include <kernel/ThreadBlocker.h>
namespace Kernel
{
@@ -13,54 +13,10 @@ namespace Kernel
public:
RWLock() = default;
void 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 rd_unlock()
{
SpinLockGuard _(m_lock);
if (--m_readers_active == 0)
m_thread_blocker.unblock();
}
void 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 wr_unlock()
{
if (--m_writer_depth != 0)
return;
SpinLockGuard _(m_lock);
m_writer = -1;
m_thread_blocker.unblock();
}
void rd_lock();
void rd_unlock();
void wr_lock();
void wr_unlock();
private:
SpinLock m_lock;
@@ -1,5 +1,6 @@
#pragma once
#include <BAN/Vector.h>
#include <kernel/Memory/MemoryRegion.h>
namespace Kernel
@@ -1,7 +1,9 @@
#pragma once
#include <BAN/HashMap.h>
#include <BAN/RefPtr.h>
#include <BAN/UniqPtr.h>
#include <BAN/Vector.h>
#include <kernel/Lock/Mutex.h>
#include <kernel/Lock/SpinLock.h>
#include <kernel/Memory/MemoryRegion.h>
@@ -4,8 +4,6 @@
#include <BAN/HashMap.h>
#include <BAN/UniqPtr.h>
#include <kernel/Networking/NetworkInterface.h>
#include <kernel/Thread.h>
#include <kernel/ThreadBlocker.h>
namespace Kernel
{
@@ -11,7 +11,6 @@
#include <kernel/Networking/NetworkInterface.h>
#include <kernel/Networking/NetworkLayer.h>
#include <kernel/Networking/NetworkSocket.h>
#include <kernel/Thread.h>
namespace Kernel
{
@@ -1,6 +1,7 @@
#pragma once
#include <kernel/Networking/NetworkInterface.h>
#include <kernel/Memory/VirtualRange.h>
namespace Kernel
{
@@ -7,7 +7,6 @@
#include <kernel/Memory/ByteRingBuffer.h>
#include <kernel/Networking/NetworkInterface.h>
#include <kernel/Networking/NetworkSocket.h>
#include <kernel/Thread.h>
#include <kernel/ThreadBlocker.h>
namespace Kernel
@@ -6,6 +6,7 @@
#include <kernel/FS/Socket.h>
#include <kernel/FS/TmpFS/Inode.h>
#include <kernel/FS/VirtualFileSystem.h>
#include <kernel/Memory/VirtualRange.h>
#include <kernel/OpenFileDescriptorSet.h>
namespace Kernel
+2
View File
@@ -3,6 +3,8 @@
#include <BAN/UniqPtr.h>
#include <BAN/Vector.h>
#include <kernel/ACPI/AML/Node.h>
#include <kernel/InterruptNumbers.h>
#include <kernel/Interruptable.h>
#include <kernel/Memory/Types.h>
#include <sys/types.h>
+1 -2
View File
@@ -19,6 +19,7 @@
#include <poll.h>
#include <sys/banan-os.h>
#include <sys/epoll.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/socket.h>
@@ -26,8 +27,6 @@
#include <sys/time.h>
#include <termios.h>
struct epoll_event;
namespace Kernel
{
+10 -7
View File
@@ -1,17 +1,14 @@
#pragma once
#include <BAN/Array.h>
#include <BAN/Atomic.h>
#include <BAN/ForwardList.h>
#include <BAN/NoCopyMove.h>
#include <BAN/Math.h>
#include <kernel/API/SharedPage.h>
#include <kernel/Arch.h>
#include <kernel/GDT.h>
#include <kernel/IDT.h>
#include <kernel/InterruptStack.h>
#include <kernel/Memory/Types.h>
#include <kernel/ProcessorID.h>
#include <kernel/Scheduler.h>
namespace Kernel
{
@@ -22,6 +19,12 @@ namespace Kernel
Enabled,
};
class GDT;
class IDT;
class Scheduler;
class SchedulerQueueNode;
class Thread;
#if ARCH(x86_64) || ARCH(i686)
class Processor
{
@@ -51,8 +54,8 @@ namespace Kernel
union
{
TLBEntry flush_tlb;
SchedulerQueue::Node* new_thread;
SchedulerQueue::Node* unblock_thread;
SchedulerQueueNode* new_thread;
SchedulerQueueNode* unblock_thread;
bool dummy;
};
};
+1 -1
View File
@@ -1,7 +1,7 @@
#pragma once
#include <BAN/Atomic.h>
#include <kernel/ProcessorID.h>
#include <kernel/Lock/SpinLock.h>
namespace Kernel
{
@@ -1,6 +1,7 @@
#pragma once
#include <BAN/WeakPtr.h>
#include <kernel/Memory/VirtualRange.h>
#include <kernel/Terminal/TTY.h>
namespace Kernel
+3 -2
View File
@@ -5,8 +5,8 @@
#include <BAN/RefPtr.h>
#include <BAN/UniqPtr.h>
#include <kernel/InterruptStack.h>
#include <kernel/Lock/Mutex.h>
#include <kernel/Memory/VirtualRange.h>
#include <kernel/ThreadBlocker.h>
#include <LibELF/AuxiliaryVector.h>
@@ -18,6 +18,7 @@ namespace Kernel
class MemoryBackedRegion;
class Process;
class ThreadBlocker;
class Thread
{
@@ -191,7 +192,7 @@ namespace Kernel
vaddr_t m_fsbase { 0 };
vaddr_t m_gsbase { 0 };
SchedulerQueue::Node* m_scheduler_node { nullptr };
SchedulerQueueNode* m_scheduler_node { nullptr };
YieldRegisters m_yield_registers { };
+6 -4
View File
@@ -1,12 +1,14 @@
#pragma once
#include <BAN/Math.h>
#include <kernel/Lock/Mutex.h>
#include <kernel/Lock/SpinLock.h>
#include <kernel/Scheduler.h>
namespace Kernel
{
class SchedulerQueueNode;
class ThreadBlocker
{
public:
@@ -27,11 +29,11 @@ namespace Kernel
}
private:
void add_thread_to_block_queue(SchedulerQueue::Node*);
void remove_thread_from_block_queue(SchedulerQueue::Node*);
void add_thread_to_block_queue(SchedulerQueueNode*);
void remove_thread_from_block_queue(SchedulerQueueNode*);
private:
SchedulerQueue::Node* m_block_chain { nullptr };
SchedulerQueueNode* m_block_chain { nullptr };
SpinLock m_lock;
friend class Scheduler;
@@ -1,6 +1,5 @@
#pragma once
#include <kernel/Process.h>
#include <kernel/USB/Device.h>
namespace Kernel