common: drop model_print_summary()
[cdsspec-compiler.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
10 /**
11  * Format an "enabled_type_t" for printing
12  * @param e The type to format
13  * @param str The output character array
14  */
15 void enabled_type_to_string(enabled_type_t e, char *str)
16 {
17         const char *res;
18         switch (e) {
19         case THREAD_DISABLED:
20                 res = "disabled";
21                 break;
22         case THREAD_ENABLED:
23                 res = "enabled";
24                 break;
25         case THREAD_SLEEP_SET:
26                 res = "sleep";
27                 break;
28         default:
29                 ASSERT(0);
30                 res = NULL;
31                 break;
32         }
33         strcpy(str, res);
34 }
35
36 /** Constructor */
37 Scheduler::Scheduler() :
38         enabled(NULL),
39         enabled_len(0),
40         curr_thread_index(0),
41         current(NULL)
42 {
43 }
44
45 void Scheduler::set_enabled(Thread *t, enabled_type_t enabled_status) {
46         int threadid = id_to_int(t->get_id());
47         if (threadid >= enabled_len) {
48                 enabled_type_t *new_enabled = (enabled_type_t *)snapshot_malloc(sizeof(enabled_type_t) * (threadid + 1));
49                 memset(&new_enabled[enabled_len], 0, (threadid + 1 - enabled_len) * sizeof(enabled_type_t));
50                 if (enabled != NULL) {
51                         memcpy(new_enabled, enabled, enabled_len * sizeof(enabled_type_t));
52                         snapshot_free(enabled);
53                 }
54                 enabled = new_enabled;
55                 enabled_len = threadid + 1;
56         }
57         enabled[threadid] = enabled_status;
58         if (enabled_status == THREAD_DISABLED)
59                 model->check_promises_thread_disabled();
60 }
61
62 /**
63  * @brief Check if a Thread is currently enabled
64  *
65  * Check if a Thread is currently enabled. "Enabled" includes both
66  * THREAD_ENABLED and THREAD_SLEEP_SET.
67  * @param t The Thread to check
68  * @return True if the Thread is currently enabled
69  */
70 bool Scheduler::is_enabled(const Thread *t) const
71 {
72         return is_enabled(t->get_id());
73 }
74
75 /**
76  * @brief Check if a Thread is currently enabled
77  *
78  * Check if a Thread is currently enabled. "Enabled" includes both
79  * THREAD_ENABLED and THREAD_SLEEP_SET.
80  * @param tid The ID of the Thread to check
81  * @return True if the Thread is currently enabled
82  */
83 bool Scheduler::is_enabled(thread_id_t tid) const
84 {
85         int i = id_to_int(tid);
86         return (i >= enabled_len) ? false : (enabled[i] != THREAD_DISABLED);
87 }
88
89 /**
90  * @brief Check if a Thread is currently in the sleep set
91  * @param t The Thread to check
92  * @return True if the Thread is currently enabled
93  */
94 bool Scheduler::is_sleep_set(const Thread *t) const
95 {
96         return get_enabled(t) == THREAD_SLEEP_SET;
97 }
98
99 /**
100  * @brief Check if execution is stuck with no enabled threads and some sleeping
101  * thread
102  * @return True if no threads are enabled an some thread is in the sleep set;
103  * false otherwise
104  */
105 bool Scheduler::all_threads_sleeping() const
106 {
107         bool sleeping = false;
108         for (int i = 0; i < enabled_len; i++)
109                 if (enabled[i] == THREAD_ENABLED)
110                         return false;
111                 else if (enabled[i] == THREAD_SLEEP_SET)
112                         sleeping = true;
113         return sleeping;
114 }
115
116 enabled_type_t Scheduler::get_enabled(const Thread *t) const
117 {
118         int id = id_to_int(t->get_id());
119         ASSERT(id < enabled_len);
120         return enabled[id];
121 }
122
123 void Scheduler::update_sleep_set(Node *n) {
124         enabled_type_t *enabled_array = n->get_enabled_array();
125         for (int i = 0; i < enabled_len; i++) {
126                 if (enabled_array[i] == THREAD_SLEEP_SET) {
127                         enabled[i] = THREAD_SLEEP_SET;
128                 }
129         }
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  * @return The next Thread to run
199  */
200 Thread * Scheduler::select_next_thread()
201 {
202         int old_curr_thread = curr_thread_index;
203         Node *n = model->get_curr_node();
204
205         bool have_enabled_thread_with_priority = false;
206         if (model->params.fairwindow != 0) {
207                 for (int i = 0; i < enabled_len; i++) {
208                         thread_id_t tid = int_to_id(i);
209                         if (n->has_priority(tid)) {
210                                 DEBUG("Node (tid %d) has priority\n", i);
211                                 if (enabled[i] != THREAD_DISABLED)
212                                         have_enabled_thread_with_priority = true;
213                         }
214                 }
215         }       
216
217         for (int i = 0; i < enabled_len; i++) {
218                 curr_thread_index = (old_curr_thread + i + 1) % enabled_len;
219                 thread_id_t curr_tid = int_to_id(curr_thread_index);
220                 if (model->params.yieldon) {
221                         bool bad_thread = false;
222                         for (int j = 0; j < enabled_len; j++) {
223                                 thread_id_t tother = int_to_id(j);
224                                 if ((enabled[j] != THREAD_DISABLED) && n->has_priority_over(curr_tid, tother)) {
225                                         bad_thread=true;
226                                         break;
227                                 }
228                         }
229                         if (bad_thread)
230                                 continue;
231                 }
232                 
233                 if (enabled[curr_thread_index] == THREAD_ENABLED &&
234                                 (!have_enabled_thread_with_priority || n->has_priority(curr_tid))) {
235                         return model->get_thread(curr_tid);
236                 }
237         }
238         
239         /* No thread was enabled */
240         return NULL;
241 }
242
243 void Scheduler::set_scheduler_thread(thread_id_t tid) {
244         curr_thread_index=id_to_int(tid);
245 }
246
247 /**
248  * @brief Set the current "running" Thread
249  * @param t Thread to run
250  */
251 void Scheduler::set_current_thread(Thread *t)
252 {
253         ASSERT(!t || !t->is_model_thread());
254
255         current = t;
256         if (DBG_ENABLED())
257                 print();
258 }
259
260 /**
261  * @return The currently-running Thread
262  */
263 Thread * Scheduler::get_current_thread() const
264 {
265         ASSERT(!current || !current->is_model_thread());
266         return current;
267 }
268
269 /**
270  * Print debugging information about the current state of the scheduler. Only
271  * prints something if debugging is enabled.
272  */
273 void Scheduler::print() const
274 {
275         int curr_id = current ? id_to_int(current->get_id()) : -1;
276
277         model_print("Scheduler: ");
278         for (int i = 0; i < enabled_len; i++) {
279                 char str[20];
280                 enabled_type_to_string(enabled[i], str);
281                 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);
282         }
283         model_print("\n");
284 }