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
|
@ -123,7 +123,7 @@ extern "C" void interrupt_timer(InterruptFrame*) {
|
||||||
dbgln("timer: %us", cnt * 50 / 1000);
|
dbgln("timer: %us", cnt * 50 / 1000);
|
||||||
}
|
}
|
||||||
outb(PIC1_COMMAND, PIC_EOI);
|
outb(PIC1_COMMAND, PIC_EOI);
|
||||||
sched::Yield();
|
sched::Preempt();
|
||||||
}
|
}
|
||||||
|
|
||||||
void EnablePic() {
|
void EnablePic() {
|
||||||
|
|
|
@ -34,55 +34,70 @@ class Scheduler {
|
||||||
void InsertProcess(Process* process) { proc_list_.PushBack(process); }
|
void InsertProcess(Process* process) { proc_list_.PushBack(process); }
|
||||||
void Enqueue(Thread* thread) { runnable_threads_.PushBack(thread); }
|
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() {
|
void Yield() {
|
||||||
if (!enabled_) {
|
if (!enabled_) {
|
||||||
|
dbgln("WARN Scheduler skipped yield.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
asm volatile("cli");
|
asm volatile("cli");
|
||||||
|
|
||||||
SharedPtr<Thread> prev = current_thread_;
|
SharedPtr<Thread> prev = current_thread_;
|
||||||
SharedPtr<Thread> next;
|
|
||||||
if (prev == sleep_thread_) {
|
if (prev == sleep_thread_) {
|
||||||
if (runnable_threads_.size() == 0) {
|
if (runnable_threads_.size() == 0) {
|
||||||
// Continue sleeping.
|
panic("Sleep thread yielded without next.");
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
// FIXME: Memory operation.
|
// FIXME: Memory operation.
|
||||||
next = runnable_threads_.PopFront();
|
current_thread_ = runnable_threads_.PopFront();
|
||||||
prev->SetState(Thread::RUNNABLE);
|
prev->SetState(Thread::RUNNABLE);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Normal thread running.
|
if (runnable_threads_.size() == 0) {
|
||||||
if (prev->GetState() == Thread::RUNNING) {
|
current_thread_ = sleep_thread_;
|
||||||
if (runnable_threads_.size() == 0) {
|
dbgln("Sleeping");
|
||||||
// This thread can continue.
|
DumpProcessStates(proc_list_);
|
||||||
return;
|
|
||||||
}
|
|
||||||
prev->SetState(Thread::RUNNABLE);
|
|
||||||
next = runnable_threads_.CycleFront(prev);
|
|
||||||
} else {
|
} else {
|
||||||
// Thread blocked/exited.
|
// FIXME: Memory operation.
|
||||||
if (runnable_threads_.size() == 0) {
|
current_thread_ = runnable_threads_.PopFront();
|
||||||
next = sleep_thread_;
|
|
||||||
dbgln("Sleeping");
|
|
||||||
DumpProcessStates(proc_list_);
|
|
||||||
} else {
|
|
||||||
// FIXME: Memory operation.
|
|
||||||
next = runnable_threads_.PopFront();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (next->GetState() != Thread::RUNNABLE) {
|
SwapToCurrent(*prev);
|
||||||
panic("Non-runnable thread in the queue");
|
|
||||||
}
|
|
||||||
|
|
||||||
next->SetState(Thread::RUNNING);
|
|
||||||
current_thread_ = next;
|
|
||||||
|
|
||||||
context_switch(prev->Rsp0Ptr(), next->Rsp0Ptr());
|
|
||||||
|
|
||||||
asm volatile("sti");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -110,6 +125,7 @@ Scheduler& GetScheduler() {
|
||||||
void InitScheduler() { gScheduler = new Scheduler(); }
|
void InitScheduler() { gScheduler = new Scheduler(); }
|
||||||
void EnableScheduler() { GetScheduler().Enable(); }
|
void EnableScheduler() { GetScheduler().Enable(); }
|
||||||
|
|
||||||
|
void Preempt() { GetScheduler().Preempt(); }
|
||||||
void Yield() { GetScheduler().Yield(); }
|
void Yield() { GetScheduler().Yield(); }
|
||||||
|
|
||||||
void InsertProcess(Process* process) { GetScheduler().InsertProcess(process); }
|
void InsertProcess(Process* process) { GetScheduler().InsertProcess(process); }
|
||||||
|
|
|
@ -12,6 +12,13 @@ void InitScheduler();
|
||||||
// Enables the scheduler such that processes will yield on ticks.
|
// Enables the scheduler such that processes will yield on ticks.
|
||||||
void EnableScheduler();
|
void EnableScheduler();
|
||||||
|
|
||||||
|
// Preempts the current thread and flags it as runnable in the queue.
|
||||||
|
// Generally used by the timer to move to the next timeslice.
|
||||||
|
void Preempt();
|
||||||
|
|
||||||
|
// Current thread yields and is not rescheduled until some external process
|
||||||
|
// adds it.
|
||||||
|
// Used when a thread blocks or exits.
|
||||||
void Yield();
|
void Yield();
|
||||||
|
|
||||||
// Scheduler will take ownership
|
// Scheduler will take ownership
|
||||||
|
|
Loading…
Reference in New Issue