test: linuxrwlocks: fixup spacing
[model-checker.git] / schedule.cc
1 #include <string.h>
2 #include <stdlib.h>
3
4 #include "threads.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_DISABLED);
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                 while(true) {
135                         curr_thread_index = (curr_thread_index+1) % enabled_len;
136                         if (enabled[curr_thread_index]==THREAD_ENABLED) {
137                                 t = model->get_thread(int_to_id(curr_thread_index));
138                                 break;
139                         }
140                         if (curr_thread_index == old_curr_thread) {
141                                 print();
142                                 return NULL;
143                         }
144                 }
145         } else if (t->is_model_thread()) {
146                 /* model-checker threads never run */
147                 t = NULL;
148         } else {
149                 curr_thread_index = id_to_int(t->get_id());
150         }
151
152         current = t;
153         print();
154         return t;
155 }
156
157 /**
158  * @return The currently-running Thread
159  */
160 Thread * Scheduler::get_current_thread() const
161 {
162         ASSERT(!current || !current->is_model_thread());
163         return current;
164 }
165
166 /**
167  * Print debugging information about the current state of the scheduler. Only
168  * prints something if debugging is enabled.
169  */
170 void Scheduler::print() const
171 {
172         if (current)
173                 DEBUG("Current thread: %d\n", id_to_int(current->get_id()));
174         else
175                 DEBUG("No current thread\n");
176 }