4 #include "threads-model.h"
11 * Format an "enabled_type_t" for printing
12 * @param e The type to format
13 * @param str The output character array
15 void enabled_type_to_string(enabled_type_t e, char *str)
25 case THREAD_SLEEP_SET:
37 Scheduler::Scheduler() :
45 void Scheduler::set_enabled(Thread *t, enabled_type_t enabled_status) {
46 int threadid = id_to_int(t->get_id());
47 if (threadid >= enabled_len) {
48 enabled_type_t *new_enabled = (enabled_type_t *)snapshot_malloc(sizeof(enabled_type_t) * (threadid + 1));
49 memset(&new_enabled[enabled_len], 0, (threadid + 1 - enabled_len) * sizeof(enabled_type_t));
50 if (enabled != NULL) {
51 memcpy(new_enabled, enabled, enabled_len * sizeof(enabled_type_t));
52 snapshot_free(enabled);
54 enabled = new_enabled;
55 enabled_len = threadid + 1;
57 enabled[threadid] = enabled_status;
58 if (enabled_status == THREAD_DISABLED)
59 model->check_promises_thread_disabled();
63 * @brief Check if a Thread is currently enabled
65 * Check if a Thread is currently enabled. "Enabled" includes both
66 * THREAD_ENABLED and THREAD_SLEEP_SET.
67 * @param t The Thread to check
68 * @return True if the Thread is currently enabled
70 bool Scheduler::is_enabled(const Thread *t) const
72 return is_enabled(t->get_id());
76 * @brief Check if a Thread is currently enabled
78 * Check if a Thread is currently enabled. "Enabled" includes both
79 * THREAD_ENABLED and THREAD_SLEEP_SET.
80 * @param tid The ID of the Thread to check
81 * @return True if the Thread is currently enabled
83 bool Scheduler::is_enabled(thread_id_t tid) const
85 int i = id_to_int(tid);
86 return (i >= enabled_len) ? false : (enabled[i] != THREAD_DISABLED);
90 * @brief Check if a Thread is currently in the sleep set
91 * @param t The Thread to check
92 * @return True if the Thread is currently enabled
94 bool Scheduler::is_sleep_set(const Thread *t) const
96 return get_enabled(t) == THREAD_SLEEP_SET;
100 * @brief Check if execution is stuck with no enabled threads and some sleeping
102 * @return True if no threads are enabled an some thread is in the sleep set;
105 bool Scheduler::all_threads_sleeping() const
107 bool sleeping = false;
108 for (int i = 0; i < enabled_len; i++)
109 if (enabled[i] == THREAD_ENABLED)
111 else if (enabled[i] == THREAD_SLEEP_SET)
116 enabled_type_t Scheduler::get_enabled(const Thread *t) const
118 int id = id_to_int(t->get_id());
119 ASSERT(id < enabled_len);
123 void Scheduler::update_sleep_set(Node *n) {
124 enabled_type_t *enabled_array = n->get_enabled_array();
125 for (int i = 0; i < enabled_len; i++) {
126 if (enabled_array[i] == THREAD_SLEEP_SET) {
127 enabled[i] = THREAD_SLEEP_SET;
133 * Add a Thread to the sleep set.
134 * @param t The Thread to add
136 void Scheduler::add_sleep(Thread *t)
138 DEBUG("thread %d\n", id_to_int(t->get_id()));
139 set_enabled(t, THREAD_SLEEP_SET);
143 * Remove a Thread from the sleep set.
144 * @param t The Thread to remove
146 void Scheduler::remove_sleep(Thread *t)
148 DEBUG("thread %d\n", id_to_int(t->get_id()));
149 set_enabled(t, THREAD_ENABLED);
153 * Add a Thread to the scheduler's ready list.
154 * @param t The Thread to add
156 void Scheduler::add_thread(Thread *t)
158 DEBUG("thread %d\n", id_to_int(t->get_id()));
159 ASSERT(!t->is_model_thread());
160 set_enabled(t, THREAD_ENABLED);
164 * Remove a given Thread from the scheduler.
165 * @param t The Thread to remove
167 void Scheduler::remove_thread(Thread *t)
171 set_enabled(t, THREAD_DISABLED);
175 * Prevent a Thread from being scheduled. The sleeping Thread should be
176 * re-awoken via Scheduler::wake.
177 * @param thread The Thread that should sleep
179 void Scheduler::sleep(Thread *t)
181 set_enabled(t, THREAD_DISABLED);
182 t->set_state(THREAD_BLOCKED);
186 * Wake a Thread up that was previously waiting (see Scheduler::wait)
187 * @param t The Thread to wake up
189 void Scheduler::wake(Thread *t)
191 ASSERT(!t->is_model_thread());
192 set_enabled(t, THREAD_ENABLED);
193 t->set_state(THREAD_READY);
197 * @brief Select a Thread to run via round-robin
198 * @return The next Thread to run
200 Thread * Scheduler::select_next_thread()
202 int old_curr_thread = curr_thread_index;
203 Node *n = model->get_curr_node();
205 bool have_enabled_thread_with_priority = false;
206 if (model->params.fairwindow != 0) {
207 for (int i = 0; i < enabled_len; i++) {
208 thread_id_t tid = int_to_id(i);
209 if (n->has_priority(tid)) {
210 DEBUG("Node (tid %d) has priority\n", i);
211 if (enabled[i] != THREAD_DISABLED)
212 have_enabled_thread_with_priority = true;
217 for (int i = 0; i < enabled_len; i++) {
218 curr_thread_index = (old_curr_thread + i + 1) % enabled_len;
219 thread_id_t curr_tid = int_to_id(curr_thread_index);
220 if (model->params.yieldon) {
221 bool bad_thread = false;
222 for (int j = 0; j < enabled_len; j++) {
223 thread_id_t tother = int_to_id(j);
224 if ((enabled[j] != THREAD_DISABLED) && n->has_priority_over(curr_tid, tother)) {
233 if (enabled[curr_thread_index] == THREAD_ENABLED &&
234 (!have_enabled_thread_with_priority || n->has_priority(curr_tid))) {
235 return model->get_thread(curr_tid);
239 /* No thread was enabled */
243 void Scheduler::set_scheduler_thread(thread_id_t tid) {
244 curr_thread_index=id_to_int(tid);
248 * @brief Set the current "running" Thread
249 * @param t Thread to run
251 void Scheduler::set_current_thread(Thread *t)
253 ASSERT(!t || !t->is_model_thread());
261 * @return The currently-running Thread
263 Thread * Scheduler::get_current_thread() const
265 ASSERT(!current || !current->is_model_thread());
270 * Print debugging information about the current state of the scheduler. Only
271 * prints something if debugging is enabled.
273 void Scheduler::print() const
275 int curr_id = current ? id_to_int(current->get_id()) : -1;
277 model_print("Scheduler: ");
278 for (int i = 0; i < enabled_len; i++) {
280 enabled_type_to_string(enabled[i], str);
281 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);