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