83616173c2c6c8878817162d8af8c20733766a1c
[c11tester.git] / schedule.cc
1 #include <string.h>
2 #include <stdlib.h>
3
4 #include "threads-model.h"
5 #include "schedule.h"
6 #include "common.h"
7 #include "model.h"
8 #include "nodestack.h"
9 #include "execution.h"
10
11 /**
12  * Format an "enabled_type_t" for printing
13  * @param e The type to format
14  * @param str The output character array
15  */
16 void enabled_type_to_string(enabled_type_t e, char *str)
17 {
18         const char *res;
19         switch (e) {
20         case THREAD_DISABLED:
21                 res = "disabled";
22                 break;
23         case THREAD_ENABLED:
24                 res = "enabled";
25                 break;
26         case THREAD_SLEEP_SET:
27                 res = "sleep";
28                 break;
29         default:
30                 ASSERT(0);
31                 res = NULL;
32                 break;
33         }
34         strcpy(str, res);
35 }
36
37 /** Constructor */
38 Scheduler::Scheduler() :
39         execution(NULL),
40         enabled(NULL),
41         enabled_len(0),
42         curr_thread_index(0),
43         current(NULL)
44 {
45 }
46
47 /**
48  * @brief Register the ModelExecution engine
49  * @param execution The ModelExecution which is controlling execution
50  */
51 void Scheduler::register_engine(ModelExecution *execution)
52 {
53         this->execution = execution;
54 }
55
56 void Scheduler::set_enabled(Thread *t, enabled_type_t enabled_status) {
57         int threadid = id_to_int(t->get_id());
58         if (threadid >= enabled_len) {
59                 enabled_type_t *new_enabled = (enabled_type_t *)snapshot_malloc(sizeof(enabled_type_t) * (threadid + 1));
60                 memset(&new_enabled[enabled_len], 0, (threadid + 1 - enabled_len) * sizeof(enabled_type_t));
61                 if (enabled != NULL) {
62                         memcpy(new_enabled, enabled, enabled_len * sizeof(enabled_type_t));
63                         snapshot_free(enabled);
64                 }
65                 enabled = new_enabled;
66                 enabled_len = threadid + 1;
67         }
68         enabled[threadid] = enabled_status;
69 }
70
71 /**
72  * @brief Check if a Thread is currently enabled
73  *
74  * Check if a Thread is currently enabled. "Enabled" includes both
75  * THREAD_ENABLED and THREAD_SLEEP_SET.
76  * @param t The Thread to check
77  * @return True if the Thread is currently enabled
78  */
79 bool Scheduler::is_enabled(const Thread *t) const
80 {
81         return is_enabled(t->get_id());
82 }
83
84 /**
85  * @brief Check if a Thread is currently enabled
86  *
87  * Check if a Thread is currently enabled. "Enabled" includes both
88  * THREAD_ENABLED and THREAD_SLEEP_SET.
89  * @param tid The ID of the Thread to check
90  * @return True if the Thread is currently enabled
91  */
92 bool Scheduler::is_enabled(thread_id_t tid) const
93 {
94         int i = id_to_int(tid);
95         return (i >= enabled_len) ? false : (enabled[i] != THREAD_DISABLED);
96 }
97
98 /**
99  * @brief Check if a Thread is currently in the sleep set
100  * @param t The Thread to check
101  * @return True if the Thread is currently enabled
102  */
103 bool Scheduler::is_sleep_set(const Thread *t) const
104 {
105         return get_enabled(t) == THREAD_SLEEP_SET;
106 }
107
108 /**
109  * @brief Check if execution is stuck with no enabled threads and some sleeping
110  * thread
111  * @return True if no threads are enabled an some thread is in the sleep set;
112  * false otherwise
113  */
114 bool Scheduler::all_threads_sleeping() const
115 {
116         bool sleeping = false;
117         for (int i = 0; i < enabled_len; i++)
118                 if (enabled[i] == THREAD_ENABLED)
119                         return false;
120                 else if (enabled[i] == THREAD_SLEEP_SET)
121                         sleeping = true;
122         return sleeping;
123 }
124
125 enabled_type_t Scheduler::get_enabled(const Thread *t) const
126 {
127         int id = id_to_int(t->get_id());
128         ASSERT(id < enabled_len);
129         return enabled[id];
130 }
131
132 /**
133  * Add a Thread to the sleep set.
134  * @param t The Thread to add
135  */
136 void Scheduler::add_sleep(Thread *t)
137 {
138         DEBUG("thread %d\n", id_to_int(t->get_id()));
139         set_enabled(t, THREAD_SLEEP_SET);
140 }
141
142 /**
143  * Remove a Thread from the sleep set.
144  * @param t The Thread to remove
145  */
146 void Scheduler::remove_sleep(Thread *t)
147 {
148         DEBUG("thread %d\n", id_to_int(t->get_id()));
149         set_enabled(t, THREAD_ENABLED);
150 }
151
152 /**
153  * Add a Thread to the scheduler's ready list.
154  * @param t The Thread to add
155  */
156 void Scheduler::add_thread(Thread *t)
157 {
158         DEBUG("thread %d\n", id_to_int(t->get_id()));
159         ASSERT(!t->is_model_thread());
160         set_enabled(t, THREAD_ENABLED);
161 }
162
163 /**
164  * Remove a given Thread from the scheduler.
165  * @param t The Thread to remove
166  */
167 void Scheduler::remove_thread(Thread *t)
168 {
169         if (current == t)
170                 current = NULL;
171         set_enabled(t, THREAD_DISABLED);
172 }
173
174 /**
175  * Prevent a Thread from being scheduled. The sleeping Thread should be
176  * re-awoken via Scheduler::wake.
177  * @param thread The Thread that should sleep
178  */
179 void Scheduler::sleep(Thread *t)
180 {
181         set_enabled(t, THREAD_DISABLED);
182         t->set_state(THREAD_BLOCKED);
183 }
184
185 /**
186  * Wake a Thread up that was previously waiting (see Scheduler::wait)
187  * @param t The Thread to wake up
188  */
189 void Scheduler::wake(Thread *t)
190 {
191         ASSERT(!t->is_model_thread());
192         set_enabled(t, THREAD_ENABLED);
193         t->set_state(THREAD_READY);
194 }
195
196 /**
197  * @brief Select a Thread to run via round-robin
198  *
199  * @param n The current Node, holding priority information for the next thread
200  * selection
201  *
202  * @return The next Thread to run
203  */
204 Thread * Scheduler::select_next_thread(Node *n)
205 {
206         int avail_threads = enabled_len;        // number of available threads
207         int thread_list[enabled_len];   // keep a list of threads to select from
208         for (int i = 0; i< enabled_len; i++){
209                 thread_list[i] = i;
210         }
211
212         while (avail_threads > 0) {
213                 int random_index = random() % avail_threads;
214                 curr_thread_index = thread_list[random_index];  // randomly select a thread from available threads
215                 
216                 // curr_thread_index = (curr_thread_index + i + 1) % enabled_len;
217                 thread_id_t curr_tid = int_to_id(curr_thread_index);
218                 
219                 if (enabled[curr_thread_index] == THREAD_ENABLED) {
220                         return model->get_thread(curr_tid);
221                 } else {        // remove this threads from available threads 
222                         thread_list[random_index] = thread_list[avail_threads - 1]; 
223                         avail_threads--;
224                 }
225         }
226         
227         /* No thread was enabled */
228         return NULL;
229 }
230
231 void Scheduler::set_scheduler_thread(thread_id_t tid) {
232         curr_thread_index=id_to_int(tid);
233 }
234
235 /**
236  * @brief Set the current "running" Thread
237  * @param t Thread to run
238  */
239 void Scheduler::set_current_thread(Thread *t)
240 {
241         ASSERT(!t || !t->is_model_thread());
242
243         current = t;
244         if (DBG_ENABLED())
245                 print();
246 }
247
248 /**
249  * @return The currently-running Thread
250  */
251 Thread * Scheduler::get_current_thread() const
252 {
253         ASSERT(!current || !current->is_model_thread());
254         return current;
255 }
256
257 /**
258  * Print debugging information about the current state of the scheduler. Only
259  * prints something if debugging is enabled.
260  */
261 void Scheduler::print() const
262 {
263         int curr_id = current ? id_to_int(current->get_id()) : -1;
264
265         model_print("Scheduler: ");
266         for (int i = 0; i < enabled_len; i++) {
267                 char str[20];
268                 enabled_type_to_string(enabled[i], str);
269                 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);
270         }
271         model_print("\n");
272 }