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