From 78624d529121083032eb18bdeef1005dfb82f732 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Thu, 2 Nov 2023 22:28:08 -0700 Subject: [PATCH] [Zion] Immediately schedule enqueued threads if we are sleeping. --- zion/scheduler/scheduler.cpp | 16 +++++++--------- zion/scheduler/scheduler.h | 4 +--- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/zion/scheduler/scheduler.cpp b/zion/scheduler/scheduler.cpp index e19bb17..b498b0e 100644 --- a/zion/scheduler/scheduler.cpp +++ b/zion/scheduler/scheduler.cpp @@ -16,7 +16,6 @@ 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_; } @@ -32,6 +31,13 @@ void Scheduler::SwapToCurrent(Thread& prev) { asm volatile("sti"); } +void Scheduler::Enqueue(const glcr::RefPtr& thread) { + runnable_threads_.PushBack(thread); + if (current_thread_ == sleep_thread_) { + Yield(); + } +} + void Scheduler::Preempt() { if (!enabled_) { return; @@ -41,14 +47,6 @@ void Scheduler::Preempt() { if (current_thread_ == sleep_thread_) { // Sleep should never be preempted. (We should yield it if another thread // becomes scheduleable). - // 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"); - } return; } diff --git a/zion/scheduler/scheduler.h b/zion/scheduler/scheduler.h index ed2f0ca..5e9ee6c 100644 --- a/zion/scheduler/scheduler.h +++ b/zion/scheduler/scheduler.h @@ -18,9 +18,7 @@ class Scheduler { Process& CurrentProcess() { return current_thread_->process(); } glcr::RefPtr CurrentThread() { return current_thread_; } - void Enqueue(const glcr::RefPtr& thread) { - runnable_threads_.PushBack(thread); - } + void Enqueue(const glcr::RefPtr& thread); void Preempt(); void Yield();