Implement timedwait
[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 "execution.h"
9 #include "fuzzer.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 }
70
71 /**
72  * @brief Check if a Thread is currently enabled
73  *
74  * Check if a Thread is currently enabled. "Enabled" includes both
75  * THREAD_ENABLED and THREAD_SLEEP_SET.
76  * @param t The Thread to check
77  * @return True if the Thread is currently enabled
78  */
79 bool Scheduler::is_enabled(const Thread *t) const
80 {
81         return is_enabled(t->get_id());
82 }
83
84 /**
85  * @brief Check if a Thread is currently enabled
86  *
87  * Check if a Thread is currently enabled. "Enabled" includes both
88  * THREAD_ENABLED and THREAD_SLEEP_SET.
89  * @param tid The ID of the Thread to check
90  * @return True if the Thread is currently enabled
91  */
92 bool Scheduler::is_enabled(thread_id_t tid) const
93 {
94         int i = id_to_int(tid);
95         return (i >= enabled_len) ? false : (enabled[i] != THREAD_DISABLED);
96 }
97
98 /**
99  * @brief Check if a Thread is currently in the sleep set
100  * @param t The Thread to check
101  */
102 bool Scheduler::is_sleep_set(const Thread *t) const
103 {
104         return is_sleep_set(t->get_id());
105 }
106
107 bool Scheduler::is_sleep_set(thread_id_t tid) const
108 {
109         int id = id_to_int(tid);
110         ASSERT(id < enabled_len);
111         return enabled[id] == THREAD_SLEEP_SET;
112 }
113
114 /**
115  * @brief Check if execution is stuck with no enabled threads and some sleeping
116  * thread
117  * @return True if no threads are enabled and some thread is in the sleep set;
118  * false otherwise
119  */
120 bool Scheduler::all_threads_sleeping() const
121 {
122         bool sleeping = false;
123         for (int i = 0;i < enabled_len;i++)
124                 if (enabled[i] == THREAD_ENABLED)
125                         return false;
126                 else if (enabled[i] == THREAD_SLEEP_SET)
127                         sleeping = true;
128         return sleeping;
129 }
130
131 enabled_type_t Scheduler::get_enabled(const Thread *t) const
132 {
133         int id = id_to_int(t->get_id());
134         ASSERT(id < enabled_len);
135         return enabled[id];
136 }
137
138 /**
139  * Add a Thread to the sleep set.
140  * @param t The Thread to add
141  * A Thread is in THREAD_SLEEP_SET if it is sleeping or blocked by a wait
142  * operation that should fail spuriously (decide by fuzzer).
143  */
144 void Scheduler::add_sleep(Thread *t)
145 {
146         DEBUG("thread %d\n", id_to_int(t->get_id()));
147         set_enabled(t, THREAD_SLEEP_SET);
148 }
149
150 /**
151  * Remove a Thread from the sleep set.
152  * @param t The Thread to remove
153  */
154 void Scheduler::remove_sleep(Thread *t)
155 {
156         DEBUG("thread %d\n", id_to_int(t->get_id()));
157         set_enabled(t, THREAD_ENABLED);
158 }
159
160 /**
161  * Add a Thread to the scheduler's ready list.
162  * @param t The Thread to add
163  */
164 void Scheduler::add_thread(Thread *t)
165 {
166         DEBUG("thread %d\n", id_to_int(t->get_id()));
167         ASSERT(!t->is_model_thread());
168         set_enabled(t, THREAD_ENABLED);
169 }
170
171 /**
172  * Remove a given Thread from the scheduler.
173  * @param t The Thread to remove
174  */
175 void Scheduler::remove_thread(Thread *t)
176 {
177         if (current == t)
178                 current = NULL;
179         set_enabled(t, THREAD_DISABLED);
180 }
181
182 /**
183  * Prevent a Thread from being scheduled. The sleeping Thread should be
184  * re-awoken via Scheduler::wake.
185  * @param thread The Thread that should sleep
186  */
187 void Scheduler::sleep(Thread *t)
188 {
189         set_enabled(t, THREAD_DISABLED);
190         t->set_state(THREAD_BLOCKED);
191 }
192
193 /**
194  * Wake a Thread up that was previously waiting (see Scheduler::wait)
195  * @param t The Thread to wake up
196  */
197 void Scheduler::wake(Thread *t)
198 {
199         ASSERT(!t->is_model_thread());
200         set_enabled(t, THREAD_ENABLED);
201         t->set_state(THREAD_READY);
202 }
203
204 /**
205  * @brief Select a Thread to run via round-robin
206  *
207  *
208  * @return The next Thread to run
209  */
210 Thread * Scheduler::select_next_thread()
211 {
212         int avail_threads = 0;
213         int sleep_threads = 0;
214         int thread_list[enabled_len], sleep_list[enabled_len];
215         Thread * thread;
216
217         for (int i = 0;i < enabled_len;i++) {
218                 if (enabled[i] == THREAD_ENABLED)
219                         thread_list[avail_threads++] = i;
220                 else if (enabled[i] == THREAD_SLEEP_SET)
221                         sleep_list[sleep_threads++] = i;
222         }
223
224         if (avail_threads == 0 && !execution->getFuzzer()->has_paused_threads()) {
225                 if (sleep_threads != 0) {
226                         // No threads available, but some threads sleeping. Wake up one of them
227                         thread = execution->getFuzzer()->selectThread(sleep_list, sleep_threads);
228                         remove_sleep(thread);
229                         thread->set_wakeup_state(true);
230                 } else {
231                         return NULL;    // No threads available and no threads sleeping.
232                 }
233         } else {
234                 // Some threads are available
235                 thread = execution->getFuzzer()->selectThread(thread_list, avail_threads);
236         }
237
238         //curr_thread_index = id_to_int(thread->get_id());
239         return thread;
240 }
241
242 void Scheduler::set_scheduler_thread(thread_id_t tid) {
243         curr_thread_index=id_to_int(tid);
244 }
245
246 /**
247  * @brief Set the current "running" Thread
248  * @param t Thread to run
249  */
250 void Scheduler::set_current_thread(Thread *t)
251 {
252         ASSERT(!t || !t->is_model_thread());
253
254         current = t;
255         if (DBG_ENABLED())
256                 print();
257 }
258
259 /**
260  * @return The currently-running Thread
261  */
262 Thread * Scheduler::get_current_thread() const
263 {
264         ASSERT(!current || !current->is_model_thread());
265         return current;
266 }
267
268 /**
269  * Print debugging information about the current state of the scheduler. Only
270  * prints something if debugging is enabled.
271  */
272 void Scheduler::print() const
273 {
274         int curr_id = current ? id_to_int(current->get_id()) : -1;
275
276         model_print("Scheduler: ");
277         for (int i = 0;i < enabled_len;i++) {
278                 char str[20];
279                 enabled_type_to_string(enabled[i], str);
280                 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);
281         }
282         model_print("\n");
283 }