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 (enabled != NULL) {
24 memcpy(new_enabled, enabled, enabled_len*sizeof(enabled_type_t));
25 snapshot_free(enabled);
28 enabled_len=threadid+1;
30 enabled[threadid]=enabled_status;
34 * @brief Check if a Thread is currently enabled
35 * @param t The Thread to check
36 * @return True if the Thread is currently enabled
38 bool Scheduler::is_enabled(Thread *t) const
40 int id = id_to_int(t->get_id());
41 return (id >= enabled_len) ? false : (enabled[id] == THREAD_ENABLED);
45 * Add a Thread to the scheduler's ready list.
46 * @param t The Thread to add
48 void Scheduler::add_thread(Thread *t)
50 DEBUG("thread %d\n", id_to_int(t->get_id()));
51 set_enabled(t, THREAD_ENABLED);
55 * Remove a given Thread from the scheduler.
56 * @param t The Thread to remove
58 void Scheduler::remove_thread(Thread *t)
62 set_enabled(t, THREAD_DISABLED);
66 * Prevent a Thread from being scheduled. The sleeping Thread should be
67 * re-awoken via Scheduler::wake.
68 * @param thread The Thread that should sleep
70 void Scheduler::sleep(Thread *t)
72 set_enabled(t, THREAD_DISABLED);
73 t->set_state(THREAD_BLOCKED);
77 * Wake a Thread up that was previously waiting (see Scheduler::wait)
78 * @param t The Thread to wake up
80 void Scheduler::wake(Thread *t)
82 set_enabled(t, THREAD_DISABLED);
83 t->set_state(THREAD_READY);
87 * Select a Thread. This implementation defaults to round-robin, if a
88 * thread is not already provided.
90 * @param t Thread to run, if chosen by an external entity (e.g.,
91 * ModelChecker). May be NULL to indicate no external choice.
92 * @return The next Thread to run
94 Thread * Scheduler::next_thread(Thread *t)
97 int old_curr_thread = curr_thread_index;
99 curr_thread_index = (curr_thread_index+1) % enabled_len;
100 if (enabled[curr_thread_index]) {
101 t = model->get_thread(int_to_id(curr_thread_index));
104 if (curr_thread_index == old_curr_thread) {
110 curr_thread_index = id_to_int(t->get_id());
119 * @return The currently-running Thread
121 Thread * Scheduler::get_current_thread() const
127 * Print debugging information about the current state of the scheduler. Only
128 * prints something if debugging is enabled.
130 void Scheduler::print() const
133 DEBUG("Current thread: %d\n", id_to_int(current->get_id()));
135 DEBUG("No current thread\n");