common: improve backtrace function
[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         set_enabled(t, THREAD_ENABLED);
52 }
53
54 /**
55  * Remove a given Thread from the scheduler.
56  * @param t The Thread to remove
57  */
58 void Scheduler::remove_thread(Thread *t)
59 {
60         if (current == t)
61                 current = NULL;
62         set_enabled(t, THREAD_DISABLED);
63 }
64
65 /**
66  * Prevent a Thread from being scheduled. The sleeping Thread should be
67  * re-awoken via Scheduler::wake.
68  * @param thread The Thread that should sleep
69  */
70 void Scheduler::sleep(Thread *t)
71 {
72         set_enabled(t, THREAD_DISABLED);
73         t->set_state(THREAD_BLOCKED);
74 }
75
76 /**
77  * Wake a Thread up that was previously waiting (see Scheduler::wait)
78  * @param t The Thread to wake up
79  */
80 void Scheduler::wake(Thread *t)
81 {
82         set_enabled(t, THREAD_DISABLED);
83         t->set_state(THREAD_READY);
84 }
85
86 /**
87  * Select a Thread. This implementation defaults to round-robin, if a
88  * thread is not already provided.
89  *
90  * @param t Thread to run, if chosen by an external entity (e.g.,
91  * ModelChecker). May be NULL to indicate no external choice.
92  * @return The next Thread to run
93  */
94 Thread * Scheduler::next_thread(Thread *t)
95 {
96         if ( t == NULL ) {
97                 int old_curr_thread = curr_thread_index;
98                 while(true) {
99                         curr_thread_index = (curr_thread_index+1) % enabled_len;
100                         if (enabled[curr_thread_index]) {
101                                 t = model->get_thread(int_to_id(curr_thread_index));
102                                 break;
103                         }
104                         if (curr_thread_index == old_curr_thread) {
105                                 print();
106                                 return NULL;
107                         }
108                 }
109         } else {
110                 curr_thread_index = id_to_int(t->get_id());
111         }
112
113         current = t;
114         print();
115         return t;
116 }
117
118 /**
119  * @return The currently-running Thread
120  */
121 Thread * Scheduler::get_current_thread() const
122 {
123         return current;
124 }
125
126 /**
127  * Print debugging information about the current state of the scheduler. Only
128  * prints something if debugging is enabled.
129  */
130 void Scheduler::print() const
131 {
132         if (current)
133                 DEBUG("Current thread: %d\n", id_to_int(current->get_id()));
134         else
135                 DEBUG("No current thread\n");
136 }