missing changes
[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  * Prevent a Thread from being scheduled. The sleeping Thread should be
36  * re-awoken via Scheduler::wake.
37  * @param thread The Thread that should sleep
38  */
39 void Scheduler::sleep(Thread *t)
40 {
41         remove_thread(t);
42         t->set_state(THREAD_BLOCKED);
43 }
44
45 /**
46  * Wake a Thread up that was previously waiting (see Scheduler::wait)
47  * @param t The Thread to wake up
48  */
49 void Scheduler::wake(Thread *t)
50 {
51         add_thread(t);
52         t->set_state(THREAD_READY);
53 }
54
55 /**
56  * Remove one Thread from the scheduler. This implementation defaults to FIFO,
57  * if a thread is not already provided.
58  *
59  * @param t Thread to run, if chosen by an external entity (e.g.,
60  * ModelChecker). May be NULL to indicate no external choice.
61  * @return The next Thread to run
62  */
63 Thread * Scheduler::next_thread(Thread *t)
64 {
65         if (t != NULL) {
66                 current = t;
67                 readyList.remove(t);
68         } else if (readyList.empty()) {
69                 t = NULL;
70         } else {
71                 t = readyList.front();
72                 current = t;
73                 readyList.pop_front();
74         }
75
76         print();
77
78         return t;
79 }
80
81 /**
82  * @return The currently-running Thread
83  */
84 Thread * Scheduler::get_current_thread() const
85 {
86         return current;
87 }
88
89 /**
90  * Print debugging information about the current state of the scheduler. Only
91  * prints something if debugging is enabled.
92  */
93 void Scheduler::print() const
94 {
95         if (current)
96                 DEBUG("Current thread: %d\n", current->get_id());
97         else
98                 DEBUG("No current thread\n");
99         DEBUG("Num. threads in ready list: %zu\n", readyList.size());
100
101         std::list<Thread *, MyAlloc< Thread * > >::const_iterator it;
102         for (it = readyList.begin(); it != readyList.end(); it++)
103                 DEBUG("In ready list: thread %d\n", (*it)->get_id());
104 }