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