my schedule 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         is_enabled(NULL),
9         enabled_len(0),
10         curr_thread_index(0),
11         current(NULL)
12 {
13 }
14
15 void Scheduler::set_enabled(Thread *t, bool enabled_status) {
16         int threadid=id_to_int(t->get_id());
17         if (threadid>=enabled_len) {
18                 bool * new_enabled=(bool *)malloc(sizeof(bool)*(threadid+1));
19                 memcpy(new_enabled, is_enabled, enabled_len*sizeof(bool));
20                 memset(&new_enabled[enabled_len], 0, (threadid+1-enabled_len)*sizeof(bool));
21                 free(is_enabled);
22                 is_enabled=new_enabled;
23                 enabled_len=threadid+1;
24         }
25         is_enabled[threadid]=enabled_status;
26 }
27
28 /**
29  * Add a Thread to the scheduler's ready list.
30  * @param t The Thread to add
31  */
32 void Scheduler::add_thread(Thread *t)
33 {
34         DEBUG("thread %d\n", t->get_id());
35         set_enabled(t, true);
36 }
37
38 /**
39  * Remove a given Thread from the scheduler.
40  * @param t The Thread to remove
41  */
42 void Scheduler::remove_thread(Thread *t)
43 {
44         if (current == t)
45                 current = NULL;
46         set_enabled(t, false);
47 }
48
49 /**
50  * Prevent a Thread from being scheduled. The sleeping Thread should be
51  * re-awoken via Scheduler::wake.
52  * @param thread The Thread that should sleep
53  */
54 void Scheduler::sleep(Thread *t)
55 {
56         set_enabled(t, false);
57         t->set_state(THREAD_BLOCKED);
58 }
59
60 /**
61  * Wake a Thread up that was previously waiting (see Scheduler::wait)
62  * @param t The Thread to wake up
63  */
64 void Scheduler::wake(Thread *t)
65 {
66         set_enabled(t, true);
67         t->set_state(THREAD_READY);
68 }
69
70 /**
71  * Remove one Thread from the scheduler. This implementation defaults to FIFO,
72  * if a thread is not already provided.
73  *
74  * @param t Thread to run, if chosen by an external entity (e.g.,
75  * ModelChecker). May be NULL to indicate no external choice.
76  * @return The next Thread to run
77  */
78 Thread * Scheduler::next_thread(Thread *t)
79 {
80         if ( t == NULL ) {
81                 int old_curr_thread = curr_thread_index;
82                 while(true) {
83                         curr_thread_index = (curr_thread_index+1) % enabled_len;
84                         if (is_enabled[curr_thread_index]) {
85                                 t = model->get_thread(int_to_id(curr_thread_index));
86                                 break;
87                         }
88                         if (curr_thread_index == old_curr_thread) {
89                                 print();
90                                 return NULL;
91                         }
92                 }
93         } else {
94                 curr_thread_index = id_to_int(t->get_id());
95         }
96
97         current = t;
98         print();
99         return t;
100 }
101
102 /**
103  * @return The currently-running Thread
104  */
105 Thread * Scheduler::get_current_thread() const
106 {
107         return current;
108 }
109
110 /**
111  * Print debugging information about the current state of the scheduler. Only
112  * prints something if debugging is enabled.
113  */
114 void Scheduler::print() const
115 {
116         if (current)
117                 DEBUG("Current thread: %d\n", current->get_id());
118         else
119                 DEBUG("No current thread\n");
120 }