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
38 * @param t The Thread to check
39 * @return True if the Thread is currently enabled
41 bool Scheduler::is_enabled(Thread *t) const
43 int id = id_to_int(t->get_id());
44 return (id >= enabled_len) ? false : (enabled[id] != THREAD_DISABLED);
47 enabled_type_t Scheduler::get_enabled(Thread *t) {
48 int id = id_to_int(t->get_id());
49 ASSERT(id<enabled_len);
53 void Scheduler::update_sleep_set(Node *n) {
54 enabled_type_t *enabled_array=n->get_enabled_array();
55 for(int i=0;i<enabled_len;i++) {
56 if (enabled_array[i]==THREAD_SLEEP_SET) {
57 enabled[i]=THREAD_SLEEP_SET;
63 * Add a Thread to the sleep set.
64 * @param t The Thread to add
66 void Scheduler::add_sleep(Thread *t)
68 DEBUG("thread %d\n", id_to_int(t->get_id()));
69 set_enabled(t, THREAD_SLEEP_SET);
73 * Remove a Thread from the sleep set.
74 * @param t The Thread to remove
76 void Scheduler::remove_sleep(Thread *t)
78 DEBUG("thread %d\n", id_to_int(t->get_id()));
79 set_enabled(t, THREAD_ENABLED);
83 * Add a Thread to the scheduler's ready list.
84 * @param t The Thread to add
86 void Scheduler::add_thread(Thread *t)
88 DEBUG("thread %d\n", id_to_int(t->get_id()));
89 ASSERT(!t->is_model_thread());
90 set_enabled(t, THREAD_ENABLED);
94 * Remove a given Thread from the scheduler.
95 * @param t The Thread to remove
97 void Scheduler::remove_thread(Thread *t)
101 set_enabled(t, THREAD_DISABLED);
105 * Prevent a Thread from being scheduled. The sleeping Thread should be
106 * re-awoken via Scheduler::wake.
107 * @param thread The Thread that should sleep
109 void Scheduler::sleep(Thread *t)
111 set_enabled(t, THREAD_DISABLED);
112 t->set_state(THREAD_BLOCKED);
116 * Wake a Thread up that was previously waiting (see Scheduler::wait)
117 * @param t The Thread to wake up
119 void Scheduler::wake(Thread *t)
121 ASSERT(!t->is_model_thread());
122 set_enabled(t, THREAD_ENABLED);
123 t->set_state(THREAD_READY);
127 * Select a Thread. This implementation defaults to round-robin, if a
128 * thread is not already provided.
130 * @param t Thread to run, if chosen by an external entity (e.g.,
131 * ModelChecker). May be NULL to indicate no external choice.
132 * @return The next Thread to run
134 Thread * Scheduler::next_thread(Thread *t)
137 int old_curr_thread = curr_thread_index;
138 bool have_enabled_thread_with_priority=false;
139 Node *n=model->get_curr_node();
141 for(int i=0;i<enabled_len;i++) {
142 thread_id_t tid=int_to_id(i);
143 if (n->has_priority(tid)) {
144 //Have a thread with priority
145 if (enabled[i]!=THREAD_DISABLED)
146 have_enabled_thread_with_priority=true;
151 curr_thread_index = (curr_thread_index+1) % enabled_len;
152 thread_id_t curr_tid=int_to_id(curr_thread_index);
153 if (enabled[curr_thread_index]==THREAD_ENABLED&&
154 (!have_enabled_thread_with_priority||n->has_priority(curr_tid))) {
155 t = model->get_thread(curr_tid);
158 if (curr_thread_index == old_curr_thread) {
163 } else if (t->is_model_thread()) {
164 /* model-checker threads never run */
167 curr_thread_index = id_to_int(t->get_id());
176 * @return The currently-running Thread
178 Thread * Scheduler::get_current_thread() const
180 ASSERT(!current || !current->is_model_thread());
185 * Print debugging information about the current state of the scheduler. Only
186 * prints something if debugging is enabled.
188 void Scheduler::print() const
191 DEBUG("Current thread: %d\n", id_to_int(current->get_id()));
193 DEBUG("No current thread\n");