banan-os/kernel/include/kernel/Scheduler.h

73 lines
1.6 KiB
C
Raw Normal View History

#pragma once
#include <BAN/LinkedList.h>
#include <BAN/Memory.h>
2023-03-30 18:45:47 +03:00
#include <kernel/Semaphore.h>
#include <kernel/Thread.h>
namespace Kernel
{
class Scheduler
{
public:
static BAN::ErrorOr<void> initialize();
static Scheduler& get();
void start();
void reschedule();
void reschedule_if_idling();
BAN::ErrorOr<void> add_thread(Thread*);
void set_current_thread_sleeping(uint64_t);
[[noreturn]] void set_current_thread_done();
2023-03-30 18:45:47 +03:00
void block_current_thread(Semaphore*);
void unblock_threads(Semaphore*);
Thread& current_thread();
private:
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();
[[noreturn]] void execute_current_thread();
private:
struct ActiveThread
{
ActiveThread(Thread* thread) : thread(thread) {}
Thread* thread;
uint64_t padding;
};
struct SleepingThread
{
SleepingThread(Thread* thread, uint64_t wake_time) : thread(thread), wake_time(wake_time) {}
Thread* thread;
uint64_t wake_time;
};
2023-03-30 18:45:47 +03:00
struct BlockingThread
{
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*)];
};
Thread* m_idle_thread { nullptr };
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;
BAN::LinkedList<ActiveThread>::iterator m_current_thread;
uint64_t m_last_reschedule = 0;
};
}