cyclegraph: add edgeCreatesCycle() function
[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 /** Constructor */
11 Scheduler::Scheduler() :
12         enabled(NULL),
13         enabled_len(0),
14         curr_thread_index(0),
15         current(NULL)
16 {
17 }
18
19 void Scheduler::set_enabled(Thread *t, enabled_type_t enabled_status) {
20         int threadid = id_to_int(t->get_id());
21         if (threadid >= enabled_len) {
22                 enabled_type_t *new_enabled = (enabled_type_t *)snapshot_malloc(sizeof(enabled_type_t) * (threadid + 1));
23                 memset(&new_enabled[enabled_len], 0, (threadid + 1 - enabled_len) * sizeof(enabled_type_t));
24                 if (enabled != NULL) {
25                         memcpy(new_enabled, enabled, enabled_len * sizeof(enabled_type_t));
26                         snapshot_free(enabled);
27                 }
28                 enabled = new_enabled;
29                 enabled_len = threadid + 1;
30         }
31         enabled[threadid] = enabled_status;
32         if (enabled_status == THREAD_DISABLED)
33                 model->check_promises_thread_disabled();
34 }
35
36 /**
37  * @brief Check if a Thread is currently enabled
38  *
39  * Check if a Thread is currently enabled. "Enabled" includes both
40  * THREAD_ENABLED and THREAD_SLEEP_SET.
41  * @param t The Thread to check
42  * @return True if the Thread is currently enabled
43  */
44 bool Scheduler::is_enabled(const Thread *t) const
45 {
46         return is_enabled(t->get_id());
47 }
48
49 /**
50  * @brief Check if a Thread is currently enabled
51  *
52  * Check if a Thread is currently enabled. "Enabled" includes both
53  * THREAD_ENABLED and THREAD_SLEEP_SET.
54  * @param tid The ID of the Thread to check
55  * @return True if the Thread is currently enabled
56  */
57 bool Scheduler::is_enabled(thread_id_t tid) const
58 {
59         int i = id_to_int(tid);
60         return (i >= enabled_len) ? false : (enabled[i] != THREAD_DISABLED);
61 }
62
63 /**
64  * @brief Check if a Thread is currently in the sleep set
65  * @param t The Thread to check
66  * @return True if the Thread is currently enabled
67  */
68 bool Scheduler::is_sleep_set(const Thread *t) const
69 {
70         return get_enabled(t) == THREAD_SLEEP_SET;
71 }
72
73 enabled_type_t Scheduler::get_enabled(const Thread *t) const
74 {
75         int id = id_to_int(t->get_id());
76         ASSERT(id < enabled_len);
77         return enabled[id];
78 }
79
80 void Scheduler::update_sleep_set(Node *n) {
81         enabled_type_t *enabled_array = n->get_enabled_array();
82         for (int i = 0; i < enabled_len; i++) {
83                 if (enabled_array[i] == THREAD_SLEEP_SET) {
84                         enabled[i] = THREAD_SLEEP_SET;
85                 }
86         }
87 }
88
89 /**
90  * Add a Thread to the sleep set.
91  * @param t The Thread to add
92  */
93 void Scheduler::add_sleep(Thread *t)
94 {
95         DEBUG("thread %d\n", id_to_int(t->get_id()));
96         set_enabled(t, THREAD_SLEEP_SET);
97 }
98
99 /**
100  * Remove a Thread from the sleep set.
101  * @param t The Thread to remove
102  */
103 void Scheduler::remove_sleep(Thread *t)
104 {
105         DEBUG("thread %d\n", id_to_int(t->get_id()));
106         set_enabled(t, THREAD_ENABLED);
107 }
108
109 /**
110  * Add a Thread to the scheduler's ready list.
111  * @param t The Thread to add
112  */
113 void Scheduler::add_thread(Thread *t)
114 {
115         DEBUG("thread %d\n", id_to_int(t->get_id()));
116         ASSERT(!t->is_model_thread());
117         set_enabled(t, THREAD_ENABLED);
118 }
119
120 /**
121  * Remove a given Thread from the scheduler.
122  * @param t The Thread to remove
123  */
124 void Scheduler::remove_thread(Thread *t)
125 {
126         if (current == t)
127                 current = NULL;
128         set_enabled(t, THREAD_DISABLED);
129 }
130
131 /**
132  * Prevent a Thread from being scheduled. The sleeping Thread should be
133  * re-awoken via Scheduler::wake.
134  * @param thread The Thread that should sleep
135  */
136 void Scheduler::sleep(Thread *t)
137 {
138         set_enabled(t, THREAD_DISABLED);
139         t->set_state(THREAD_BLOCKED);
140 }
141
142 /**
143  * Wake a Thread up that was previously waiting (see Scheduler::wait)
144  * @param t The Thread to wake up
145  */
146 void Scheduler::wake(Thread *t)
147 {
148         ASSERT(!t->is_model_thread());
149         set_enabled(t, THREAD_ENABLED);
150         t->set_state(THREAD_READY);
151 }
152
153 /**
154  * Select a Thread. This implementation defaults to round-robin, if a
155  * thread is not already provided.
156  *
157  * @param t Thread to run, if chosen by an external entity (e.g.,
158  * ModelChecker). May be NULL to indicate no external choice.
159  * @return The next Thread to run
160  */
161 Thread * Scheduler::next_thread(Thread *t)
162 {
163         if (t == NULL) {
164                 int old_curr_thread = curr_thread_index;
165                 bool have_enabled_thread_with_priority = false;
166                 Node *n = model->get_curr_node();
167
168                 for (int i = 0; i < enabled_len; i++) {
169                         thread_id_t tid = int_to_id(i);
170                         if (n->has_priority(tid)) {
171                                 DEBUG("Node (tid %d) has priority\n", i);
172                                 //Have a thread with priority
173                                 if (enabled[i] != THREAD_DISABLED)
174                                         have_enabled_thread_with_priority = true;
175                         }
176                 }
177
178                 while (true) {
179                         curr_thread_index = (curr_thread_index + 1) % enabled_len;
180                         thread_id_t curr_tid = int_to_id(curr_thread_index);
181                         if (enabled[curr_thread_index] == THREAD_ENABLED &&
182                                         (!have_enabled_thread_with_priority || n->has_priority(curr_tid))) {
183                                 t = model->get_thread(curr_tid);
184                                 break;
185                         }
186                         if (curr_thread_index == old_curr_thread) {
187                                 print();
188                                 return NULL;
189                         }
190                 }
191         } else if (t->is_model_thread()) {
192                 /* model-checker threads never run */
193                 t = NULL;
194         } else {
195                 curr_thread_index = id_to_int(t->get_id());
196         }
197
198         current = t;
199         print();
200         return t;
201 }
202
203 /**
204  * @return The currently-running Thread
205  */
206 Thread * Scheduler::get_current_thread() const
207 {
208         ASSERT(!current || !current->is_model_thread());
209         return current;
210 }
211
212 /**
213  * Print debugging information about the current state of the scheduler. Only
214  * prints something if debugging is enabled.
215  */
216 void Scheduler::print() const
217 {
218         if (current)
219                 DEBUG("Current thread: %d\n", id_to_int(current->get_id()));
220         else
221                 DEBUG("No current thread\n");
222 }