4 #include "threads-model.h"
11 Scheduler::Scheduler() :
19 void Scheduler::set_enabled(Thread *t, enabled_type_t enabled_status) {
20 int threadid = id_to_int(t->get_id());
21 if (threadid >= enabled_len) {
22 enabled_type_t *new_enabled = (enabled_type_t *)snapshot_malloc(sizeof(enabled_type_t) * (threadid + 1));
23 memset(&new_enabled[enabled_len], 0, (threadid + 1 - enabled_len) * sizeof(enabled_type_t));
24 if (enabled != NULL) {
25 memcpy(new_enabled, enabled, enabled_len * sizeof(enabled_type_t));
26 snapshot_free(enabled);
28 enabled = new_enabled;
29 enabled_len = threadid + 1;
31 enabled[threadid] = enabled_status;
32 if (enabled_status == THREAD_DISABLED)
33 model->check_promises_thread_disabled();
37 * @brief Check if a Thread is currently enabled
39 * Check if a Thread is currently enabled. "Enabled" includes both
40 * THREAD_ENABLED and THREAD_SLEEP_SET.
41 * @param t The Thread to check
42 * @return True if the Thread is currently enabled
44 bool Scheduler::is_enabled(const Thread *t) const
46 return is_enabled(t->get_id());
50 * @brief Check if a Thread is currently enabled
52 * Check if a Thread is currently enabled. "Enabled" includes both
53 * THREAD_ENABLED and THREAD_SLEEP_SET.
54 * @param tid The ID of the Thread to check
55 * @return True if the Thread is currently enabled
57 bool Scheduler::is_enabled(thread_id_t tid) const
59 int i = id_to_int(tid);
60 return (i >= enabled_len) ? false : (enabled[i] != THREAD_DISABLED);
64 * @brief Check if a Thread is currently in the sleep set
65 * @param t The Thread to check
66 * @return True if the Thread is currently enabled
68 bool Scheduler::is_sleep_set(const Thread *t) const
70 return get_enabled(t) == THREAD_SLEEP_SET;
73 enabled_type_t Scheduler::get_enabled(const Thread *t) const
75 int id = id_to_int(t->get_id());
76 ASSERT(id < enabled_len);
80 void Scheduler::update_sleep_set(Node *n) {
81 enabled_type_t *enabled_array = n->get_enabled_array();
82 for (int i = 0; i < enabled_len; i++) {
83 if (enabled_array[i] == THREAD_SLEEP_SET) {
84 enabled[i] = THREAD_SLEEP_SET;
90 * Add a Thread to the sleep set.
91 * @param t The Thread to add
93 void Scheduler::add_sleep(Thread *t)
95 DEBUG("thread %d\n", id_to_int(t->get_id()));
96 set_enabled(t, THREAD_SLEEP_SET);
100 * Remove a Thread from the sleep set.
101 * @param t The Thread to remove
103 void Scheduler::remove_sleep(Thread *t)
105 DEBUG("thread %d\n", id_to_int(t->get_id()));
106 set_enabled(t, THREAD_ENABLED);
110 * Add a Thread to the scheduler's ready list.
111 * @param t The Thread to add
113 void Scheduler::add_thread(Thread *t)
115 DEBUG("thread %d\n", id_to_int(t->get_id()));
116 ASSERT(!t->is_model_thread());
117 set_enabled(t, THREAD_ENABLED);
121 * Remove a given Thread from the scheduler.
122 * @param t The Thread to remove
124 void Scheduler::remove_thread(Thread *t)
128 set_enabled(t, THREAD_DISABLED);
132 * Prevent a Thread from being scheduled. The sleeping Thread should be
133 * re-awoken via Scheduler::wake.
134 * @param thread The Thread that should sleep
136 void Scheduler::sleep(Thread *t)
138 set_enabled(t, THREAD_DISABLED);
139 t->set_state(THREAD_BLOCKED);
143 * Wake a Thread up that was previously waiting (see Scheduler::wait)
144 * @param t The Thread to wake up
146 void Scheduler::wake(Thread *t)
148 ASSERT(!t->is_model_thread());
149 set_enabled(t, THREAD_ENABLED);
150 t->set_state(THREAD_READY);
154 * Select a Thread. This implementation defaults to round-robin, if a
155 * thread is not already provided.
157 * @param t Thread to run, if chosen by an external entity (e.g.,
158 * ModelChecker). May be NULL to indicate no external choice.
159 * @return The next Thread to run
161 Thread * Scheduler::next_thread(Thread *t)
164 int old_curr_thread = curr_thread_index;
165 bool have_enabled_thread_with_priority = false;
166 Node *n = model->get_curr_node();
168 for (int i = 0; i < enabled_len; i++) {
169 thread_id_t tid = int_to_id(i);
170 if (n->has_priority(tid)) {
171 DEBUG("Node (tid %d) has priority\n", i);
172 //Have a thread with priority
173 if (enabled[i] != THREAD_DISABLED)
174 have_enabled_thread_with_priority = true;
179 curr_thread_index = (curr_thread_index + 1) % enabled_len;
180 thread_id_t curr_tid = int_to_id(curr_thread_index);
181 if (enabled[curr_thread_index] == THREAD_ENABLED &&
182 (!have_enabled_thread_with_priority || n->has_priority(curr_tid))) {
183 t = model->get_thread(curr_tid);
186 if (curr_thread_index == old_curr_thread) {
191 } else if (t->is_model_thread()) {
192 /* model-checker threads never run */
195 curr_thread_index = id_to_int(t->get_id());
204 * @return The currently-running Thread
206 Thread * Scheduler::get_current_thread() const
208 ASSERT(!current || !current->is_model_thread());
213 * Print debugging information about the current state of the scheduler. Only
214 * prints something if debugging is enabled.
216 void Scheduler::print() const
219 DEBUG("Current thread: %d\n", id_to_int(current->get_id()));
221 DEBUG("No current thread\n");