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;
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(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);
63 enabled_type_t Scheduler::get_enabled(Thread *t) {
64 int id = id_to_int(t->get_id());
65 ASSERT(id<enabled_len);
69 void Scheduler::update_sleep_set(Node *n) {
70 enabled_type_t *enabled_array=n->get_enabled_array();
71 for(int i=0;i<enabled_len;i++) {
72 if (enabled_array[i]==THREAD_SLEEP_SET) {
73 enabled[i]=THREAD_SLEEP_SET;
79 * Add a Thread to the sleep set.
80 * @param t The Thread to add
82 void Scheduler::add_sleep(Thread *t)
84 DEBUG("thread %d\n", id_to_int(t->get_id()));
85 set_enabled(t, THREAD_SLEEP_SET);
89 * Remove a Thread from the sleep set.
90 * @param t The Thread to remove
92 void Scheduler::remove_sleep(Thread *t)
94 DEBUG("thread %d\n", id_to_int(t->get_id()));
95 set_enabled(t, THREAD_ENABLED);
99 * Add a Thread to the scheduler's ready list.
100 * @param t The Thread to add
102 void Scheduler::add_thread(Thread *t)
104 DEBUG("thread %d\n", id_to_int(t->get_id()));
105 ASSERT(!t->is_model_thread());
106 set_enabled(t, THREAD_ENABLED);
110 * Remove a given Thread from the scheduler.
111 * @param t The Thread to remove
113 void Scheduler::remove_thread(Thread *t)
117 set_enabled(t, THREAD_DISABLED);
121 * Prevent a Thread from being scheduled. The sleeping Thread should be
122 * re-awoken via Scheduler::wake.
123 * @param thread The Thread that should sleep
125 void Scheduler::sleep(Thread *t)
127 set_enabled(t, THREAD_DISABLED);
128 t->set_state(THREAD_BLOCKED);
132 * Wake a Thread up that was previously waiting (see Scheduler::wait)
133 * @param t The Thread to wake up
135 void Scheduler::wake(Thread *t)
137 ASSERT(!t->is_model_thread());
138 set_enabled(t, THREAD_ENABLED);
139 t->set_state(THREAD_READY);
143 * Select a Thread. This implementation defaults to round-robin, if a
144 * thread is not already provided.
146 * @param t Thread to run, if chosen by an external entity (e.g.,
147 * ModelChecker). May be NULL to indicate no external choice.
148 * @return The next Thread to run
150 Thread * Scheduler::next_thread(Thread *t)
153 int old_curr_thread = curr_thread_index;
154 bool have_enabled_thread_with_priority=false;
155 Node *n=model->get_curr_node();
157 for(int i=0;i<enabled_len;i++) {
158 thread_id_t tid=int_to_id(i);
159 if (n->has_priority(tid)) {
160 //Have a thread with priority
161 if (enabled[i]!=THREAD_DISABLED)
162 have_enabled_thread_with_priority=true;
167 curr_thread_index = (curr_thread_index+1) % enabled_len;
168 thread_id_t curr_tid=int_to_id(curr_thread_index);
169 if (enabled[curr_thread_index]==THREAD_ENABLED&&
170 (!have_enabled_thread_with_priority||n->has_priority(curr_tid))) {
171 t = model->get_thread(curr_tid);
174 if (curr_thread_index == old_curr_thread) {
179 } else if (t->is_model_thread()) {
180 /* model-checker threads never run */
183 curr_thread_index = id_to_int(t->get_id());
192 * @return The currently-running Thread
194 Thread * Scheduler::get_current_thread() const
196 ASSERT(!current || !current->is_model_thread());
201 * Print debugging information about the current state of the scheduler. Only
202 * prints something if debugging is enabled.
204 void Scheduler::print() const
207 DEBUG("Current thread: %d\n", id_to_int(current->get_id()));
209 DEBUG("No current thread\n");