partial conversion to fuzzer
[c11tester.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 /**
135  * Add a Thread to the sleep set.
136  * @param t The Thread to add
137  */
138 void Scheduler::add_sleep(Thread *t)
139 {
140         DEBUG("thread %d\n", id_to_int(t->get_id()));
141         set_enabled(t, THREAD_SLEEP_SET);
142 }
143
144 /**
145  * Remove a Thread from the sleep set.
146  * @param t The Thread to remove
147  */
148 void Scheduler::remove_sleep(Thread *t)
149 {
150         DEBUG("thread %d\n", id_to_int(t->get_id()));
151         set_enabled(t, THREAD_ENABLED);
152 }
153
154 /**
155  * Add a Thread to the scheduler's ready list.
156  * @param t The Thread to add
157  */
158 void Scheduler::add_thread(Thread *t)
159 {
160         DEBUG("thread %d\n", id_to_int(t->get_id()));
161         ASSERT(!t->is_model_thread());
162         set_enabled(t, THREAD_ENABLED);
163 }
164
165 /**
166  * Remove a given Thread from the scheduler.
167  * @param t The Thread to remove
168  */
169 void Scheduler::remove_thread(Thread *t)
170 {
171         if (current == t)
172                 current = NULL;
173         set_enabled(t, THREAD_DISABLED);
174 }
175
176 /**
177  * Prevent a Thread from being scheduled. The sleeping Thread should be
178  * re-awoken via Scheduler::wake.
179  * @param thread The Thread that should sleep
180  */
181 void Scheduler::sleep(Thread *t)
182 {
183         set_enabled(t, THREAD_DISABLED);
184         t->set_state(THREAD_BLOCKED);
185 }
186
187 /**
188  * Wake a Thread up that was previously waiting (see Scheduler::wait)
189  * @param t The Thread to wake up
190  */
191 void Scheduler::wake(Thread *t)
192 {
193         ASSERT(!t->is_model_thread());
194         set_enabled(t, THREAD_ENABLED);
195         t->set_state(THREAD_READY);
196 }
197
198 /**
199  * @brief Select a Thread to run via round-robin
200  *
201  * @param n The current Node, holding priority information for the next thread
202  * selection
203  *
204  * @return The next Thread to run
205  */
206 Thread * Scheduler::select_next_thread(Node *n)
207 {
208         bool have_enabled_thread_with_priority = false;
209         if (model->params.fairwindow != 0) {
210                 for (int i = 0; i < enabled_len; i++) {
211                         thread_id_t tid = int_to_id(i);
212                         if (n->has_priority(tid)) {
213                                 DEBUG("Node (tid %d) has priority\n", i);
214                                 if (enabled[i] != THREAD_DISABLED)
215                                         have_enabled_thread_with_priority = true;
216                         }
217                 }
218         }       
219
220         int avail_threads = enabled_len;        // number of available threads
221         int thread_list[enabled_len];   // keep a list of threads to select from
222         for (int i = 0; i< enabled_len; i++){
223                 thread_list[i] = i;
224         }
225
226         while (avail_threads > 0) {
227                 int random_index = rand() % avail_threads;
228                 curr_thread_index = thread_list[random_index];  // randomly select a thread from available threads
229                 
230                 // curr_thread_index = (curr_thread_index + i + 1) % enabled_len;
231                 thread_id_t curr_tid = int_to_id(curr_thread_index);
232                 if (model->params.yieldon) {
233                         bool bad_thread = false;
234                         for (int j = 0; j < enabled_len; j++) {
235                                 thread_id_t tother = int_to_id(j);
236                                 if ((enabled[j] != THREAD_DISABLED) && n->has_priority_over(curr_tid, tother)) {
237                                         bad_thread=true;
238                                         break;
239                                 }
240                         }
241                         if (bad_thread) {
242                                 thread_list[random_index] = thread_list[avail_threads - 1]; // remove this threads from available threads 
243                                 avail_threads--;
244
245                                 continue;
246                         }
247                 }
248                 
249                 if (enabled[curr_thread_index] == THREAD_ENABLED &&
250                                 (!have_enabled_thread_with_priority || n->has_priority(curr_tid))) {
251                         return model->get_thread(curr_tid);
252                 } else {        // remove this threads from available threads 
253                         thread_list[random_index] = thread_list[avail_threads - 1]; 
254                         avail_threads--;
255                 }
256         }
257         
258         /* No thread was enabled */
259         return NULL;
260 }
261
262 void Scheduler::set_scheduler_thread(thread_id_t tid) {
263         curr_thread_index=id_to_int(tid);
264 }
265
266 /**
267  * @brief Set the current "running" Thread
268  * @param t Thread to run
269  */
270 void Scheduler::set_current_thread(Thread *t)
271 {
272         ASSERT(!t || !t->is_model_thread());
273
274         current = t;
275         if (DBG_ENABLED())
276                 print();
277 }
278
279 /**
280  * @return The currently-running Thread
281  */
282 Thread * Scheduler::get_current_thread() const
283 {
284         ASSERT(!current || !current->is_model_thread());
285         return current;
286 }
287
288 /**
289  * Print debugging information about the current state of the scheduler. Only
290  * prints something if debugging is enabled.
291  */
292 void Scheduler::print() const
293 {
294         int curr_id = current ? id_to_int(current->get_id()) : -1;
295
296         model_print("Scheduler: ");
297         for (int i = 0; i < enabled_len; i++) {
298                 char str[20];
299                 enabled_type_to_string(enabled[i], str);
300                 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);
301         }
302         model_print("\n");
303 }