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