4 #include "threads-model.h"
13 * Format an "enabled_type_t" for printing
14 * @param e The type to format
15 * @param str The output character array
17 void enabled_type_to_string(enabled_type_t e, char *str)
27 case THREAD_SLEEP_SET:
39 Scheduler::Scheduler() :
49 * @brief Register the ModelExecution engine
50 * @param execution The ModelExecution which is controlling execution
52 void Scheduler::register_engine(ModelExecution *execution)
54 this->execution = execution;
57 void Scheduler::set_enabled(Thread *t, enabled_type_t enabled_status) {
58 int threadid = id_to_int(t->get_id());
59 if (threadid >= enabled_len) {
60 enabled_type_t *new_enabled = (enabled_type_t *)snapshot_malloc(sizeof(enabled_type_t) * (threadid + 1));
61 memset(&new_enabled[enabled_len], 0, (threadid + 1 - enabled_len) * sizeof(enabled_type_t));
62 if (enabled != NULL) {
63 memcpy(new_enabled, enabled, enabled_len * sizeof(enabled_type_t));
64 snapshot_free(enabled);
66 enabled = new_enabled;
67 enabled_len = threadid + 1;
69 enabled[threadid] = enabled_status;
73 * @brief Check if a Thread is currently enabled
75 * Check if a Thread is currently enabled. "Enabled" includes both
76 * THREAD_ENABLED and THREAD_SLEEP_SET.
77 * @param t The Thread to check
78 * @return True if the Thread is currently enabled
80 bool Scheduler::is_enabled(const Thread *t) const
82 return is_enabled(t->get_id());
86 * @brief Check if a Thread is currently enabled
88 * Check if a Thread is currently enabled. "Enabled" includes both
89 * THREAD_ENABLED and THREAD_SLEEP_SET.
90 * @param tid The ID of the Thread to check
91 * @return True if the Thread is currently enabled
93 bool Scheduler::is_enabled(thread_id_t tid) const
95 int i = id_to_int(tid);
96 return (i >= enabled_len) ? false : (enabled[i] != THREAD_DISABLED);
100 * @brief Check if a Thread is currently in the sleep set
101 * @param t The Thread to check
102 * @return True if the Thread is currently enabled
104 bool Scheduler::is_sleep_set(const Thread *t) const
106 return get_enabled(t) == THREAD_SLEEP_SET;
110 * @brief Check if execution is stuck with no enabled threads and some sleeping
112 * @return True if no threads are enabled an some thread is in the sleep set;
115 bool Scheduler::all_threads_sleeping() const
117 bool sleeping = false;
118 for (int i = 0;i < enabled_len;i++)
119 if (enabled[i] == THREAD_ENABLED)
121 else if (enabled[i] == THREAD_SLEEP_SET)
126 enabled_type_t Scheduler::get_enabled(const Thread *t) const
128 int id = id_to_int(t->get_id());
129 ASSERT(id < enabled_len);
134 * Add a Thread to the sleep set.
135 * @param t The Thread to add
137 void Scheduler::add_sleep(Thread *t)
139 DEBUG("thread %d\n", id_to_int(t->get_id()));
140 set_enabled(t, THREAD_SLEEP_SET);
144 * Remove a Thread from the sleep set.
145 * @param t The Thread to remove
147 void Scheduler::remove_sleep(Thread *t)
149 DEBUG("thread %d\n", id_to_int(t->get_id()));
150 set_enabled(t, THREAD_ENABLED);
154 * Add a Thread to the scheduler's ready list.
155 * @param t The Thread to add
157 void Scheduler::add_thread(Thread *t)
159 DEBUG("thread %d\n", id_to_int(t->get_id()));
160 ASSERT(!t->is_model_thread());
161 set_enabled(t, THREAD_ENABLED);
165 * Remove a given Thread from the scheduler.
166 * @param t The Thread to remove
168 void Scheduler::remove_thread(Thread *t)
172 set_enabled(t, THREAD_DISABLED);
176 * Prevent a Thread from being scheduled. The sleeping Thread should be
177 * re-awoken via Scheduler::wake.
178 * @param thread The Thread that should sleep
180 void Scheduler::sleep(Thread *t)
182 set_enabled(t, THREAD_DISABLED);
183 t->set_state(THREAD_BLOCKED);
187 * Wake a Thread up that was previously waiting (see Scheduler::wait)
188 * @param t The Thread to wake up
190 void Scheduler::wake(Thread *t)
192 ASSERT(!t->is_model_thread());
193 set_enabled(t, THREAD_ENABLED);
194 t->set_state(THREAD_READY);
198 * @brief Select a Thread to run via round-robin
200 * @param n The current Node, holding priority information for the next thread
203 * @return The next Thread to run
205 Thread * Scheduler::select_next_thread(Node *n)
207 int avail_threads = 0;
208 int thread_list[enabled_len];
209 for (int i = 0;i< enabled_len;i++) {
210 if (enabled[i] == THREAD_ENABLED)
211 thread_list[avail_threads++] = i;
214 if (avail_threads == 0)
215 return NULL;// No threads availablex
217 Thread * thread = execution->getFuzzer()->selectThread(n, thread_list, avail_threads);
218 curr_thread_index = id_to_int(thread->get_id());
222 void Scheduler::set_scheduler_thread(thread_id_t tid) {
223 curr_thread_index=id_to_int(tid);
227 * @brief Set the current "running" Thread
228 * @param t Thread to run
230 void Scheduler::set_current_thread(Thread *t)
232 ASSERT(!t || !t->is_model_thread());
240 * @return The currently-running Thread
242 Thread * Scheduler::get_current_thread() const
244 ASSERT(!current || !current->is_model_thread());
249 * Print debugging information about the current state of the scheduler. Only
250 * prints something if debugging is enabled.
252 void Scheduler::print() const
254 int curr_id = current ? id_to_int(current->get_id()) : -1;
256 model_print("Scheduler: ");
257 for (int i = 0;i < enabled_len;i++) {
259 enabled_type_to_string(enabled[i], str);
260 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);