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;
69 if (enabled_status == THREAD_DISABLED)
70 execution->check_promises_thread_disabled();
74 * @brief Check if a Thread is currently enabled
76 * Check if a Thread is currently enabled. "Enabled" includes both
77 * THREAD_ENABLED and THREAD_SLEEP_SET.
78 * @param t The Thread to check
79 * @return True if the Thread is currently enabled
81 bool Scheduler::is_enabled(const Thread *t) const
83 return is_enabled(t->get_id());
87 * @brief Check if a Thread is currently enabled
89 * Check if a Thread is currently enabled. "Enabled" includes both
90 * THREAD_ENABLED and THREAD_SLEEP_SET.
91 * @param tid The ID of the Thread to check
92 * @return True if the Thread is currently enabled
94 bool Scheduler::is_enabled(thread_id_t tid) const
96 int i = id_to_int(tid);
97 return (i >= enabled_len) ? false : (enabled[i] != THREAD_DISABLED);
101 * @brief Check if a Thread is currently in the sleep set
102 * @param t The Thread to check
103 * @return True if the Thread is currently enabled
105 bool Scheduler::is_sleep_set(const Thread *t) const
107 return get_enabled(t) == THREAD_SLEEP_SET;
111 * @brief Check if execution is stuck with no enabled threads and some sleeping
113 * @return True if no threads are enabled an some thread is in the sleep set;
116 bool Scheduler::all_threads_sleeping() const
118 bool sleeping = false;
119 for (int i = 0; i < enabled_len; i++)
120 if (enabled[i] == THREAD_ENABLED)
122 else if (enabled[i] == THREAD_SLEEP_SET)
127 enabled_type_t Scheduler::get_enabled(const Thread *t) const
129 int id = id_to_int(t->get_id());
130 ASSERT(id < enabled_len);
134 void Scheduler::update_sleep_set(Node *n) {
135 enabled_type_t *enabled_array = n->get_enabled_array();
136 for (int i = 0; i < enabled_len; i++) {
137 if (enabled_array[i] == THREAD_SLEEP_SET) {
138 enabled[i] = THREAD_SLEEP_SET;
144 * Add a Thread to the sleep set.
145 * @param t The Thread to add
147 void Scheduler::add_sleep(Thread *t)
149 DEBUG("thread %d\n", id_to_int(t->get_id()));
150 set_enabled(t, THREAD_SLEEP_SET);
154 * Remove a Thread from the sleep set.
155 * @param t The Thread to remove
157 void Scheduler::remove_sleep(Thread *t)
159 DEBUG("thread %d\n", id_to_int(t->get_id()));
160 set_enabled(t, THREAD_ENABLED);
164 * Add a Thread to the scheduler's ready list.
165 * @param t The Thread to add
167 void Scheduler::add_thread(Thread *t)
169 DEBUG("thread %d\n", id_to_int(t->get_id()));
170 ASSERT(!t->is_model_thread());
171 set_enabled(t, THREAD_ENABLED);
175 * Remove a given Thread from the scheduler.
176 * @param t The Thread to remove
178 void Scheduler::remove_thread(Thread *t)
182 set_enabled(t, THREAD_DISABLED);
186 * Prevent a Thread from being scheduled. The sleeping Thread should be
187 * re-awoken via Scheduler::wake.
188 * @param thread The Thread that should sleep
190 void Scheduler::sleep(Thread *t)
192 set_enabled(t, THREAD_DISABLED);
193 t->set_state(THREAD_BLOCKED);
197 * Wake a Thread up that was previously waiting (see Scheduler::wait)
198 * @param t The Thread to wake up
200 void Scheduler::wake(Thread *t)
202 ASSERT(!t->is_model_thread());
203 set_enabled(t, THREAD_ENABLED);
204 t->set_state(THREAD_READY);
208 * @brief Select a Thread to run via round-robin
210 * @param n The current Node, holding priority information for the next thread
213 * @return The next Thread to run
215 Thread * Scheduler::select_next_thread(Node *n)
217 int old_curr_thread = curr_thread_index;
219 bool have_enabled_thread_with_priority = false;
220 if (model->params.fairwindow != 0) {
221 for (int i = 0; i < enabled_len; i++) {
222 thread_id_t tid = int_to_id(i);
223 if (n->has_priority(tid)) {
224 DEBUG("Node (tid %d) has priority\n", i);
225 if (enabled[i] != THREAD_DISABLED)
226 have_enabled_thread_with_priority = true;
231 for (int i = 0; i < enabled_len; i++) {
232 curr_thread_index = (old_curr_thread + i + 1) % enabled_len;
233 thread_id_t curr_tid = int_to_id(curr_thread_index);
234 if (model->params.yieldon) {
235 bool bad_thread = false;
236 for (int j = 0; j < enabled_len; j++) {
237 thread_id_t tother = int_to_id(j);
238 if ((enabled[j] != THREAD_DISABLED) && n->has_priority_over(curr_tid, tother)) {
247 if (enabled[curr_thread_index] == THREAD_ENABLED &&
248 (!have_enabled_thread_with_priority || n->has_priority(curr_tid))) {
249 return model->get_thread(curr_tid);
253 /* No thread was enabled */
257 void Scheduler::set_scheduler_thread(thread_id_t tid) {
258 curr_thread_index=id_to_int(tid);
262 * @brief Set the current "running" Thread
263 * @param t Thread to run
265 void Scheduler::set_current_thread(Thread *t)
267 ASSERT(!t || !t->is_model_thread());
275 * @return The currently-running Thread
277 Thread * Scheduler::get_current_thread() const
279 ASSERT(!current || !current->is_model_thread());
284 * Print debugging information about the current state of the scheduler. Only
285 * prints something if debugging is enabled.
287 void Scheduler::print() const
289 int curr_id = current ? id_to_int(current->get_id()) : -1;
291 model_print("Scheduler: ");
292 for (int i = 0; i < enabled_len; i++) {
294 enabled_type_to_string(enabled[i], str);
295 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);