bug fixes for lock support...think it works now...
[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                 memset(&new_enabled[enabled_len], 0, (threadid+1-enabled_len)*sizeof(bool));
20                 if (is_enabled != NULL) {
21                         memcpy(new_enabled, is_enabled, enabled_len*sizeof(bool));
22                         free(is_enabled);
23                 }
24                 is_enabled=new_enabled;
25                 enabled_len=threadid+1;
26         }
27         is_enabled[threadid]=enabled_status;
28 }
29
30 /**
31  * Add a Thread to the scheduler's ready list.
32  * @param t The Thread to add
33  */
34 void Scheduler::add_thread(Thread *t)
35 {
36         DEBUG("thread %d\n", t->get_id());
37         set_enabled(t, true);
38 }
39
40 /**
41  * Remove a given Thread from the scheduler.
42  * @param t The Thread to remove
43  */
44 void Scheduler::remove_thread(Thread *t)
45 {
46         if (current == t)
47                 current = NULL;
48         set_enabled(t, false);
49 }
50
51 /**
52  * Prevent a Thread from being scheduled. The sleeping Thread should be
53  * re-awoken via Scheduler::wake.
54  * @param thread The Thread that should sleep
55  */
56 void Scheduler::sleep(Thread *t)
57 {
58         set_enabled(t, false);
59         t->set_state(THREAD_BLOCKED);
60 }
61
62 /**
63  * Wake a Thread up that was previously waiting (see Scheduler::wait)
64  * @param t The Thread to wake up
65  */
66 void Scheduler::wake(Thread *t)
67 {
68         set_enabled(t, true);
69         t->set_state(THREAD_READY);
70 }
71
72 /**
73  * Remove one Thread from the scheduler. This implementation defaults to FIFO,
74  * if a thread is not already provided.
75  *
76  * @param t Thread to run, if chosen by an external entity (e.g.,
77  * ModelChecker). May be NULL to indicate no external choice.
78  * @return The next Thread to run
79  */
80 Thread * Scheduler::next_thread(Thread *t)
81 {
82         if ( t == NULL ) {
83                 int old_curr_thread = curr_thread_index;
84                 while(true) {
85                         curr_thread_index = (curr_thread_index+1) % enabled_len;
86                         if (is_enabled[curr_thread_index]) {
87                                 t = model->get_thread(int_to_id(curr_thread_index));
88                                 break;
89                         }
90                         if (curr_thread_index == old_curr_thread) {
91                                 print();
92                                 return NULL;
93                         }
94                 }
95         } else {
96                 curr_thread_index = id_to_int(t->get_id());
97         }
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 }