Work around changes in newer versions of glibc
[model-checker.git] / schedule.cc
1 #include <string.h>
2 #include <stdlib.h>
3
4 #include "threads-model.h"
5 #include "schedule.h"
6 #include "common.h"
7 #include "model.h"
8 #include "nodestack.h"
9 #include "execution.h"
10
11 /**
12  * Format an "enabled_type_t" for printing
13  * @param e The type to format
14  * @param str The output character array
15  */
16 void enabled_type_to_string(enabled_type_t e, char *str)
17 {
18         const char *res;
19         switch (e) {
20         case THREAD_DISABLED:
21                 res = "disabled";
22                 break;
23         case THREAD_ENABLED:
24                 res = "enabled";
25                 break;
26         case THREAD_SLEEP_SET:
27                 res = "sleep";
28                 break;
29         default:
30                 ASSERT(0);
31                 res = NULL;
32                 break;
33         }
34         strcpy(str, res);
35 }
36
37 /** Constructor */
38 Scheduler::Scheduler() :
39         execution(NULL),
40         enabled(NULL),
41         enabled_len(0),
42         curr_thread_index(0),
43         current(NULL)
44 {
45 }
46
47 /**
48  * @brief Register the ModelExecution engine
49  * @param execution The ModelExecution which is controlling execution
50  */
51 void Scheduler::register_engine(ModelExecution *execution)
52 {
53         this->execution = execution;
54 }
55
56 void Scheduler::set_enabled(Thread *t, enabled_type_t enabled_status) {
57         int threadid = id_to_int(t->get_id());
58         if (threadid >= enabled_len) {
59                 enabled_type_t *new_enabled = (enabled_type_t *)snapshot_malloc(sizeof(enabled_type_t) * (threadid + 1));
60                 memset(&new_enabled[enabled_len], 0, (threadid + 1 - enabled_len) * sizeof(enabled_type_t));
61                 if (enabled != NULL) {
62                         memcpy(new_enabled, enabled, enabled_len * sizeof(enabled_type_t));
63                         snapshot_free(enabled);
64                 }
65                 enabled = new_enabled;
66                 enabled_len = threadid + 1;
67         }
68         enabled[threadid] = enabled_status;
69         if (enabled_status == THREAD_DISABLED)
70                 execution->check_promises_thread_disabled();
71 }
72
73 /**
74  * @brief Check if a Thread is currently enabled
75  *
76  * Check if a Thread is currently enabled. "Enabled" includes both
77  * THREAD_ENABLED and THREAD_SLEEP_SET.
78  * @param t The Thread to check
79  * @return True if the Thread is currently enabled
80  */
81 bool Scheduler::is_enabled(const Thread *t) const
82 {
83         return is_enabled(t->get_id());
84 }
85
86 /**
87  * @brief Check if a Thread is currently enabled
88  *
89  * Check if a Thread is currently enabled. "Enabled" includes both
90  * THREAD_ENABLED and THREAD_SLEEP_SET.
91  * @param tid The ID of the Thread to check
92  * @return True if the Thread is currently enabled
93  */
94 bool Scheduler::is_enabled(thread_id_t tid) const
95 {
96         int i = id_to_int(tid);
97         return (i >= enabled_len) ? false : (enabled[i] != THREAD_DISABLED);
98 }
99
100 /**
101  * @brief Check if a Thread is currently in the sleep set
102  * @param t The Thread to check
103  * @return True if the Thread is currently enabled
104  */
105 bool Scheduler::is_sleep_set(const Thread *t) const
106 {
107         return get_enabled(t) == THREAD_SLEEP_SET;
108 }
109
110 /**
111  * @brief Check if execution is stuck with no enabled threads and some sleeping
112  * thread
113  * @return True if no threads are enabled an some thread is in the sleep set;
114  * false otherwise
115  */
116 bool Scheduler::all_threads_sleeping() const
117 {
118         bool sleeping = false;
119         for (int i = 0; i < enabled_len; i++)
120                 if (enabled[i] == THREAD_ENABLED)
121                         return false;
122                 else if (enabled[i] == THREAD_SLEEP_SET)
123                         sleeping = true;
124         return sleeping;
125 }
126
127 enabled_type_t Scheduler::get_enabled(const Thread *t) const
128 {
129         int id = id_to_int(t->get_id());
130         ASSERT(id < enabled_len);
131         return enabled[id];
132 }
133
134 void Scheduler::update_sleep_set(Node *n) {
135         enabled_type_t *enabled_array = n->get_enabled_array();
136         for (int i = 0; i < enabled_len; i++) {
137                 if (enabled_array[i] == THREAD_SLEEP_SET) {
138                         enabled[i] = THREAD_SLEEP_SET;
139                 }
140         }
141 }
142
143 /**
144  * Add a Thread to the sleep set.
145  * @param t The Thread to add
146  */
147 void Scheduler::add_sleep(Thread *t)
148 {
149         DEBUG("thread %d\n", id_to_int(t->get_id()));
150         set_enabled(t, THREAD_SLEEP_SET);
151 }
152
153 /**
154  * Remove a Thread from the sleep set.
155  * @param t The Thread to remove
156  */
157 void Scheduler::remove_sleep(Thread *t)
158 {
159         DEBUG("thread %d\n", id_to_int(t->get_id()));
160         set_enabled(t, THREAD_ENABLED);
161 }
162
163 /**
164  * Add a Thread to the scheduler's ready list.
165  * @param t The Thread to add
166  */
167 void Scheduler::add_thread(Thread *t)
168 {
169         DEBUG("thread %d\n", id_to_int(t->get_id()));
170         ASSERT(!t->is_model_thread());
171         set_enabled(t, THREAD_ENABLED);
172 }
173
174 /**
175  * Remove a given Thread from the scheduler.
176  * @param t The Thread to remove
177  */
178 void Scheduler::remove_thread(Thread *t)
179 {
180         if (current == t)
181                 current = NULL;
182         set_enabled(t, THREAD_DISABLED);
183 }
184
185 /**
186  * Prevent a Thread from being scheduled. The sleeping Thread should be
187  * re-awoken via Scheduler::wake.
188  * @param thread The Thread that should sleep
189  */
190 void Scheduler::sleep(Thread *t)
191 {
192         set_enabled(t, THREAD_DISABLED);
193         t->set_state(THREAD_BLOCKED);
194 }
195
196 /**
197  * Wake a Thread up that was previously waiting (see Scheduler::wait)
198  * @param t The Thread to wake up
199  */
200 void Scheduler::wake(Thread *t)
201 {
202         ASSERT(!t->is_model_thread());
203         set_enabled(t, THREAD_ENABLED);
204         t->set_state(THREAD_READY);
205 }
206
207 /**
208  * @brief Select a Thread to run via round-robin
209  *
210  * @param n The current Node, holding priority information for the next thread
211  * selection
212  *
213  * @return The next Thread to run
214  */
215 Thread * Scheduler::select_next_thread(Node *n)
216 {
217         int old_curr_thread = curr_thread_index;
218
219         bool have_enabled_thread_with_priority = false;
220         if (model->params.fairwindow != 0) {
221                 for (int i = 0; i < enabled_len; i++) {
222                         thread_id_t tid = int_to_id(i);
223                         if (n->has_priority(tid)) {
224                                 DEBUG("Node (tid %d) has priority\n", i);
225                                 if (enabled[i] != THREAD_DISABLED)
226                                         have_enabled_thread_with_priority = true;
227                         }
228                 }
229         }       
230
231         for (int i = 0; i < enabled_len; i++) {
232                 curr_thread_index = (old_curr_thread + i + 1) % enabled_len;
233                 thread_id_t curr_tid = int_to_id(curr_thread_index);
234                 if (model->params.yieldon) {
235                         bool bad_thread = false;
236                         for (int j = 0; j < enabled_len; j++) {
237                                 thread_id_t tother = int_to_id(j);
238                                 if ((enabled[j] != THREAD_DISABLED) && n->has_priority_over(curr_tid, tother)) {
239                                         bad_thread=true;
240                                         break;
241                                 }
242                         }
243                         if (bad_thread)
244                                 continue;
245                 }
246                 
247                 if (enabled[curr_thread_index] == THREAD_ENABLED &&
248                                 (!have_enabled_thread_with_priority || n->has_priority(curr_tid))) {
249                         return model->get_thread(curr_tid);
250                 }
251         }
252         
253         /* No thread was enabled */
254         return NULL;
255 }
256
257 void Scheduler::set_scheduler_thread(thread_id_t tid) {
258         curr_thread_index=id_to_int(tid);
259 }
260
261 /**
262  * @brief Set the current "running" Thread
263  * @param t Thread to run
264  */
265 void Scheduler::set_current_thread(Thread *t)
266 {
267         ASSERT(!t || !t->is_model_thread());
268
269         current = t;
270         if (DBG_ENABLED())
271                 print();
272 }
273
274 /**
275  * @return The currently-running Thread
276  */
277 Thread * Scheduler::get_current_thread() const
278 {
279         ASSERT(!current || !current->is_model_thread());
280         return current;
281 }
282
283 /**
284  * Print debugging information about the current state of the scheduler. Only
285  * prints something if debugging is enabled.
286  */
287 void Scheduler::print() const
288 {
289         int curr_id = current ? id_to_int(current->get_id()) : -1;
290
291         model_print("Scheduler: ");
292         for (int i = 0; i < enabled_len; i++) {
293                 char str[20];
294                 enabled_type_to_string(enabled[i], str);
295                 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);
296         }
297         model_print("\n");
298 }