Kernel: Implement somewhat functioning multithread support

This still uses only a single cpu, but we can now have 'parallelization'

This seems to work fine in qemu and bochs, but my own computer did not
like this when I last tried.

I have absolutely no idea how multithreading should actually be
implmemented and I just thought and implemented the most simple one I
could think of. This might not be in any way correct :D
This commit is contained in:
Bananymous
2023-02-01 01:53:35 +02:00
parent 7d8aafa0b5
commit 6a9d60a8fb
10 changed files with 320 additions and 18 deletions

View File

@@ -2,6 +2,8 @@
#include <stdint.h>
#define PIT_IRQ 0
namespace PIT
{

View File

@@ -0,0 +1,34 @@
#pragma once
#include <BAN/LinkedList.h>
#include <kernel/Thread.h>
namespace Kernel
{
class Scheduler
{
BAN_NON_COPYABLE(Scheduler);
BAN_NON_MOVABLE(Scheduler);
public:
static void Initialize();
static Scheduler& Get();
const Thread& CurrentThread() const;
void AddThread(void(*)());
void Switch();
void Start();
static constexpr size_t ms_between_switch = 4;
private:
Scheduler() {}
private:
BAN::LinkedList<Thread> m_threads;
BAN::LinkedList<Thread>::iterator m_current_iterator;
};
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include <kernel/TerminalDriver.h>
#include <kernel/SpinLock.h>
class TTY
{
@@ -51,4 +52,5 @@ private:
Cell* m_buffer { nullptr };
AnsiState m_ansi_state;
TerminalDriver* m_terminal_driver { nullptr };
Kernel::SpinLock m_lock;
};

View File

@@ -0,0 +1,47 @@
#pragma once
#include <BAN/Memory.h>
namespace Kernel
{
class Thread
{
BAN_NON_COPYABLE(Thread);
BAN_NON_MOVABLE(Thread);
public:
enum class State
{
NotStarted,
Running,
Paused,
Done,
};
public:
Thread(void(*)());
~Thread();
uint32_t id() const { return m_id; }
void set_rip(uintptr_t rip) { m_rip = rip; }
void set_rsp(uintptr_t rsp) { m_rsp = rsp; }
void set_state(State state) { m_state = state; }
uintptr_t rip() const { return m_rip; }
uintptr_t rsp() const { return m_rsp; }
State state() const { return m_state; }
private:
static void on_exit();
private:
void* m_stack_base = nullptr;
State m_state = State::NotStarted;
uintptr_t m_rip = 0;
uintptr_t m_rsp = 0;
const uint32_t m_id = 0;
};
}