10 Scheduler::Scheduler() :
18 void Scheduler::set_enabled(Thread *t, enabled_type_t enabled_status) {
19 int threadid=id_to_int(t->get_id());
20 if (threadid>=enabled_len) {
21 enabled_type_t *new_enabled = (enabled_type_t *)snapshot_malloc(sizeof(enabled_type_t) * (threadid + 1));
22 memset(&new_enabled[enabled_len], 0, (threadid+1-enabled_len)*sizeof(enabled_type_t));
23 if (is_enabled != NULL) {
24 memcpy(new_enabled, is_enabled, enabled_len*sizeof(enabled_type_t));
25 snapshot_free(is_enabled);
27 is_enabled=new_enabled;
28 enabled_len=threadid+1;
30 is_enabled[threadid]=enabled_status;
34 * Add a Thread to the scheduler's ready list.
35 * @param t The Thread to add
37 void Scheduler::add_thread(Thread *t)
39 DEBUG("thread %d\n", id_to_int(t->get_id()));
40 set_enabled(t, THREAD_ENABLED);
44 * Remove a given Thread from the scheduler.
45 * @param t The Thread to remove
47 void Scheduler::remove_thread(Thread *t)
51 set_enabled(t, THREAD_DISABLED);
55 * Prevent a Thread from being scheduled. The sleeping Thread should be
56 * re-awoken via Scheduler::wake.
57 * @param thread The Thread that should sleep
59 void Scheduler::sleep(Thread *t)
61 set_enabled(t, THREAD_DISABLED);
62 t->set_state(THREAD_BLOCKED);
66 * Wake a Thread up that was previously waiting (see Scheduler::wait)
67 * @param t The Thread to wake up
69 void Scheduler::wake(Thread *t)
71 set_enabled(t, THREAD_DISABLED);
72 t->set_state(THREAD_READY);
76 * Select a Thread. This implementation defaults to round-robin, if a
77 * thread is not already provided.
79 * @param t Thread to run, if chosen by an external entity (e.g.,
80 * ModelChecker). May be NULL to indicate no external choice.
81 * @return The next Thread to run
83 Thread * Scheduler::next_thread(Thread *t)
86 int old_curr_thread = curr_thread_index;
88 curr_thread_index = (curr_thread_index+1) % enabled_len;
89 if (is_enabled[curr_thread_index]) {
90 t = model->get_thread(int_to_id(curr_thread_index));
93 if (curr_thread_index == old_curr_thread) {
99 curr_thread_index = id_to_int(t->get_id());
108 * @return The currently-running Thread
110 Thread * Scheduler::get_current_thread() const
116 * Print debugging information about the current state of the scheduler. Only
117 * prints something if debugging is enabled.
119 void Scheduler::print() const
122 DEBUG("Current thread: %d\n", id_to_int(current->get_id()));
124 DEBUG("No current thread\n");