Kernel: Initial work on new scheduler with queues

Sleeping is definately broken
This commit is contained in:
Bananymous
2023-03-07 19:17:49 +02:00
parent a9acf1f6dc
commit b8ee77eb78
8 changed files with 218 additions and 124 deletions

View File

@@ -0,0 +1,3 @@
#pragma once
#define ALWAYS_INLINE inline __attribute__((always_inline))

View File

@@ -1,7 +1,7 @@
#pragma once
#include <BAN/Function.h>
#include <BAN/LinkedList.h>
#include <BAN/Memory.h>
#include <kernel/Thread.h>
namespace Kernel
@@ -9,33 +9,48 @@ namespace Kernel
class Scheduler
{
BAN_NON_COPYABLE(Scheduler);
BAN_NON_MOVABLE(Scheduler);
public:
static void initialize();
static BAN::ErrorOr<void> initialize();
static Scheduler& get();
const Thread& current_thread() const;
BAN::ErrorOr<void> add_thread(const BAN::Function<void()>& function);
void reschedule();
void set_current_thread_sleeping();
void start();
void reschedule();
static constexpr size_t ms_between_switch = 4;
BAN::ErrorOr<void> add_thread(BAN::RefCounted<Thread>);
void set_current_thread_sleeping(uint64_t);
[[noreturn]] void set_current_thread_done();
private:
Scheduler() {}
void switch_thread();
Scheduler() = default;
BAN::RefCounted<Thread> current_thread();
void wake_threads();
[[nodiscard]] bool save_current_thread();
void get_next_thread();
[[noreturn]] void execute_current_thread();
private:
BAN::LinkedList<Thread> m_threads;
BAN::LinkedList<Thread>::iterator m_current_iterator;
uint64_t m_last_reschedule = 0;
struct ActiveThread
{
BAN::RefCounted<Thread> thread;
uint64_t padding;
};
friend class Thread;
struct SleepingThread
{
BAN::RefCounted<Thread> thread;
uint64_t wake_delta;
};
BAN::RefCounted<Thread> m_idle_thread;
BAN::LinkedList<ActiveThread> m_active_threads;
BAN::LinkedList<SleepingThread> m_sleeping_threads;
BAN::LinkedList<ActiveThread>::iterator m_current_thread;
uint64_t m_last_reschedule = 0;
};
}

View File

@@ -12,41 +12,35 @@ namespace Kernel
BAN_NON_MOVABLE(Thread);
public:
enum class State
{
NotStarted,
Running,
Paused,
Sleeping,
Done,
};
public:
Thread(const BAN::Function<void()>&);
static BAN::ErrorOr<BAN::RefCounted<Thread>> create(const BAN::Function<void()>&);
~Thread();
uint32_t tid() const { return m_tid; }
void set_rsp(uintptr_t rsp) { m_rsp = rsp; }
void set_rip(uintptr_t rip) { m_rip = rip; }
void set_state(State state) { m_state = state; }
uintptr_t rsp() const { return m_rsp; }
uintptr_t rip() const { return m_rip; }
State state() const { return m_state; }
void set_started() { m_started = true; }
bool started() const { return m_started; }
const BAN::Function<void()>* function() const { return &m_function; }
private:
Thread(const BAN::Function<void()>&);
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_tid = 0;
bool m_started = false;
BAN::Function<void()> m_function;
friend class BAN::RefCounted<Thread>;
};
}