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);
29 enabled_len=threadid+1;
31 enabled[threadid]=enabled_status;
35 * @brief Check if a Thread is currently enabled
36 * @param t The Thread to check
37 * @return True if the Thread is currently enabled
39 bool Scheduler::is_enabled(Thread *t) const
41 int id = id_to_int(t->get_id());
42 return (id >= enabled_len) ? false : (enabled[id] != THREAD_DISABLED);
45 enabled_type_t Scheduler::get_enabled(Thread *t) {
46 return enabled[id_to_int(t->get_id())];
49 void Scheduler::update_sleep_set(Node *n) {
50 enabled_type_t *enabled_array=n->get_enabled_array();
51 for(int i=0;i<enabled_len;i++) {
52 if (enabled_array[i]==THREAD_SLEEP_SET) {
53 enabled[i]=THREAD_SLEEP_SET;
59 * Add a Thread to the sleep set.
60 * @param t The Thread to add
62 void Scheduler::add_sleep(Thread *t)
64 DEBUG("thread %d\n", id_to_int(t->get_id()));
65 set_enabled(t, THREAD_SLEEP_SET);
69 * Remove a Thread from the sleep set.
70 * @param t The Thread to remove
72 void Scheduler::remove_sleep(Thread *t)
74 DEBUG("thread %d\n", id_to_int(t->get_id()));
75 set_enabled(t, THREAD_ENABLED);
79 * Add a Thread to the scheduler's ready list.
80 * @param t The Thread to add
82 void Scheduler::add_thread(Thread *t)
84 DEBUG("thread %d\n", id_to_int(t->get_id()));
85 ASSERT(!t->is_model_thread());
86 set_enabled(t, THREAD_ENABLED);
90 * Remove a given Thread from the scheduler.
91 * @param t The Thread to remove
93 void Scheduler::remove_thread(Thread *t)
97 set_enabled(t, THREAD_DISABLED);
101 * Prevent a Thread from being scheduled. The sleeping Thread should be
102 * re-awoken via Scheduler::wake.
103 * @param thread The Thread that should sleep
105 void Scheduler::sleep(Thread *t)
107 set_enabled(t, THREAD_DISABLED);
108 t->set_state(THREAD_BLOCKED);
112 * Wake a Thread up that was previously waiting (see Scheduler::wait)
113 * @param t The Thread to wake up
115 void Scheduler::wake(Thread *t)
117 ASSERT(!t->is_model_thread());
118 set_enabled(t, THREAD_ENABLED);
119 t->set_state(THREAD_READY);
123 * Select a Thread. This implementation defaults to round-robin, if a
124 * thread is not already provided.
126 * @param t Thread to run, if chosen by an external entity (e.g.,
127 * ModelChecker). May be NULL to indicate no external choice.
128 * @return The next Thread to run
130 Thread * Scheduler::next_thread(Thread *t)
133 int old_curr_thread = curr_thread_index;
134 bool have_enabled_thread_with_priority=false;
135 Node *n=model->get_curr_node();
137 for(int i=0;i<enabled_len;i++) {
138 thread_id_t tid=int_to_id(i);
139 if (n->has_priority(tid)) {
140 //Have a thread with priority
141 if (enabled[i]!=THREAD_DISABLED)
142 have_enabled_thread_with_priority=true;
147 curr_thread_index = (curr_thread_index+1) % enabled_len;
148 thread_id_t curr_tid=int_to_id(curr_thread_index);
149 if (enabled[curr_thread_index]==THREAD_ENABLED&&
150 (!have_enabled_thread_with_priority||n->has_priority(curr_tid))) {
151 t = model->get_thread(curr_tid);
154 if (curr_thread_index == old_curr_thread) {
159 } else if (t->is_model_thread()) {
160 /* model-checker threads never run */
163 curr_thread_index = id_to_int(t->get_id());
172 * @return The currently-running Thread
174 Thread * Scheduler::get_current_thread() const
176 ASSERT(!current || !current->is_model_thread());
181 * Print debugging information about the current state of the scheduler. Only
182 * prints something if debugging is enabled.
184 void Scheduler::print() const
187 DEBUG("Current thread: %d\n", id_to_int(current->get_id()));
189 DEBUG("No current thread\n");