impatomic: move atomic_{thread,signal}_fence() to namespace std
[c11tester.git] / schedule.cc
1 #include <string.h>
2 #include <stdlib.h>
3
4 #include "threads-model.h"
5 #include "schedule.h"
6 #include "common.h"
7 #include "model.h"
8 #include "nodestack.h"
9
10 /** Constructor */
11 Scheduler::Scheduler() :
12         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 (enabled != NULL) {
25                         memcpy(new_enabled, enabled, enabled_len*sizeof(enabled_type_t));
26                         snapshot_free(enabled);
27                 }
28                 enabled=new_enabled;
29                 enabled_len=threadid+1;
30         }
31         enabled[threadid]=enabled_status;
32         if (enabled_status == THREAD_DISABLED)
33                 model->check_promises_thread_disabled();
34 }
35
36 /**
37  * @brief Check if a Thread is currently enabled
38  *
39  * Check if a Thread is currently enabled. "Enabled" includes both
40  * THREAD_ENABLED and THREAD_SLEEP_SET.
41  * @param t The Thread to check
42  * @return True if the Thread is currently enabled
43  */
44 bool Scheduler::is_enabled(Thread *t) const
45 {
46         return is_enabled(t->get_id());
47 }
48
49 /**
50  * @brief Check if a Thread is currently enabled
51  *
52  * Check if a Thread is currently enabled. "Enabled" includes both
53  * THREAD_ENABLED and THREAD_SLEEP_SET.
54  * @param tid The ID of the Thread to check
55  * @return True if the Thread is currently enabled
56  */
57 bool Scheduler::is_enabled(thread_id_t tid) const
58 {
59         int i = id_to_int(tid);
60         return (i >= enabled_len) ? false : (enabled[i] != THREAD_DISABLED);
61 }
62
63 enabled_type_t Scheduler::get_enabled(Thread *t) {
64         int id = id_to_int(t->get_id());
65         ASSERT(id<enabled_len);
66         return enabled[id];
67 }
68
69 void Scheduler::update_sleep_set(Node *n) {
70         enabled_type_t *enabled_array=n->get_enabled_array();
71         for(int i=0;i<enabled_len;i++) {
72                 if (enabled_array[i]==THREAD_SLEEP_SET) {
73                         enabled[i]=THREAD_SLEEP_SET;
74                 }
75         }
76 }
77
78 /**
79  * Add a Thread to the sleep set.
80  * @param t The Thread to add
81  */
82 void Scheduler::add_sleep(Thread *t)
83 {
84         DEBUG("thread %d\n", id_to_int(t->get_id()));
85         set_enabled(t, THREAD_SLEEP_SET);
86 }
87
88 /**
89  * Remove a Thread from the sleep set.
90  * @param t The Thread to remove
91  */
92 void Scheduler::remove_sleep(Thread *t)
93 {
94         DEBUG("thread %d\n", id_to_int(t->get_id()));
95         set_enabled(t, THREAD_ENABLED);
96 }
97
98 /**
99  * Add a Thread to the scheduler's ready list.
100  * @param t The Thread to add
101  */
102 void Scheduler::add_thread(Thread *t)
103 {
104         DEBUG("thread %d\n", id_to_int(t->get_id()));
105         ASSERT(!t->is_model_thread());
106         set_enabled(t, THREAD_ENABLED);
107 }
108
109 /**
110  * Remove a given Thread from the scheduler.
111  * @param t The Thread to remove
112  */
113 void Scheduler::remove_thread(Thread *t)
114 {
115         if (current == t)
116                 current = NULL;
117         set_enabled(t, THREAD_DISABLED);
118 }
119
120 /**
121  * Prevent a Thread from being scheduled. The sleeping Thread should be
122  * re-awoken via Scheduler::wake.
123  * @param thread The Thread that should sleep
124  */
125 void Scheduler::sleep(Thread *t)
126 {
127         set_enabled(t, THREAD_DISABLED);
128         t->set_state(THREAD_BLOCKED);
129 }
130
131 /**
132  * Wake a Thread up that was previously waiting (see Scheduler::wait)
133  * @param t The Thread to wake up
134  */
135 void Scheduler::wake(Thread *t)
136 {
137         ASSERT(!t->is_model_thread());
138         set_enabled(t, THREAD_ENABLED);
139         t->set_state(THREAD_READY);
140 }
141
142 /**
143  * Select a Thread. This implementation defaults to round-robin, if a
144  * thread is not already provided.
145  *
146  * @param t Thread to run, if chosen by an external entity (e.g.,
147  * ModelChecker). May be NULL to indicate no external choice.
148  * @return The next Thread to run
149  */
150 Thread * Scheduler::next_thread(Thread *t)
151 {
152         if ( t == NULL ) {
153                 int old_curr_thread = curr_thread_index;
154                 bool have_enabled_thread_with_priority=false;
155                 Node *n=model->get_curr_node();
156
157                 for(int i=0;i<enabled_len;i++) {
158                         thread_id_t tid=int_to_id(i);
159                         if (n->has_priority(tid)) {
160                                 //Have a thread with priority
161                                 if (enabled[i]!=THREAD_DISABLED)
162                                         have_enabled_thread_with_priority=true;
163                         }
164                 }
165
166                 while(true) {
167                         curr_thread_index = (curr_thread_index+1) % enabled_len;
168                         thread_id_t curr_tid=int_to_id(curr_thread_index);
169                         if (enabled[curr_thread_index]==THREAD_ENABLED&&
170                                         (!have_enabled_thread_with_priority||n->has_priority(curr_tid))) {
171                                 t = model->get_thread(curr_tid);
172                                 break;
173                         }
174                         if (curr_thread_index == old_curr_thread) {
175                                 print();
176                                 return NULL;
177                         }
178                 }
179         } else if (t->is_model_thread()) {
180                 /* model-checker threads never run */
181                 t = NULL;
182         } else {
183                 curr_thread_index = id_to_int(t->get_id());
184         }
185
186         current = t;
187         print();
188         return t;
189 }
190
191 /**
192  * @return The currently-running Thread
193  */
194 Thread * Scheduler::get_current_thread() const
195 {
196         ASSERT(!current || !current->is_model_thread());
197         return current;
198 }
199
200 /**
201  * Print debugging information about the current state of the scheduler. Only
202  * prints something if debugging is enabled.
203  */
204 void Scheduler::print() const
205 {
206         if (current)
207                 DEBUG("Current thread: %d\n", id_to_int(current->get_id()));
208         else
209                 DEBUG("No current thread\n");
210 }