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 ASSERT(!t->is_model_thread());
52 set_enabled(t, THREAD_ENABLED);
56 * Remove a given Thread from the scheduler.
57 * @param t The Thread to remove
59 void Scheduler::remove_thread(Thread *t)
63 set_enabled(t, THREAD_DISABLED);
67 * Prevent a Thread from being scheduled. The sleeping Thread should be
68 * re-awoken via Scheduler::wake.
69 * @param thread The Thread that should sleep
71 void Scheduler::sleep(Thread *t)
73 set_enabled(t, THREAD_DISABLED);
74 t->set_state(THREAD_BLOCKED);
78 * Wake a Thread up that was previously waiting (see Scheduler::wait)
79 * @param t The Thread to wake up
81 void Scheduler::wake(Thread *t)
83 ASSERT(!t->is_model_thread());
84 set_enabled(t, THREAD_DISABLED);
85 t->set_state(THREAD_READY);
89 * Select a Thread. This implementation defaults to round-robin, if a
90 * thread is not already provided.
92 * @param t Thread to run, if chosen by an external entity (e.g.,
93 * ModelChecker). May be NULL to indicate no external choice.
94 * @return The next Thread to run
96 Thread * Scheduler::next_thread(Thread *t)
99 int old_curr_thread = curr_thread_index;
101 curr_thread_index = (curr_thread_index+1) % enabled_len;
102 if (enabled[curr_thread_index]) {
103 t = model->get_thread(int_to_id(curr_thread_index));
106 if (curr_thread_index == old_curr_thread) {
111 } else if (t->is_model_thread()) {
112 /* model-checker threads never run */
115 curr_thread_index = id_to_int(t->get_id());
124 * @return The currently-running Thread
126 Thread * Scheduler::get_current_thread() const
128 ASSERT(!current || !current->is_model_thread());
133 * Print debugging information about the current state of the scheduler. Only
134 * prints something if debugging is enabled.
136 void Scheduler::print() const
139 DEBUG("Current thread: %d\n", id_to_int(current->get_id()));
141 DEBUG("No current thread\n");