Split Yield and Preempt into separate scheduling functions.
This switch makes the logic for each much easier to parse.
This commit is contained in:
parent
3fee5ac9d7
commit
b58186265e
3 changed files with 54 additions and 31 deletions
|
|
@ -34,55 +34,70 @@ class Scheduler {
|
|||
void InsertProcess(Process* process) { proc_list_.PushBack(process); }
|
||||
void Enqueue(Thread* thread) { runnable_threads_.PushBack(thread); }
|
||||
|
||||
void SwapToCurrent(Thread& prev) {
|
||||
if (current_thread_->GetState() != Thread::RUNNABLE) {
|
||||
panic("Swapping to non-runnable thread.");
|
||||
}
|
||||
current_thread_->SetState(Thread::RUNNING);
|
||||
|
||||
context_switch(prev.Rsp0Ptr(), current_thread_->Rsp0Ptr());
|
||||
|
||||
asm volatile("sti");
|
||||
}
|
||||
|
||||
void Preempt() {
|
||||
if (!enabled_) {
|
||||
return;
|
||||
}
|
||||
|
||||
asm volatile("cli");
|
||||
if (current_thread_ == sleep_thread_) {
|
||||
// Sleep should never be preempted. (We should yield it if another thread
|
||||
// becomes scheduleable).
|
||||
return;
|
||||
}
|
||||
|
||||
if (runnable_threads_.size() == 0) {
|
||||
// Continue.
|
||||
return;
|
||||
}
|
||||
|
||||
SharedPtr<Thread> prev = current_thread_;
|
||||
prev->SetState(Thread::RUNNABLE);
|
||||
current_thread_ = runnable_threads_.CycleFront(prev);
|
||||
|
||||
SwapToCurrent(*prev);
|
||||
}
|
||||
|
||||
void Yield() {
|
||||
if (!enabled_) {
|
||||
dbgln("WARN Scheduler skipped yield.");
|
||||
return;
|
||||
}
|
||||
asm volatile("cli");
|
||||
|
||||
SharedPtr<Thread> prev = current_thread_;
|
||||
SharedPtr<Thread> next;
|
||||
if (prev == sleep_thread_) {
|
||||
if (runnable_threads_.size() == 0) {
|
||||
// Continue sleeping.
|
||||
panic("Sleep thread yielded without next.");
|
||||
return;
|
||||
} else {
|
||||
// FIXME: Memory operation.
|
||||
next = runnable_threads_.PopFront();
|
||||
current_thread_ = runnable_threads_.PopFront();
|
||||
prev->SetState(Thread::RUNNABLE);
|
||||
}
|
||||
} else {
|
||||
// Normal thread running.
|
||||
if (prev->GetState() == Thread::RUNNING) {
|
||||
if (runnable_threads_.size() == 0) {
|
||||
// This thread can continue.
|
||||
return;
|
||||
}
|
||||
prev->SetState(Thread::RUNNABLE);
|
||||
next = runnable_threads_.CycleFront(prev);
|
||||
if (runnable_threads_.size() == 0) {
|
||||
current_thread_ = sleep_thread_;
|
||||
dbgln("Sleeping");
|
||||
DumpProcessStates(proc_list_);
|
||||
} else {
|
||||
// Thread blocked/exited.
|
||||
if (runnable_threads_.size() == 0) {
|
||||
next = sleep_thread_;
|
||||
dbgln("Sleeping");
|
||||
DumpProcessStates(proc_list_);
|
||||
} else {
|
||||
// FIXME: Memory operation.
|
||||
next = runnable_threads_.PopFront();
|
||||
}
|
||||
// FIXME: Memory operation.
|
||||
current_thread_ = runnable_threads_.PopFront();
|
||||
}
|
||||
}
|
||||
|
||||
if (next->GetState() != Thread::RUNNABLE) {
|
||||
panic("Non-runnable thread in the queue");
|
||||
}
|
||||
|
||||
next->SetState(Thread::RUNNING);
|
||||
current_thread_ = next;
|
||||
|
||||
context_switch(prev->Rsp0Ptr(), next->Rsp0Ptr());
|
||||
|
||||
asm volatile("sti");
|
||||
SwapToCurrent(*prev);
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
@ -110,6 +125,7 @@ Scheduler& GetScheduler() {
|
|||
void InitScheduler() { gScheduler = new Scheduler(); }
|
||||
void EnableScheduler() { GetScheduler().Enable(); }
|
||||
|
||||
void Preempt() { GetScheduler().Preempt(); }
|
||||
void Yield() { GetScheduler().Yield(); }
|
||||
|
||||
void InsertProcess(Process* process) { GetScheduler().InsertProcess(process); }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue