schedule: make print() const
[model-checker.git] / schedule.cc
1 #include "threads.h"
2 #include "schedule.h"
3 #include "common.h"
4 #include "model.h"
5
6 /** Constructor */
7 Scheduler::Scheduler() :
8         current(NULL)
9 {
10 }
11
12 /**
13  * Add a Thread to the scheduler's ready list.
14  * @param t The Thread to add
15  */
16 void Scheduler::add_thread(Thread *t)
17 {
18         DEBUG("thread %d\n", t->get_id());
19         readyList.push_back(t);
20 }
21
22 /**
23  * Remove a given Thread from the scheduler.
24  * @param t The Thread to remove
25  */
26 void Scheduler::remove_thread(Thread *t)
27 {
28         if (current == t)
29                 current = NULL;
30         else
31                 readyList.remove(t);
32 }
33
34 /**
35  * Remove one Thread from the scheduler. This implementation performs FIFO.
36  * @return The next Thread to run
37  */
38 Thread * Scheduler::next_thread()
39 {
40         Thread *t = model->schedule_next_thread();
41
42         if (t != NULL) {
43                 current = t;
44                 readyList.remove(t);
45         } else if (readyList.empty()) {
46                 t = NULL;
47         } else {
48                 t = readyList.front();
49                 current = t;
50                 readyList.pop_front();
51         }
52
53         print();
54
55         return t;
56 }
57
58 /**
59  * @return The currently-running Thread
60  */
61 Thread * Scheduler::get_current_thread() const
62 {
63         return current;
64 }
65
66 /**
67  * Print debugging information about the current state of the scheduler. Only
68  * prints something if debugging is enabled.
69  */
70 void Scheduler::print() const
71 {
72         if (current)
73                 DEBUG("Current thread: %d\n", current->get_id());
74         else
75                 DEBUG("No current thread\n");
76         DEBUG("Num. threads in ready list: %zu\n", readyList.size());
77
78         std::list<Thread *, MyAlloc< Thread * > >::const_iterator it;
79         for (it = readyList.begin(); it != readyList.end(); it++)
80                 DEBUG("In ready list: thread %d\n", (*it)->get_id());
81 }