2023-05-18 12:43:53 -07:00
|
|
|
#include "scheduler/scheduler.h"
|
|
|
|
|
2023-06-16 01:52:55 -07:00
|
|
|
#include "common/gdt.h"
|
2023-05-18 12:43:53 -07:00
|
|
|
#include "debug/debug.h"
|
2023-05-29 23:35:44 -07:00
|
|
|
#include "scheduler/process_manager.h"
|
2023-05-18 12:43:53 -07:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2023-05-18 13:24:02 -07:00
|
|
|
extern "C" void context_switch(uint64_t* current_esp, uint64_t* next_esp);
|
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
} // namespace
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
Scheduler* gScheduler = nullptr;
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
void Scheduler::Init() { gScheduler = new Scheduler(); }
|
|
|
|
Scheduler::Scheduler() {
|
|
|
|
Process& root = gProcMan->FromId(0);
|
|
|
|
sleep_thread_ = root.GetThread(0);
|
|
|
|
// TODO: Implement a separate sleep thread?
|
|
|
|
current_thread_ = sleep_thread_;
|
|
|
|
}
|
2023-05-18 13:24:02 -07:00
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
void Scheduler::SwapToCurrent(Thread& prev) {
|
|
|
|
if (current_thread_->GetState() != Thread::RUNNABLE) {
|
|
|
|
panic("Swapping to non-runnable thread.");
|
|
|
|
}
|
|
|
|
current_thread_->SetState(Thread::RUNNING);
|
2023-05-29 23:09:39 -07:00
|
|
|
|
2023-06-16 01:52:55 -07:00
|
|
|
SetRsp0(current_thread_->Rsp0Start());
|
2023-05-29 23:48:32 -07:00
|
|
|
context_switch(prev.Rsp0Ptr(), current_thread_->Rsp0Ptr());
|
2023-05-29 23:09:39 -07:00
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
asm volatile("sti");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::Preempt() {
|
|
|
|
if (!enabled_) {
|
|
|
|
return;
|
2023-05-29 23:09:39 -07:00
|
|
|
}
|
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
asm volatile("cli");
|
|
|
|
if (current_thread_ == sleep_thread_) {
|
|
|
|
// Sleep should never be preempted. (We should yield it if another thread
|
|
|
|
// becomes scheduleable).
|
2023-06-19 21:47:37 -07:00
|
|
|
// FIXME: We should yield these threads instead of preempting them.
|
|
|
|
if (runnable_threads_.size() > 0) {
|
|
|
|
current_thread_ = runnable_threads_.PopFront();
|
|
|
|
sleep_thread_->SetState(Thread::RUNNABLE);
|
|
|
|
SwapToCurrent(*sleep_thread_);
|
|
|
|
} else {
|
|
|
|
asm volatile("sti");
|
|
|
|
}
|
2023-05-29 23:48:32 -07:00
|
|
|
return;
|
|
|
|
}
|
2023-05-29 23:09:39 -07:00
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
if (runnable_threads_.size() == 0) {
|
|
|
|
// Continue.
|
2023-06-12 19:20:51 -07:00
|
|
|
asm volatile("sti");
|
2023-05-29 23:48:32 -07:00
|
|
|
return;
|
|
|
|
}
|
2023-05-29 23:09:39 -07:00
|
|
|
|
2023-06-21 15:07:40 -07:00
|
|
|
glcr::RefPtr<Thread> prev = current_thread_;
|
2023-05-29 23:48:32 -07:00
|
|
|
prev->SetState(Thread::RUNNABLE);
|
2023-06-21 16:28:42 -07:00
|
|
|
runnable_threads_.PushBack(prev);
|
|
|
|
current_thread_ = runnable_threads_.PopFront();
|
2023-05-29 23:09:39 -07:00
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
SwapToCurrent(*prev);
|
|
|
|
}
|
2023-05-29 23:09:39 -07:00
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
void Scheduler::Yield() {
|
|
|
|
if (!enabled_) {
|
|
|
|
dbgln("WARN Scheduler skipped yield.");
|
|
|
|
return;
|
2023-05-29 23:09:39 -07:00
|
|
|
}
|
2023-05-29 23:48:32 -07:00
|
|
|
asm volatile("cli");
|
2023-05-29 23:09:39 -07:00
|
|
|
|
2023-06-21 15:07:40 -07:00
|
|
|
glcr::RefPtr<Thread> prev = current_thread_;
|
2023-05-29 23:48:32 -07:00
|
|
|
if (prev == sleep_thread_) {
|
|
|
|
if (runnable_threads_.size() == 0) {
|
|
|
|
panic("Sleep thread yielded without next.");
|
2023-05-18 13:24:02 -07:00
|
|
|
return;
|
2023-05-29 23:48:32 -07:00
|
|
|
} else {
|
|
|
|
current_thread_ = runnable_threads_.PopFront();
|
|
|
|
prev->SetState(Thread::RUNNABLE);
|
2023-05-18 13:24:02 -07:00
|
|
|
}
|
2023-05-29 23:48:32 -07:00
|
|
|
} else {
|
|
|
|
if (runnable_threads_.size() == 0) {
|
|
|
|
current_thread_ = sleep_thread_;
|
|
|
|
dbgln("Sleeping");
|
2023-05-29 15:50:38 -07:00
|
|
|
} else {
|
2023-05-29 23:48:32 -07:00
|
|
|
current_thread_ = runnable_threads_.PopFront();
|
2023-05-18 13:56:54 -07:00
|
|
|
}
|
2023-05-18 13:24:02 -07:00
|
|
|
}
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-05-29 23:48:32 -07:00
|
|
|
SwapToCurrent(*prev);
|
2023-05-18 12:43:53 -07:00
|
|
|
}
|