4 #include "threads-model.h"
12 * Format an "enabled_type_t" for printing
13 * @param e The type to format
14 * @param str The output character array
16 void enabled_type_to_string(enabled_type_t e, char *str)
26 case THREAD_SLEEP_SET:
38 Scheduler::Scheduler() :
48 * @brief Register the ModelExecution engine
49 * @param execution The ModelExecution which is controlling execution
51 void Scheduler::register_engine(ModelExecution *execution)
53 this->execution = execution;
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);
65 enabled = new_enabled;
66 enabled_len = threadid + 1;
68 enabled[threadid] = enabled_status;
72 * @brief Check if a Thread is currently enabled
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
79 bool Scheduler::is_enabled(const Thread *t) const
81 return is_enabled(t->get_id());
85 * @brief Check if a Thread is currently enabled
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
92 bool Scheduler::is_enabled(thread_id_t tid) const
94 int i = id_to_int(tid);
95 return (i >= enabled_len) ? false : (enabled[i] != THREAD_DISABLED);
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
103 bool Scheduler::is_sleep_set(const Thread *t) const
105 return is_sleep_set(t->get_id());
108 bool Scheduler::is_sleep_set(thread_id_t tid) const
110 int id = id_to_int(tid);
111 ASSERT(id < enabled_len);
112 return enabled[id] == THREAD_SLEEP_SET;
116 * @brief Check if execution is stuck with no enabled threads and some sleeping
118 * @return True if no threads are enabled and some thread is in the sleep set;
121 bool Scheduler::all_threads_sleeping() const
123 bool sleeping = false;
124 for (int i = 0;i < enabled_len;i++)
125 if (enabled[i] == THREAD_ENABLED)
127 else if (enabled[i] == THREAD_SLEEP_SET)
132 enabled_type_t Scheduler::get_enabled(const Thread *t) const
134 int id = id_to_int(t->get_id());
135 ASSERT(id < enabled_len);
140 * Add a Thread to the sleep set.
141 * @param t The Thread to add
143 void Scheduler::add_sleep(Thread *t)
145 DEBUG("thread %d\n", id_to_int(t->get_id()));
146 set_enabled(t, THREAD_SLEEP_SET);
150 * Remove a Thread from the sleep set.
151 * @param t The Thread to remove
153 void Scheduler::remove_sleep(Thread *t)
155 DEBUG("thread %d\n", id_to_int(t->get_id()));
156 set_enabled(t, THREAD_ENABLED);
160 * Add a Thread to the scheduler's ready list.
161 * @param t The Thread to add
163 void Scheduler::add_thread(Thread *t)
165 DEBUG("thread %d\n", id_to_int(t->get_id()));
166 ASSERT(!t->is_model_thread());
167 set_enabled(t, THREAD_ENABLED);
171 * Remove a given Thread from the scheduler.
172 * @param t The Thread to remove
174 void Scheduler::remove_thread(Thread *t)
178 set_enabled(t, THREAD_DISABLED);
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
186 void Scheduler::sleep(Thread *t)
188 set_enabled(t, THREAD_DISABLED);
189 t->set_state(THREAD_BLOCKED);
193 * Wake a Thread up that was previously waiting (see Scheduler::wait)
194 * @param t The Thread to wake up
196 void Scheduler::wake(Thread *t)
198 ASSERT(!t->is_model_thread());
199 set_enabled(t, THREAD_ENABLED);
200 t->set_state(THREAD_READY);
204 * @brief Select a Thread to run via round-robin
207 * @return The next Thread to run
209 Thread * Scheduler::select_next_thread()
211 int avail_threads = 0;
212 int sleep_threads = 0;
213 int thread_list[enabled_len], sleep_list[enabled_len];
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;
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);
230 return NULL; // No threads available and no threads sleeping.
233 // Some threads are available
234 thread = execution->getFuzzer()->selectThread(thread_list, avail_threads);
237 //curr_thread_index = id_to_int(thread->get_id());
241 void Scheduler::set_scheduler_thread(thread_id_t tid) {
242 curr_thread_index=id_to_int(tid);
246 * @brief Set the current "running" Thread
247 * @param t Thread to run
249 void Scheduler::set_current_thread(Thread *t)
251 ASSERT(!t || !t->is_model_thread());
259 * @return The currently-running Thread
261 Thread * Scheduler::get_current_thread() const
263 ASSERT(!current || !current->is_model_thread());
268 * Print debugging information about the current state of the scheduler. Only
269 * prints something if debugging is enabled.
271 void Scheduler::print() const
273 int curr_id = current ? id_to_int(current->get_id()) : -1;
275 model_print("Scheduler: ");
276 for (int i = 0;i < enabled_len;i++) {
278 enabled_type_to_string(enabled[i], str);
279 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);