328bda6990c3e55a61574261f893fa77a9031eaa
[c11tester.git] / schedule.cc
1 /* -*- Mode: C; indent-tabs-mode: t -*- */
2
3 #include "threads.h"
4 #include "schedule.h"
5 #include "common.h"
6 #include "model.h"
7
8 Scheduler::Scheduler(): 
9 current(NULL) 
10 {
11 }
12
13 void Scheduler::add_thread(Thread *t)
14 {
15         DEBUG("thread %d\n", t->get_id());
16         readyList.push_back(t);
17 }
18
19 void Scheduler::remove_thread(Thread *t)
20 {
21         if (current == t)
22                 current = NULL;
23         else
24                 readyList.remove(t);
25 }
26
27 Thread * Scheduler::next_thread(void)
28 {
29         Thread *t = model->schedule_next_thread();
30
31         if (t != NULL) {
32                 current = t;
33                 readyList.remove(t);
34         } else if (readyList.empty()) {
35                 t = NULL;
36         } else {
37                 t = readyList.front();
38                 current = t;
39                 readyList.pop_front();
40         }
41
42         print();
43
44         return t;
45 }
46
47 Thread * Scheduler::get_current_thread(void)
48 {
49         return current;
50 }
51
52 void Scheduler::print()
53 {
54         if (current)
55                 DEBUG("Current thread: %d\n", current->get_id());
56         else
57                 DEBUG("No current thread\n");
58         DEBUG("Num. threads in ready list: %zu\n", readyList.size());
59
60         std::list<Thread *, MyAlloc< Thread * > >::iterator it;
61         for (it = readyList.begin(); it != readyList.end(); it++)
62                 DEBUG("In ready list: thread %d\n", (*it)->get_id());
63 }