Ytshih/hw2
This commit is contained in:
@@ -74,13 +74,13 @@ Scheduler::ReadyToRun (Thread *thread)
|
||||
Thread *
|
||||
Scheduler::FindNextToRun ()
|
||||
{
|
||||
ASSERT(kernel->interrupt->getLevel() == IntOff);
|
||||
ASSERT(kernel->interrupt->getLevel() == IntOff);
|
||||
|
||||
if (readyList->IsEmpty()) {
|
||||
return NULL;
|
||||
} else {
|
||||
return readyList->RemoveFront();
|
||||
}
|
||||
if (readyList->IsEmpty()) {
|
||||
return NULL;
|
||||
} else {
|
||||
return readyList->RemoveFront();
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
@@ -103,50 +103,50 @@ Scheduler::FindNextToRun ()
|
||||
void
|
||||
Scheduler::Run (Thread *nextThread, bool finishing)
|
||||
{
|
||||
Thread *oldThread = kernel->currentThread;
|
||||
|
||||
ASSERT(kernel->interrupt->getLevel() == IntOff);
|
||||
Thread *oldThread = kernel->currentThread;
|
||||
|
||||
if (finishing) { // mark that we need to delete current thread
|
||||
ASSERT(toBeDestroyed == NULL);
|
||||
toBeDestroyed = oldThread;
|
||||
}
|
||||
|
||||
if (oldThread->space != NULL) { // if this thread is a user program,
|
||||
oldThread->SaveUserState(); // save the user's CPU registers
|
||||
oldThread->space->SaveState();
|
||||
}
|
||||
|
||||
oldThread->CheckOverflow(); // check if the old thread
|
||||
// had an undetected stack overflow
|
||||
ASSERT(kernel->interrupt->getLevel() == IntOff);
|
||||
|
||||
kernel->currentThread = nextThread; // switch to the next thread
|
||||
nextThread->setStatus(RUNNING); // nextThread is now running
|
||||
|
||||
DEBUG(dbgThread, "Switching from: " << oldThread->getName() << " to: " << nextThread->getName());
|
||||
|
||||
// This is a machine-dependent assembly language routine defined
|
||||
// in switch.s. You may have to think
|
||||
// a bit to figure out what happens after this, both from the point
|
||||
// of view of the thread and from the perspective of the "outside world".
|
||||
if (finishing) { // mark that we need to delete current thread
|
||||
ASSERT(toBeDestroyed == NULL);
|
||||
toBeDestroyed = oldThread;
|
||||
}
|
||||
|
||||
SWITCH(oldThread, nextThread);
|
||||
if (oldThread->space != NULL) { // if this thread is a user program,
|
||||
oldThread->SaveUserState(); // save the user's CPU registers
|
||||
oldThread->space->SaveState();
|
||||
}
|
||||
|
||||
// we're back, running oldThread
|
||||
|
||||
// interrupts are off when we return from switch!
|
||||
ASSERT(kernel->interrupt->getLevel() == IntOff);
|
||||
oldThread->CheckOverflow(); // check if the old thread
|
||||
// had an undetected stack overflow
|
||||
|
||||
DEBUG(dbgThread, "Now in thread: " << oldThread->getName());
|
||||
kernel->currentThread = nextThread; // switch to the next thread
|
||||
nextThread->setStatus(RUNNING); // nextThread is now running
|
||||
|
||||
CheckToBeDestroyed(); // check if thread we were running
|
||||
// before this one has finished
|
||||
// and needs to be cleaned up
|
||||
|
||||
if (oldThread->space != NULL) { // if there is an address space
|
||||
oldThread->RestoreUserState(); // to restore, do it.
|
||||
oldThread->space->RestoreState();
|
||||
}
|
||||
DEBUG(dbgThread, "Switching from: " << oldThread->getName() << " to: " << nextThread->getName());
|
||||
|
||||
// This is a machine-dependent assembly language routine defined
|
||||
// in switch.s. You may have to think
|
||||
// a bit to figure out what happens after this, both from the point
|
||||
// of view of the thread and from the perspective of the "outside world".
|
||||
|
||||
SWITCH(oldThread, nextThread);
|
||||
|
||||
// we're back, running oldThread
|
||||
|
||||
// interrupts are off when we return from switch!
|
||||
ASSERT(kernel->interrupt->getLevel() == IntOff);
|
||||
|
||||
DEBUG(dbgThread, "Now in thread: " << oldThread->getName());
|
||||
|
||||
CheckToBeDestroyed(); // check if thread we were running
|
||||
// before this one has finished
|
||||
// and needs to be cleaned up
|
||||
|
||||
if (oldThread->space != NULL) { // if there is an address space
|
||||
oldThread->RestoreUserState(); // to restore, do it.
|
||||
oldThread->space->RestoreState();
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
@@ -160,10 +160,10 @@ Scheduler::Run (Thread *nextThread, bool finishing)
|
||||
void
|
||||
Scheduler::CheckToBeDestroyed()
|
||||
{
|
||||
if (toBeDestroyed != NULL) {
|
||||
delete toBeDestroyed;
|
||||
toBeDestroyed = NULL;
|
||||
}
|
||||
if (toBeDestroyed != NULL) {
|
||||
delete toBeDestroyed;
|
||||
toBeDestroyed = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user