schedule: add const
[model-checker.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 enabled_type_t Scheduler::get_enabled(const Thread *t) const
64 {
65         int id = id_to_int(t->get_id());
66         ASSERT(id < enabled_len);
67         return enabled[id];
68 }
69
70 void Scheduler::update_sleep_set(Node *n) {
71         enabled_type_t *enabled_array=n->get_enabled_array();
72         for(int i=0;i<enabled_len;i++) {
73                 if (enabled_array[i]==THREAD_SLEEP_SET) {
74                         enabled[i]=THREAD_SLEEP_SET;
75                 }
76         }
77 }
78
79 /**
80  * Add a Thread to the sleep set.
81  * @param t The Thread to add
82  */
83 void Scheduler::add_sleep(Thread *t)
84 {
85         DEBUG("thread %d\n", id_to_int(t->get_id()));
86         set_enabled(t, THREAD_SLEEP_SET);
87 }
88
89 /**
90  * Remove a Thread from the sleep set.
91  * @param t The Thread to remove
92  */
93 void Scheduler::remove_sleep(Thread *t)
94 {
95         DEBUG("thread %d\n", id_to_int(t->get_id()));
96         set_enabled(t, THREAD_ENABLED);
97 }
98
99 /**
100  * Add a Thread to the scheduler's ready list.
101  * @param t The Thread to add
102  */
103 void Scheduler::add_thread(Thread *t)
104 {
105         DEBUG("thread %d\n", id_to_int(t->get_id()));
106         ASSERT(!t->is_model_thread());
107         set_enabled(t, THREAD_ENABLED);
108 }
109
110 /**
111  * Remove a given Thread from the scheduler.
112  * @param t The Thread to remove
113  */
114 void Scheduler::remove_thread(Thread *t)
115 {
116         if (current == t)
117                 current = NULL;
118         set_enabled(t, THREAD_DISABLED);
119 }
120
121 /**
122  * Prevent a Thread from being scheduled. The sleeping Thread should be
123  * re-awoken via Scheduler::wake.
124  * @param thread The Thread that should sleep
125  */
126 void Scheduler::sleep(Thread *t)
127 {
128         set_enabled(t, THREAD_DISABLED);
129         t->set_state(THREAD_BLOCKED);
130 }
131
132 /**
133  * Wake a Thread up that was previously waiting (see Scheduler::wait)
134  * @param t The Thread to wake up
135  */
136 void Scheduler::wake(Thread *t)
137 {
138         ASSERT(!t->is_model_thread());
139         set_enabled(t, THREAD_ENABLED);
140         t->set_state(THREAD_READY);
141 }
142
143 /**
144  * Select a Thread. This implementation defaults to round-robin, if a
145  * thread is not already provided.
146  *
147  * @param t Thread to run, if chosen by an external entity (e.g.,
148  * ModelChecker). May be NULL to indicate no external choice.
149  * @return The next Thread to run
150  */
151 Thread * Scheduler::next_thread(Thread *t)
152 {
153         if ( t == NULL ) {
154                 int old_curr_thread = curr_thread_index;
155                 bool have_enabled_thread_with_priority=false;
156                 Node *n=model->get_curr_node();
157
158                 for(int i=0;i<enabled_len;i++) {
159                         thread_id_t tid=int_to_id(i);
160                         if (n->has_priority(tid)) {
161                                 //Have a thread with priority
162                                 if (enabled[i]!=THREAD_DISABLED)
163                                         have_enabled_thread_with_priority=true;
164                         }
165                 }
166
167                 while(true) {
168                         curr_thread_index = (curr_thread_index+1) % enabled_len;
169                         thread_id_t curr_tid=int_to_id(curr_thread_index);
170                         if (enabled[curr_thread_index]==THREAD_ENABLED&&
171                                         (!have_enabled_thread_with_priority||n->has_priority(curr_tid))) {
172                                 t = model->get_thread(curr_tid);
173                                 break;
174                         }
175                         if (curr_thread_index == old_curr_thread) {
176                                 print();
177                                 return NULL;
178                         }
179                 }
180         } else if (t->is_model_thread()) {
181                 /* model-checker threads never run */
182                 t = NULL;
183         } else {
184                 curr_thread_index = id_to_int(t->get_id());
185         }
186
187         current = t;
188         print();
189         return t;
190 }
191
192 /**
193  * @return The currently-running Thread
194  */
195 Thread * Scheduler::get_current_thread() const
196 {
197         ASSERT(!current || !current->is_model_thread());
198         return current;
199 }
200
201 /**
202  * Print debugging information about the current state of the scheduler. Only
203  * prints something if debugging is enabled.
204  */
205 void Scheduler::print() const
206 {
207         if (current)
208                 DEBUG("Current thread: %d\n", id_to_int(current->get_id()));
209         else
210                 DEBUG("No current thread\n");
211 }