X-Git-Url: http://plrg.eecs.uci.edu/git/?p=c11tester.git;a=blobdiff_plain;f=schedule.cc;h=1791605b7c38842c37d11553c37f663c9dc88c38;hp=abb3e4b3975f34c3cbaf876ead0af680699864d9;hb=1069e1b27fc4ed06476da0a8793a55e68b8a6b86;hpb=3e3ba08b7f8483430946ac7db0cc3bdbaa6f8a90 diff --git a/schedule.cc b/schedule.cc index abb3e4b3..1791605b 100644 --- a/schedule.cc +++ b/schedule.cc @@ -1,26 +1,61 @@ -#include "libthreads.h" +#include "threads.h" #include "schedule.h" #include "common.h" #include "model.h" -void Scheduler::add_thread(struct thread *t) +Scheduler::Scheduler() : + current(NULL) { - DEBUG("thread %d\n", t->id); - queue.push(t); } -struct thread *Scheduler::next_thread(void) +void Scheduler::add_thread(Thread *t) { - if (queue.empty()) - return NULL; + DEBUG("thread %d\n", t->get_id()); + readyList.push_back(t); +} + +void Scheduler::remove_thread(Thread *t) +{ + if (current == t) + current = NULL; + else + readyList.remove(t); +} - current = queue.front(); - queue.pop(); +Thread * Scheduler::next_thread(void) +{ + Thread *t = model->schedule_next_thread(); - return current; + if (t != NULL) { + current = t; + readyList.remove(t); + } else if (readyList.empty()) { + t = NULL; + } else { + t = readyList.front(); + current = t; + readyList.pop_front(); + } + + print(); + + return t; } -struct thread *Scheduler::get_current_thread(void) +Thread * Scheduler::get_current_thread(void) { return current; } + +void Scheduler::print() +{ + if (current) + DEBUG("Current thread: %d\n", current->get_id()); + else + DEBUG("No current thread\n"); + DEBUG("Num. threads in ready list: %zu\n", readyList.size()); + + std::list >::iterator it; + for (it = readyList.begin(); it != readyList.end(); it++) + DEBUG("In ready list: thread %d\n", (*it)->get_id()); +}