2023-05-18 12:43:53 -07:00
|
|
|
#pragma once
|
|
|
|
|
2023-06-06 20:18:53 -07:00
|
|
|
#include "object/process.h"
|
|
|
|
#include "object/thread.h"
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
class Scheduler {
|
|
|
|
public:
|
|
|
|
// Initializes the scheduler in a disabled state.
|
|
|
|
//
|
|
|
|
// Requires the process manager to have been initialized.
|
|
|
|
static void Init();
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
void Enable() { enabled_ = true; }
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
Process& CurrentProcess() { return current_thread_->process(); }
|
|
|
|
Thread& CurrentThread() { return *current_thread_; }
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-06-06 19:05:03 -07:00
|
|
|
void Enqueue(const RefPtr<Thread>& thread) {
|
2023-05-29 23:48:32 -07:00
|
|
|
runnable_threads_.PushBack(thread);
|
|
|
|
}
|
2023-05-29 23:09:39 -07:00
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
void Preempt();
|
|
|
|
void Yield();
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
private:
|
|
|
|
bool enabled_ = false;
|
2023-05-18 13:24:02 -07:00
|
|
|
|
2023-06-06 19:05:03 -07:00
|
|
|
RefPtr<Thread> current_thread_;
|
|
|
|
LinkedList<RefPtr<Thread>> runnable_threads_;
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-06-06 19:05:03 -07:00
|
|
|
RefPtr<Thread> sleep_thread_;
|
2023-05-29 23:48:32 -07:00
|
|
|
|
|
|
|
Scheduler();
|
|
|
|
void SwapToCurrent(Thread& prev);
|
|
|
|
};
|
|
|
|
|
|
|
|
extern Scheduler* gScheduler;
|