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 get_enabled(t) == THREAD_SLEEP_SET;
109 * @brief Check if execution is stuck with no enabled threads and some sleeping
111 * @return True if no threads are enabled an some thread is in the sleep set;
114 bool Scheduler::all_threads_sleeping() const
116 bool sleeping = false;
117 for (int i = 0; i < enabled_len; i++)
118 if (enabled[i] == THREAD_ENABLED)
120 else if (enabled[i] == THREAD_SLEEP_SET)
125 enabled_type_t Scheduler::get_enabled(const Thread *t) const
127 int id = id_to_int(t->get_id());
128 ASSERT(id < enabled_len);
132 void Scheduler::update_sleep_set(Node *n) {
133 enabled_type_t *enabled_array = n->get_enabled_array();
134 for (int i = 0; i < enabled_len; i++) {
135 if (enabled_array[i] == THREAD_SLEEP_SET) {
136 enabled[i] = THREAD_SLEEP_SET;
142 * Add a Thread to the sleep set.
143 * @param t The Thread to add
145 void Scheduler::add_sleep(Thread *t)
147 DEBUG("thread %d\n", id_to_int(t->get_id()));
148 set_enabled(t, THREAD_SLEEP_SET);
152 * Remove a Thread from the sleep set.
153 * @param t The Thread to remove
155 void Scheduler::remove_sleep(Thread *t)
157 DEBUG("thread %d\n", id_to_int(t->get_id()));
158 set_enabled(t, THREAD_ENABLED);
162 * Add a Thread to the scheduler's ready list.
163 * @param t The Thread to add
165 void Scheduler::add_thread(Thread *t)
167 DEBUG("thread %d\n", id_to_int(t->get_id()));
168 ASSERT(!t->is_model_thread());
169 set_enabled(t, THREAD_ENABLED);
173 * Remove a given Thread from the scheduler.
174 * @param t The Thread to remove
176 void Scheduler::remove_thread(Thread *t)
180 set_enabled(t, THREAD_DISABLED);
184 * Prevent a Thread from being scheduled. The sleeping Thread should be
185 * re-awoken via Scheduler::wake.
186 * @param thread The Thread that should sleep
188 void Scheduler::sleep(Thread *t)
190 set_enabled(t, THREAD_DISABLED);
191 t->set_state(THREAD_BLOCKED);
195 * Wake a Thread up that was previously waiting (see Scheduler::wait)
196 * @param t The Thread to wake up
198 void Scheduler::wake(Thread *t)
200 ASSERT(!t->is_model_thread());
201 set_enabled(t, THREAD_ENABLED);
202 t->set_state(THREAD_READY);
206 * @brief Select a Thread to run via round-robin
208 * @param n The current Node, holding priority information for the next thread
211 * @return The next Thread to run
213 Thread * Scheduler::select_next_thread(Node *n)
215 bool have_enabled_thread_with_priority = false;
216 if (model->params.fairwindow != 0) {
217 for (int i = 0; i < enabled_len; i++) {
218 thread_id_t tid = int_to_id(i);
219 if (n->has_priority(tid)) {
220 DEBUG("Node (tid %d) has priority\n", i);
221 if (enabled[i] != THREAD_DISABLED)
222 have_enabled_thread_with_priority = true;
227 int avail_threads = enabled_len; // number of available threads
228 int thread_list[enabled_len]; // keep a list of threads to select from
229 for (int i = 0; i< enabled_len; i++){
233 while (avail_threads > 0) {
234 int random_index = rand() % avail_threads;
235 curr_thread_index = thread_list[random_index]; // randomly select a thread from available threads
237 // curr_thread_index = (curr_thread_index + i + 1) % enabled_len;
238 thread_id_t curr_tid = int_to_id(curr_thread_index);
239 if (model->params.yieldon) {
240 bool bad_thread = false;
241 for (int j = 0; j < enabled_len; j++) {
242 thread_id_t tother = int_to_id(j);
243 if ((enabled[j] != THREAD_DISABLED) && n->has_priority_over(curr_tid, tother)) {
249 thread_list[random_index] = thread_list[avail_threads - 1]; // remove this threads from available threads
256 if (enabled[curr_thread_index] == THREAD_ENABLED &&
257 (!have_enabled_thread_with_priority || n->has_priority(curr_tid))) {
258 return model->get_thread(curr_tid);
259 } else { // remove this threads from available threads
260 thread_list[random_index] = thread_list[avail_threads - 1];
265 /* No thread was enabled */
269 void Scheduler::set_scheduler_thread(thread_id_t tid) {
270 curr_thread_index=id_to_int(tid);
274 * @brief Set the current "running" Thread
275 * @param t Thread to run
277 void Scheduler::set_current_thread(Thread *t)
279 ASSERT(!t || !t->is_model_thread());
287 * @return The currently-running Thread
289 Thread * Scheduler::get_current_thread() const
291 ASSERT(!current || !current->is_model_thread());
296 * Print debugging information about the current state of the scheduler. Only
297 * prints something if debugging is enabled.
299 void Scheduler::print() const
301 int curr_id = current ? id_to_int(current->get_id()) : -1;
303 model_print("Scheduler: ");
304 for (int i = 0; i < enabled_len; i++) {
306 enabled_type_to_string(enabled[i], str);
307 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);