c5e58fe65cb50960db366bd39fb45177ec6fc7db
[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         is_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 (is_enabled != NULL) {
25                         memcpy(new_enabled, is_enabled, enabled_len*sizeof(enabled_type_t));
26                         snapshot_free(is_enabled);
27                 }
28                 is_enabled=new_enabled;
29                 enabled_len=threadid+1;
30         }
31         is_enabled[threadid]=enabled_status;
32 }
33
34 enabled_type_t Scheduler::get_enabled(Thread *t) {
35         return is_enabled[id_to_int(t->get_id())];
36 }
37
38 void Scheduler::update_sleep_set(Node *n) {
39         enabled_type_t *enabled_array=n->get_enabled_array();
40         for(int i=0;i<enabled_len;i++) {
41                 if (enabled_array[i]==THREAD_SLEEP_SET) {
42                         is_enabled[i]=THREAD_SLEEP_SET;
43                 }
44         }
45 }
46
47 /**
48  * Add a Thread to the sleep set.
49  * @param t The Thread to add
50  */
51 void Scheduler::add_sleep(Thread *t)
52 {
53         DEBUG("thread %d\n", id_to_int(t->get_id()));
54         set_enabled(t, THREAD_SLEEP_SET);
55 }
56
57 /**
58  * Remove a Thread from the sleep set.
59  * @param t The Thread to remove
60  */
61 void Scheduler::remove_sleep(Thread *t)
62 {
63         DEBUG("thread %d\n", id_to_int(t->get_id()));
64         set_enabled(t, THREAD_ENABLED);
65 }
66
67 /**
68  * Add a Thread to the scheduler's ready list.
69  * @param t The Thread to add
70  */
71 void Scheduler::add_thread(Thread *t)
72 {
73         DEBUG("thread %d\n", id_to_int(t->get_id()));
74         set_enabled(t, THREAD_ENABLED);
75 }
76
77 /**
78  * Remove a given Thread from the scheduler.
79  * @param t The Thread to remove
80  */
81 void Scheduler::remove_thread(Thread *t)
82 {
83         if (current == t)
84                 current = NULL;
85         set_enabled(t, THREAD_DISABLED);
86 }
87
88 /**
89  * Prevent a Thread from being scheduled. The sleeping Thread should be
90  * re-awoken via Scheduler::wake.
91  * @param thread The Thread that should sleep
92  */
93 void Scheduler::sleep(Thread *t)
94 {
95         set_enabled(t, THREAD_DISABLED);
96         t->set_state(THREAD_BLOCKED);
97 }
98
99 /**
100  * Wake a Thread up that was previously waiting (see Scheduler::wait)
101  * @param t The Thread to wake up
102  */
103 void Scheduler::wake(Thread *t)
104 {
105         set_enabled(t, THREAD_DISABLED);
106         t->set_state(THREAD_READY);
107 }
108
109 /**
110  * Select a Thread. This implementation defaults to round-robin, if a
111  * thread is not already provided.
112  *
113  * @param t Thread to run, if chosen by an external entity (e.g.,
114  * ModelChecker). May be NULL to indicate no external choice.
115  * @return The next Thread to run
116  */
117 Thread * Scheduler::next_thread(Thread *t)
118 {
119         if ( t == NULL ) {
120                 int old_curr_thread = curr_thread_index;
121                 while(true) {
122                         curr_thread_index = (curr_thread_index+1) % enabled_len;
123                         if (is_enabled[curr_thread_index]==THREAD_ENABLED) {
124                                 t = model->get_thread(int_to_id(curr_thread_index));
125                                 break;
126                         }
127                         if (curr_thread_index == old_curr_thread) {
128                                 print();
129                                 return NULL;
130                         }
131                 }
132         } else {
133                 curr_thread_index = id_to_int(t->get_id());
134         }
135
136         current = t;
137         print();
138         return t;
139 }
140
141 /**
142  * @return The currently-running Thread
143  */
144 Thread * Scheduler::get_current_thread() const
145 {
146         return current;
147 }
148
149 /**
150  * Print debugging information about the current state of the scheduler. Only
151  * prints something if debugging is enabled.
152  */
153 void Scheduler::print() const
154 {
155         if (current)
156                 DEBUG("Current thread: %d\n", id_to_int(current->get_id()));
157         else
158                 DEBUG("No current thread\n");
159 }