2023-02-01 01:53:35 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <BAN/LinkedList.h>
|
2023-03-07 19:17:49 +02:00
|
|
|
#include <BAN/Memory.h>
|
2023-03-30 18:45:47 +03:00
|
|
|
#include <kernel/Semaphore.h>
|
2023-02-01 01:53:35 +02:00
|
|
|
#include <kernel/Thread.h>
|
|
|
|
|
|
|
|
namespace Kernel
|
|
|
|
{
|
|
|
|
|
|
|
|
class Scheduler
|
|
|
|
{
|
|
|
|
public:
|
2023-03-07 19:17:49 +02:00
|
|
|
static BAN::ErrorOr<void> initialize();
|
2023-02-02 23:24:12 +02:00
|
|
|
static Scheduler& get();
|
|
|
|
|
|
|
|
void start();
|
2023-03-07 19:17:49 +02:00
|
|
|
void reschedule();
|
2023-04-03 01:51:05 +03:00
|
|
|
void reschedule_if_idling();
|
2023-02-02 23:24:12 +02:00
|
|
|
|
2023-03-30 19:13:28 +03:00
|
|
|
BAN::ErrorOr<void> add_thread(Thread*);
|
2023-03-07 19:17:49 +02:00
|
|
|
|
|
|
|
void set_current_thread_sleeping(uint64_t);
|
|
|
|
[[noreturn]] void set_current_thread_done();
|
2023-02-01 01:53:35 +02:00
|
|
|
|
2023-03-30 18:45:47 +03:00
|
|
|
void block_current_thread(Semaphore*);
|
|
|
|
void unblock_threads(Semaphore*);
|
|
|
|
|
2023-03-30 19:13:28 +03:00
|
|
|
Thread& current_thread();
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2023-02-01 01:53:35 +02:00
|
|
|
private:
|
2023-03-07 19:17:49 +02:00
|
|
|
Scheduler() = default;
|
|
|
|
|
|
|
|
void wake_threads();
|
|
|
|
[[nodiscard]] bool save_current_thread();
|
2023-03-08 03:23:46 +02:00
|
|
|
void remove_and_advance_current_thread();
|
|
|
|
void advance_current_thread();
|
2023-03-07 19:17:49 +02:00
|
|
|
[[noreturn]] void execute_current_thread();
|
2023-02-01 01:53:35 +02:00
|
|
|
|
|
|
|
private:
|
2023-03-07 19:17:49 +02:00
|
|
|
struct ActiveThread
|
|
|
|
{
|
2023-03-30 19:13:28 +03:00
|
|
|
ActiveThread(Thread* thread) : thread(thread) {}
|
|
|
|
Thread* thread;
|
|
|
|
uint64_t padding;
|
2023-03-07 19:17:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SleepingThread
|
|
|
|
{
|
2023-03-30 19:13:28 +03:00
|
|
|
SleepingThread(Thread* thread, uint64_t wake_time) : thread(thread), wake_time(wake_time) {}
|
|
|
|
Thread* thread;
|
2023-03-08 03:21:30 +02:00
|
|
|
uint64_t wake_time;
|
2023-03-07 19:17:49 +02:00
|
|
|
};
|
|
|
|
|
2023-03-30 18:45:47 +03:00
|
|
|
struct BlockingThread
|
|
|
|
{
|
2023-03-30 19:13:28 +03:00
|
|
|
BlockingThread(Thread* thread, Semaphore* semaphore) : thread(thread), semaphore(semaphore) {}
|
|
|
|
Thread* thread;
|
2023-03-30 18:45:47 +03:00
|
|
|
Semaphore* semaphore;
|
|
|
|
uint8_t padding[sizeof(uint64_t) - sizeof(Semaphore*)];
|
|
|
|
};
|
|
|
|
|
2023-03-30 19:13:28 +03:00
|
|
|
Thread* m_idle_thread { nullptr };
|
2023-03-07 19:17:49 +02:00
|
|
|
BAN::LinkedList<ActiveThread> m_active_threads;
|
|
|
|
BAN::LinkedList<SleepingThread> m_sleeping_threads;
|
2023-03-30 18:45:47 +03:00
|
|
|
BAN::LinkedList<BlockingThread> m_blocking_threads;
|
2023-03-07 19:17:49 +02:00
|
|
|
|
|
|
|
BAN::LinkedList<ActiveThread>::iterator m_current_thread;
|
2023-02-19 17:53:29 +02:00
|
|
|
|
2023-03-07 19:17:49 +02:00
|
|
|
uint64_t m_last_reschedule = 0;
|
2023-02-01 01:53:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|