7 Scheduler::Scheduler() :
15 void Scheduler::set_enabled(Thread *t, bool enabled_status) {
16 int threadid=id_to_int(t->get_id());
17 if (threadid>=enabled_len) {
18 bool * new_enabled=(bool *)malloc(sizeof(bool)*(threadid+1));
19 memset(&new_enabled[enabled_len], 0, (threadid+1-enabled_len)*sizeof(bool));
20 if (is_enabled != NULL) {
21 memcpy(new_enabled, is_enabled, enabled_len*sizeof(bool));
24 is_enabled=new_enabled;
25 enabled_len=threadid+1;
27 is_enabled[threadid]=enabled_status;
31 * Add a Thread to the scheduler's ready list.
32 * @param t The Thread to add
34 void Scheduler::add_thread(Thread *t)
36 DEBUG("thread %d\n", t->get_id());
41 * Remove a given Thread from the scheduler.
42 * @param t The Thread to remove
44 void Scheduler::remove_thread(Thread *t)
48 set_enabled(t, false);
52 * Prevent a Thread from being scheduled. The sleeping Thread should be
53 * re-awoken via Scheduler::wake.
54 * @param thread The Thread that should sleep
56 void Scheduler::sleep(Thread *t)
58 set_enabled(t, false);
59 t->set_state(THREAD_BLOCKED);
63 * Wake a Thread up that was previously waiting (see Scheduler::wait)
64 * @param t The Thread to wake up
66 void Scheduler::wake(Thread *t)
69 t->set_state(THREAD_READY);
73 * Remove one Thread from the scheduler. This implementation defaults to FIFO,
74 * if a thread is not already provided.
76 * @param t Thread to run, if chosen by an external entity (e.g.,
77 * ModelChecker). May be NULL to indicate no external choice.
78 * @return The next Thread to run
80 Thread * Scheduler::next_thread(Thread *t)
83 int old_curr_thread = curr_thread_index;
85 curr_thread_index = (curr_thread_index+1) % enabled_len;
86 if (is_enabled[curr_thread_index]) {
87 t = model->get_thread(int_to_id(curr_thread_index));
90 if (curr_thread_index == old_curr_thread) {
96 curr_thread_index = id_to_int(t->get_id());
105 * @return The currently-running Thread
107 Thread * Scheduler::get_current_thread() const
113 * Print debugging information about the current state of the scheduler. Only
114 * prints something if debugging is enabled.
116 void Scheduler::print() const
119 DEBUG("Current thread: %d\n", current->get_id());
121 DEBUG("No current thread\n");