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 memcpy(new_enabled, is_enabled, enabled_len*sizeof(bool));
20 memset(&new_enabled[enabled_len], 0, (threadid+1-enabled_len)*sizeof(bool));
22 is_enabled=new_enabled;
23 enabled_len=threadid+1;
25 is_enabled[threadid]=enabled_status;
29 * Add a Thread to the scheduler's ready list.
30 * @param t The Thread to add
32 void Scheduler::add_thread(Thread *t)
34 DEBUG("thread %d\n", t->get_id());
39 * Remove a given Thread from the scheduler.
40 * @param t The Thread to remove
42 void Scheduler::remove_thread(Thread *t)
46 set_enabled(t, false);
50 * Prevent a Thread from being scheduled. The sleeping Thread should be
51 * re-awoken via Scheduler::wake.
52 * @param thread The Thread that should sleep
54 void Scheduler::sleep(Thread *t)
56 set_enabled(t, false);
57 t->set_state(THREAD_BLOCKED);
61 * Wake a Thread up that was previously waiting (see Scheduler::wait)
62 * @param t The Thread to wake up
64 void Scheduler::wake(Thread *t)
67 t->set_state(THREAD_READY);
71 * Remove one Thread from the scheduler. This implementation defaults to FIFO,
72 * if a thread is not already provided.
74 * @param t Thread to run, if chosen by an external entity (e.g.,
75 * ModelChecker). May be NULL to indicate no external choice.
76 * @return The next Thread to run
78 Thread * Scheduler::next_thread(Thread *t)
81 int old_curr_thread = curr_thread_index;
83 curr_thread_index = (curr_thread_index+1) % enabled_len;
84 if (is_enabled[curr_thread_index]) {
85 t = model->get_thread(int_to_id(curr_thread_index));
88 if (curr_thread_index == old_curr_thread) {
100 * @return The currently-running Thread
102 Thread * Scheduler::get_current_thread() const
108 * Print debugging information about the current state of the scheduler. Only
109 * prints something if debugging is enabled.
111 void Scheduler::print() const
114 DEBUG("Current thread: %d\n", current->get_id());
116 DEBUG("No current thread\n");