This commit is contained in:
2024-12-30 05:59:42 +08:00
parent bf78b95c9d
commit 72320ede22
42 changed files with 1758 additions and 1556 deletions

View File

@@ -22,6 +22,7 @@
#include "debug.h"
#include "scheduler.h"
#include "main.h"
#include <functional>
//----------------------------------------------------------------------
// Scheduler::Scheduler
@@ -29,11 +30,22 @@
// Initially, no ready threads.
//----------------------------------------------------------------------
// Todo ----
int cmp(Thread *a, Thread *b)
{
int ap = a->getPriority();
int bp = b->getPriority();
return (ap < bp) - (ap > bp);
}
Scheduler::Scheduler()
{
readyList = new List<Thread *>;
toBeDestroyed = NULL;
}
{
readyList = new SortedList(cmp);
// ---------
toBeDestroyed = NULL;
}
//----------------------------------------------------------------------
// Scheduler::~Scheduler
@@ -41,9 +53,9 @@ Scheduler::Scheduler()
//----------------------------------------------------------------------
Scheduler::~Scheduler()
{
delete readyList;
}
{
delete readyList;
}
//----------------------------------------------------------------------
// Scheduler::ReadyToRun
@@ -54,13 +66,15 @@ Scheduler::~Scheduler()
//----------------------------------------------------------------------
void
Scheduler::ReadyToRun (Thread *thread)
Scheduler::ReadyToRun(Thread* thread)
{
ASSERT(kernel->interrupt->getLevel() == IntOff);
DEBUG(dbgThread, "Putting thread on ready list: " << thread->getName());
//cout << "Putting thread on ready list: " << thread->getName() << endl ;
thread->setStatus(READY);
readyList->Append(thread);
ASSERT(kernel->interrupt->getLevel() == IntOff);
DEBUG(dbgThread, "Putting thread on ready list: " << thread->getName());
//cout << "Putting thread on ready list: " << thread->getName() << endl ;
thread->setStatus(READY);
DEBUG(dbgSche, "[A] Tick [" << kernel->stats->totalTicks << "]: Process [" << thread->getName() << "] is inserted into queue.");
readyList->Insert(thread);
}
//----------------------------------------------------------------------
@@ -71,14 +85,16 @@ Scheduler::ReadyToRun (Thread *thread)
// Thread is removed from the ready list.
//----------------------------------------------------------------------
Thread *
Scheduler::FindNextToRun ()
Thread*
Scheduler::FindNextToRun()
{
ASSERT(kernel->interrupt->getLevel() == IntOff);
if (readyList->IsEmpty()) {
return NULL;
} else {
}
else {
DEBUG(dbgSche, "[B] Tick [" << kernel->stats->totalTicks << "]: Process [" << readyList->Front()->getName() << "] is removed from queue.");
return readyList->RemoveFront();
}
}
@@ -101,9 +117,9 @@ Scheduler::FindNextToRun ()
//----------------------------------------------------------------------
void
Scheduler::Run (Thread *nextThread, bool finishing)
Scheduler::Run(Thread* nextThread, bool finishing)
{
Thread *oldThread = kernel->currentThread;
Thread* oldThread = kernel->currentThread;
ASSERT(kernel->interrupt->getLevel() == IntOff);
@@ -124,6 +140,7 @@ Scheduler::Run (Thread *nextThread, bool finishing)
nextThread->setStatus(RUNNING); // nextThread is now running
DEBUG(dbgThread, "Switching from: " << oldThread->getName() << " to: " << nextThread->getName());
DEBUG(dbgSche, "[C] Tick [" << kernel->stats->totalTicks << "]: Process [" << nextThread->getName() << "] is now selected for execution, thread [" << oldThread->getName() << "] is replaced.");
// This is a machine-dependent assembly language routine defined
// in switch.s. You may have to think
@@ -165,7 +182,7 @@ Scheduler::CheckToBeDestroyed()
toBeDestroyed = NULL;
}
}
//----------------------------------------------------------------------
// Scheduler::Print
// Print the scheduler state -- in other words, the contents of
@@ -174,6 +191,6 @@ Scheduler::CheckToBeDestroyed()
void
Scheduler::Print()
{
cout << "Ready list contents:\n";
readyList->Apply(ThreadPrint);
cout << "Ready list contents:\n";
readyList->Apply(ThreadPrint);
}