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