2023-02-01 01:53:35 +02:00
|
|
|
#pragma once
|
|
|
|
|
2024-03-08 22:13:45 +02:00
|
|
|
#include <kernel/SchedulerQueue.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();
|
|
|
|
|
2023-04-11 23:33:20 +03:00
|
|
|
[[noreturn]] void start();
|
2023-06-09 00:47:17 +03:00
|
|
|
|
2024-03-29 18:02:12 +02:00
|
|
|
void yield();
|
|
|
|
|
2023-06-09 00:41:43 +03:00
|
|
|
void timer_reschedule();
|
2024-03-29 18:02:12 +02:00
|
|
|
void irq_reschedule();
|
2023-04-03 01:51:05 +03:00
|
|
|
void reschedule_if_idling();
|
2023-02-02 23:24:12 +02:00
|
|
|
|
2024-02-09 15:18:34 +02:00
|
|
|
void set_current_thread_sleeping(uint64_t wake_time);
|
2023-04-12 17:49:04 +03:00
|
|
|
|
2024-02-09 15:18:34 +02:00
|
|
|
void block_current_thread(Semaphore*, uint64_t wake_time);
|
2023-03-30 18:45:47 +03:00
|
|
|
void unblock_threads(Semaphore*);
|
2023-12-06 16:12:37 +02:00
|
|
|
// Makes sleeping or blocked thread with tid active.
|
2023-07-24 22:24:21 +03:00
|
|
|
void unblock_thread(pid_t tid);
|
2023-03-30 18:45:47 +03:00
|
|
|
|
2023-03-30 19:13:28 +03:00
|
|
|
Thread& current_thread();
|
2023-05-28 16:18:18 +03:00
|
|
|
static pid_t current_tid();
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2024-03-01 15:49:39 +02:00
|
|
|
// This is no return if called on current thread
|
|
|
|
void terminate_thread(Thread*);
|
|
|
|
|
2023-02-01 01:53:35 +02:00
|
|
|
private:
|
2023-03-07 19:17:49 +02:00
|
|
|
Scheduler() = default;
|
|
|
|
|
2024-03-08 23:24:18 +02:00
|
|
|
void set_current_thread_sleeping_impl(Semaphore* semaphore, uint64_t wake_time);
|
2024-02-09 15:18:34 +02:00
|
|
|
|
2024-03-29 18:02:12 +02:00
|
|
|
void setup_next_thread();
|
2024-03-01 15:49:39 +02:00
|
|
|
|
2023-04-19 00:39:06 +03:00
|
|
|
BAN::ErrorOr<void> add_thread(Thread*);
|
|
|
|
|
2023-02-01 01:53:35 +02:00
|
|
|
private:
|
2024-03-04 22:36:41 +02:00
|
|
|
SpinLock m_lock;
|
2024-03-01 15:49:39 +02:00
|
|
|
|
2024-03-08 22:13:45 +02:00
|
|
|
SchedulerQueue m_active_threads;
|
|
|
|
SchedulerQueue m_blocking_threads;
|
2023-03-07 19:17:49 +02:00
|
|
|
|
2023-04-19 00:39:06 +03:00
|
|
|
friend class Process;
|
2023-02-01 01:53:35 +02:00
|
|
|
};
|
|
|
|
|
2024-01-24 14:43:46 +02:00
|
|
|
}
|