schedule: refactor next_thread() for better debug printing
[model-checker.git] / schedule.cc
1 #include "threads.h"
2 #include "schedule.h"
3 #include "common.h"
4 #include "model.h"
5
6 void Scheduler::add_thread(Thread *t)
7 {
8         DEBUG("thread %d\n", t->get_id());
9         readyList.push_back(t);
10 }
11
12 Thread *Scheduler::next_thread(void)
13 {
14         Thread *t = model->schedule_next_thread();
15
16         if (t != NULL) {
17                 readyList.remove(t);
18         } else if (readyList.empty()) {
19                 t = NULL;
20         } else {
21                 t = readyList.front();
22                 current = t;
23                 readyList.pop_front();
24         }
25
26         print();
27
28         return t;
29 }
30
31 Thread *Scheduler::get_current_thread(void)
32 {
33         return current;
34 }
35
36 void Scheduler::print()
37 {
38         if (current)
39                 printf("Current thread: %d\n", current->get_id());
40         else
41                 printf("No current thread\n");
42         printf("# Threads in ready list: %ld\n", readyList.size());
43
44         std::list<Thread *>::iterator it;
45         for (it = readyList.begin(); it != readyList.end(); it++)
46                 printf("In ready list: thread %d\n", (*it)->get_id());
47 }