model: add read-write coherence for promise nodes
[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  * Select a Thread. This implementation defaults to round-robin, if a
181  * thread is not already provided.
182  *
183  * @param t Thread to run, if chosen by an external entity (e.g.,
184  * ModelChecker). May be NULL to indicate no external choice.
185  * @return The next Thread to run
186  */
187 Thread * Scheduler::next_thread(Thread *t)
188 {
189         if (t == NULL) {
190                 int old_curr_thread = curr_thread_index;
191                 bool have_enabled_thread_with_priority = false;
192                 Node *n = model->get_curr_node();
193
194                 for (int i = 0; i < enabled_len; i++) {
195                         thread_id_t tid = int_to_id(i);
196                         if (n->has_priority(tid)) {
197                                 DEBUG("Node (tid %d) has priority\n", i);
198                                 //Have a thread with priority
199                                 if (enabled[i] != THREAD_DISABLED)
200                                         have_enabled_thread_with_priority = true;
201                         }
202                 }
203
204                 while (true) {
205                         curr_thread_index = (curr_thread_index + 1) % enabled_len;
206                         thread_id_t curr_tid = int_to_id(curr_thread_index);
207                         if (enabled[curr_thread_index] == THREAD_ENABLED &&
208                                         (!have_enabled_thread_with_priority || n->has_priority(curr_tid))) {
209                                 t = model->get_thread(curr_tid);
210                                 break;
211                         }
212                         if (curr_thread_index == old_curr_thread) {
213                                 if (DBG_ENABLED())
214                                         print();
215                                 return NULL;
216                         }
217                 }
218         } else if (t->is_model_thread()) {
219                 /* model-checker threads never run */
220                 t = NULL;
221         } else {
222                 curr_thread_index = id_to_int(t->get_id());
223         }
224
225         current = t;
226         if (DBG_ENABLED())
227                 print();
228         return t;
229 }
230
231 /**
232  * @return The currently-running Thread
233  */
234 Thread * Scheduler::get_current_thread() const
235 {
236         ASSERT(!current || !current->is_model_thread());
237         return current;
238 }
239
240 /**
241  * Print debugging information about the current state of the scheduler. Only
242  * prints something if debugging is enabled.
243  */
244 void Scheduler::print() const
245 {
246         int curr_id = current ? id_to_int(current->get_id()) : -1;
247
248         model_print("Scheduler: ");
249         for (int i = 0; i < enabled_len; i++) {
250                 char str[20];
251                 enabled_type_to_string(enabled[i], str);
252                 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);
253         }
254         model_print("\n");
255 }