action: backtrack/sleep-sets for seq-cst fences
[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
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 static 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 enabled_type_t Scheduler::get_enabled(const Thread *t) const
100 {
101         int id = id_to_int(t->get_id());
102         ASSERT(id < enabled_len);
103         return enabled[id];
104 }
105
106 void Scheduler::update_sleep_set(Node *n) {
107         enabled_type_t *enabled_array = n->get_enabled_array();
108         for (int i = 0; i < enabled_len; i++) {
109                 if (enabled_array[i] == THREAD_SLEEP_SET) {
110                         enabled[i] = THREAD_SLEEP_SET;
111                 }
112         }
113 }
114
115 /**
116  * Add a Thread to the sleep set.
117  * @param t The Thread to add
118  */
119 void Scheduler::add_sleep(Thread *t)
120 {
121         DEBUG("thread %d\n", id_to_int(t->get_id()));
122         set_enabled(t, THREAD_SLEEP_SET);
123 }
124
125 /**
126  * Remove a Thread from the sleep set.
127  * @param t The Thread to remove
128  */
129 void Scheduler::remove_sleep(Thread *t)
130 {
131         DEBUG("thread %d\n", id_to_int(t->get_id()));
132         set_enabled(t, THREAD_ENABLED);
133 }
134
135 /**
136  * Add a Thread to the scheduler's ready list.
137  * @param t The Thread to add
138  */
139 void Scheduler::add_thread(Thread *t)
140 {
141         DEBUG("thread %d\n", id_to_int(t->get_id()));
142         ASSERT(!t->is_model_thread());
143         set_enabled(t, THREAD_ENABLED);
144 }
145
146 /**
147  * Remove a given Thread from the scheduler.
148  * @param t The Thread to remove
149  */
150 void Scheduler::remove_thread(Thread *t)
151 {
152         if (current == t)
153                 current = NULL;
154         set_enabled(t, THREAD_DISABLED);
155 }
156
157 /**
158  * Prevent a Thread from being scheduled. The sleeping Thread should be
159  * re-awoken via Scheduler::wake.
160  * @param thread The Thread that should sleep
161  */
162 void Scheduler::sleep(Thread *t)
163 {
164         set_enabled(t, THREAD_DISABLED);
165         t->set_state(THREAD_BLOCKED);
166 }
167
168 /**
169  * Wake a Thread up that was previously waiting (see Scheduler::wait)
170  * @param t The Thread to wake up
171  */
172 void Scheduler::wake(Thread *t)
173 {
174         ASSERT(!t->is_model_thread());
175         set_enabled(t, THREAD_ENABLED);
176         t->set_state(THREAD_READY);
177 }
178
179 /**
180  * @brief Select a Thread to run via round-robin
181  * @return The next Thread to run
182  */
183 Thread * Scheduler::select_next_thread()
184 {
185         int old_curr_thread = curr_thread_index;
186         bool have_enabled_thread_with_priority = false;
187         Node *n = model->get_curr_node();
188
189         for (int i = 0; i < enabled_len; i++) {
190                 thread_id_t tid = int_to_id(i);
191                 if (n->has_priority(tid)) {
192                         DEBUG("Node (tid %d) has priority\n", i);
193                         if (enabled[i] != THREAD_DISABLED)
194                                 have_enabled_thread_with_priority = true;
195                 }
196         }
197
198         for (int i = 0; i < enabled_len; i++) {
199                 curr_thread_index = (old_curr_thread + i + 1) % enabled_len;
200                 thread_id_t curr_tid = int_to_id(curr_thread_index);
201                 if (enabled[curr_thread_index] == THREAD_ENABLED &&
202                                 (!have_enabled_thread_with_priority || n->has_priority(curr_tid))) {
203                         return model->get_thread(curr_tid);
204                 }
205         }
206         /* No thread was enabled */
207         return NULL;
208 }
209
210 /**
211  * @brief Set the current "running" Thread
212  * @param t Thread to run
213  */
214 void Scheduler::set_current_thread(Thread *t)
215 {
216         ASSERT(t && !t->is_model_thread());
217
218         curr_thread_index = id_to_int(t->get_id());
219
220         current = t;
221         if (DBG_ENABLED())
222                 print();
223 }
224
225 /**
226  * @return The currently-running Thread
227  */
228 Thread * Scheduler::get_current_thread() const
229 {
230         ASSERT(!current || !current->is_model_thread());
231         return current;
232 }
233
234 /**
235  * Print debugging information about the current state of the scheduler. Only
236  * prints something if debugging is enabled.
237  */
238 void Scheduler::print() const
239 {
240         int curr_id = current ? id_to_int(current->get_id()) : -1;
241
242         model_print("Scheduler: ");
243         for (int i = 0; i < enabled_len; i++) {
244                 char str[20];
245                 enabled_type_to_string(enabled[i], str);
246                 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);
247         }
248         model_print("\n");
249 }