classScheduler { public: Scheduler(); // Initialize list of ready threads Scheduler(SchedulerType type); // 這個要改,加上參數 ~Scheduler(); // De-allocate ready list
voidReadyToRun(Thread* thread); // Thread can be dispatched. Thread* FindNextToRun(); // Dequeue first thread on the ready // list, if any, and return thread. voidRun(Thread* nextThread, bool finishing); // Cause nextThread to start running voidCheckToBeDestroyed(); // Check if thread that had been // running needs to be deleted voidPrint(); // Print contents of ready list
//---------------------------------------------------------------------- // Scheduler::Scheduler // Initialize the list of ready but not running threads. // Initially, no ready threads. //---------------------------------------------------------------------- Scheduler::Scheduler() { Scheduler(RR); }
// 主要要自己動手的地方都在這裡而已 Scheduler::Scheduler(SchedulerType type) { schedulerType = type; switch(schedulerType) { case RR: readyList = new List<Thread *>; break; case SJF: readyList = newSortedList<Thread *>(SJFCompare); break; case Priority: readyList = newSortedList<Thread *>(PriorityCompare); break; case FIFO: readyList = newSortedList<Thread *>(FIFOCompare); break; case SRTF: // 和SJF其實一樣,這兩個的差別本來就只有是不是preemptive而已 readyList = newSortedList<Thread *>(SJFCompare); break; } toBeDestroyed = NULL; }
// 對原本的PutToSleep多做調整 voidSleepList::PutToSleep(Thread*t, int x) { IntStatus oldLevel = kernel->interrupt->SetLevel(IntOff); // 不讓其他 thread 可以中斷 ASSERT(kernel->interrupt->getLevel() == IntOff); // check if it cannot be interrupt. threadlist.push_back(SleepThread(t, counter + x)); // put into the list if(kernel->scheduler->getSchedulerType() != SRTF){ t->Sleep(false); } kernel->interrupt->SetLevel(oldLevel); }