5c93381b99763344d3fce9548a39955c041c6d79
[model-checker.git] / schedule.cc
1 #include "threads.h"
2 #include "schedule.h"
3 #include "common.h"
4 #include "model.h"
5
6 /** Constructor */
7 Scheduler::Scheduler() :
8         is_enabled(NULL),
9         enabled_len(0),
10         curr_thread_index(0),
11         current(NULL)
12 {
13 }
14
15 void Scheduler::set_enabled(Thread *t, bool enabled_status) {
16         int threadid=id_to_int(t->get_id());
17         if (threadid>=enabled_len) {
18                 bool * new_enabled=(bool *)malloc(sizeof(bool)*(threadid+1));
19                 memcpy(new_enabled, is_enabled, enabled_len*sizeof(bool));
20                 memset(&new_enabled[enabled_len], 0, (threadid+1-enabled_len)*sizeof(bool));
21                 free(is_enabled);
22                 is_enabled=new_enabled;
23                 enabled_len=threadid+1;
24         }
25         is_enabled[threadid]=enabled_status;
26 }
27
28 /**
29  * Add a Thread to the scheduler's ready list.
30  * @param t The Thread to add
31  */
32 void Scheduler::add_thread(Thread *t)
33 {
34         DEBUG("thread %d\n", t->get_id());
35         set_enabled(t, true);
36 }
37
38 /**
39  * Remove a given Thread from the scheduler.
40  * @param t The Thread to remove
41  */
42 void Scheduler::remove_thread(Thread *t)
43 {
44         if (current == t)
45                 current = NULL;
46         set_enabled(t, false);
47 }
48
49 /**
50  * Prevent a Thread from being scheduled. The sleeping Thread should be
51  * re-awoken via Scheduler::wake.
52  * @param thread The Thread that should sleep
53  */
54 void Scheduler::sleep(Thread *t)
55 {
56         set_enabled(t, false);
57         t->set_state(THREAD_BLOCKED);
58 }
59
60 /**
61  * Wake a Thread up that was previously waiting (see Scheduler::wait)
62  * @param t The Thread to wake up
63  */
64 void Scheduler::wake(Thread *t)
65 {
66         set_enabled(t, true);
67         t->set_state(THREAD_READY);
68 }
69
70 /**
71  * Remove one Thread from the scheduler. This implementation defaults to FIFO,
72  * if a thread is not already provided.
73  *
74  * @param t Thread to run, if chosen by an external entity (e.g.,
75  * ModelChecker). May be NULL to indicate no external choice.
76  * @return The next Thread to run
77  */
78 Thread * Scheduler::next_thread(Thread *t)
79 {
80         printf("%p\n",t);
81         if ( t == NULL ) {
82                 int old_curr_thread = curr_thread_index;
83                 while(true) {
84                         curr_thread_index = (curr_thread_index+1) % enabled_len;
85                         if (is_enabled[curr_thread_index]) {
86                                 t = model->get_thread(int_to_id(curr_thread_index));
87                                 break;
88                         }
89                         if (curr_thread_index == old_curr_thread) {
90                                 print();
91                                 return NULL;
92                         }
93                 }
94         } else {
95                 curr_thread_index = id_to_int(t->get_id());
96         }
97         printf("index=%u enabled=%u\n", curr_thread_index, is_enabled[curr_thread_index]);
98
99         current = t;
100         print();
101         return t;
102 }
103
104 /**
105  * @return The currently-running Thread
106  */
107 Thread * Scheduler::get_current_thread() const
108 {
109         return current;
110 }
111
112 /**
113  * Print debugging information about the current state of the scheduler. Only
114  * prints something if debugging is enabled.
115  */
116 void Scheduler::print() const
117 {
118         if (current)
119                 DEBUG("Current thread: %d\n", current->get_id());
120         else
121                 DEBUG("No current thread\n");
122 }