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 static 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;
99 enabled_type_t Scheduler::get_enabled(const Thread *t) const
101 int id = id_to_int(t->get_id());
102 ASSERT(id < enabled_len);
106 void Scheduler::update_sleep_set(Node *n) {
107 enabled_type_t *enabled_array = n->get_enabled_array();
108 for (int i = 0; i < enabled_len; i++) {
109 if (enabled_array[i] == THREAD_SLEEP_SET) {
110 enabled[i] = THREAD_SLEEP_SET;
116 * Add a Thread to the sleep set.
117 * @param t The Thread to add
119 void Scheduler::add_sleep(Thread *t)
121 DEBUG("thread %d\n", id_to_int(t->get_id()));
122 set_enabled(t, THREAD_SLEEP_SET);
126 * Remove a Thread from the sleep set.
127 * @param t The Thread to remove
129 void Scheduler::remove_sleep(Thread *t)
131 DEBUG("thread %d\n", id_to_int(t->get_id()));
132 set_enabled(t, THREAD_ENABLED);
136 * Add a Thread to the scheduler's ready list.
137 * @param t The Thread to add
139 void Scheduler::add_thread(Thread *t)
141 DEBUG("thread %d\n", id_to_int(t->get_id()));
142 ASSERT(!t->is_model_thread());
143 set_enabled(t, THREAD_ENABLED);
147 * Remove a given Thread from the scheduler.
148 * @param t The Thread to remove
150 void Scheduler::remove_thread(Thread *t)
154 set_enabled(t, THREAD_DISABLED);
158 * Prevent a Thread from being scheduled. The sleeping Thread should be
159 * re-awoken via Scheduler::wake.
160 * @param thread The Thread that should sleep
162 void Scheduler::sleep(Thread *t)
164 set_enabled(t, THREAD_DISABLED);
165 t->set_state(THREAD_BLOCKED);
169 * Wake a Thread up that was previously waiting (see Scheduler::wait)
170 * @param t The Thread to wake up
172 void Scheduler::wake(Thread *t)
174 ASSERT(!t->is_model_thread());
175 set_enabled(t, THREAD_ENABLED);
176 t->set_state(THREAD_READY);
180 * Select a Thread. This implementation defaults to round-robin, if a
181 * thread is not already provided.
183 * @param t Thread to run, if chosen by an external entity (e.g.,
184 * ModelChecker). May be NULL to indicate no external choice.
185 * @return The next Thread to run
187 Thread * Scheduler::next_thread(Thread *t)
190 int old_curr_thread = curr_thread_index;
191 bool have_enabled_thread_with_priority = false;
192 Node *n = model->get_curr_node();
194 for (int i = 0; i < enabled_len; i++) {
195 thread_id_t tid = int_to_id(i);
196 if (n->has_priority(tid)) {
197 DEBUG("Node (tid %d) has priority\n", i);
198 //Have a thread with priority
199 if (enabled[i] != THREAD_DISABLED)
200 have_enabled_thread_with_priority = true;
205 curr_thread_index = (curr_thread_index + 1) % enabled_len;
206 thread_id_t curr_tid = int_to_id(curr_thread_index);
207 if (enabled[curr_thread_index] == THREAD_ENABLED &&
208 (!have_enabled_thread_with_priority || n->has_priority(curr_tid))) {
209 t = model->get_thread(curr_tid);
212 if (curr_thread_index == old_curr_thread) {
218 } else if (t->is_model_thread()) {
219 /* model-checker threads never run */
222 curr_thread_index = id_to_int(t->get_id());
232 * @return The currently-running Thread
234 Thread * Scheduler::get_current_thread() const
236 ASSERT(!current || !current->is_model_thread());
241 * Print debugging information about the current state of the scheduler. Only
242 * prints something if debugging is enabled.
244 void Scheduler::print() const
246 int curr_id = current ? id_to_int(current->get_id()) : -1;
248 model_print("Scheduler: ");
249 for (int i = 0; i < enabled_len; i++) {
251 enabled_type_to_string(enabled[i], str);
252 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);