Properly handle current thread in rescheduling
This commit is contained in:
@@ -85,29 +85,21 @@ std::optional<int> Kernel::getNextThread() {
|
|||||||
// See if there is a higher priority, ready thread and switch to that
|
// See if there is a higher priority, ready thread and switch to that
|
||||||
void Kernel::rescheduleThreads() {
|
void Kernel::rescheduleThreads() {
|
||||||
Thread& current = threads[currentThreadIndex]; // Current running thread
|
Thread& current = threads[currentThreadIndex]; // Current running thread
|
||||||
ThreadStatus currentStatus = current.status; // Its status
|
|
||||||
|
|
||||||
// If the current thread is running and hasn't gone to sleep or whatever, set it to Ready instead of Running
|
// If the current thread is running and hasn't gone to sleep or whatever, set it to Ready instead of Running
|
||||||
// Since rescheduleThreads will put it to wait and run another thread in the meantime
|
// So that getNextThread will evaluate it properly
|
||||||
if (currentStatus == ThreadStatus::Running) {
|
if (current.status == ThreadStatus::Running) {
|
||||||
currentStatus = ThreadStatus::Ready;
|
current.status = ThreadStatus::Ready;
|
||||||
}
|
}
|
||||||
|
ThreadStatus currentStatus = current.status;
|
||||||
current.status = ThreadStatus::Dead; // Temporarily mark it as dead so getNextThread will ignore it
|
|
||||||
std::optional<int> newThreadIndex = getNextThread();
|
std::optional<int> newThreadIndex = getNextThread();
|
||||||
current.status = currentStatus; // Restore old status
|
|
||||||
|
|
||||||
// Case 1: Another thread can run (that is not the idle thread)
|
// Case 1: A thread can run
|
||||||
if (newThreadIndex.has_value() && newThreadIndex.value() != idleThreadIndex) {
|
if (newThreadIndex.has_value()) {
|
||||||
switchThread(newThreadIndex.value());
|
switchThread(newThreadIndex.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 2: No other thread can run but this one can
|
// Case 2: No other thread can run, straight to the idle thread
|
||||||
else if (currentStatus == ThreadStatus::Running) {
|
|
||||||
switchThread(currentThreadIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Case 3: No thread can run other than the idle thread
|
|
||||||
else {
|
else {
|
||||||
switchThread(idleThreadIndex);
|
switchThread(idleThreadIndex);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user