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