9 ModelChecker::ModelChecker()
11 /* First thread created (system_thread) will have id 1 */
12 this->used_thread_id = 0;
13 /* Initialize default scheduler */
14 this->scheduler = new Scheduler();
17 this->current_action = NULL;
18 this->exploring = NULL;
19 this->nextThread = THREAD_ID_T_NONE;
21 rootNode = new TreeNode(NULL);
22 currentNode = rootNode;
23 action_trace = new action_list_t();
26 ModelChecker::~ModelChecker()
29 delete this->scheduler;
33 void ModelChecker::reset_to_initial_state()
35 DEBUG("+++ Resetting to initial state +++\n");
36 std::map<thread_id_t, class Thread *>::iterator it;
37 for (it = thread_map.begin(); it != thread_map.end(); it++) {
41 action_trace = new action_list_t();
42 currentNode = rootNode;
43 current_action = NULL;
44 used_thread_id = 1; // ?
45 /* scheduler reset ? */
48 int ModelChecker::get_next_id()
50 return ++used_thread_id;
53 void ModelChecker::add_system_thread(Thread *t)
55 this->system_thread = t;
58 Thread * ModelChecker::schedule_next_thread()
61 if (nextThread == THREAD_ID_T_NONE)
63 t = thread_map[nextThread];
65 DEBUG("*** error: thread not in thread_map: id = %d\n", nextThread);
70 * get_next_replay_thread() - Choose the next thread in the replay sequence
72 * If we've reached the 'diverge' point, then we pick a thread from the
74 * Otherwise, we simply return the next thread in the sequence.
76 thread_id_t ModelChecker::get_next_replay_thread()
81 next = exploring->get_state();
83 if (next == exploring->get_diverge()) {
84 TreeNode *node = next->get_node();
86 /* Reached divergence point; discard our current 'exploring' */
87 DEBUG("*** Discard 'Backtrack' object ***\n");
88 tid = node->getNextBacktrack();
92 tid = next->get_tid();
94 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
98 thread_id_t ModelChecker::advance_backtracking_state()
100 /* Have we completed exploring the preselected path? */
101 if (exploring == NULL)
102 return THREAD_ID_T_NONE;
104 /* Else, we are trying to replay an execution */
105 exploring->advance_state();
106 if (exploring->get_state() == NULL)
107 DEBUG("*** error: reached end of backtrack trace\n");
109 return get_next_replay_thread();
112 bool ModelChecker::next_execution()
116 if ((exploring = model->get_next_backtrack()) == NULL)
118 model->reset_to_initial_state();
119 nextThread = get_next_replay_thread();
123 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
125 void *loc = act->get_location();
126 action_type type = act->get_type();
127 thread_id_t id = act->get_tid();
139 /* linear search: from most recent to oldest */
140 action_list_t::reverse_iterator rit;
141 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
142 ModelAction *prev = *rit;
143 if (prev->get_location() != loc)
145 if (type == ATOMIC_READ && prev->get_type() != ATOMIC_WRITE)
147 /* Conflict from the same thread is not really a conflict */
148 if (id == prev->get_tid())
155 void ModelChecker::set_backtracking(ModelAction *act)
160 prev = get_last_conflict(act);
164 node = prev->get_node();
166 /* Check if this has been explored already */
167 if (node->hasBeenExplored(act->get_tid()))
169 /* If this is a new backtracking point, mark the tree */
170 if (node->setBacktrack(act->get_tid()) != 0)
173 printf("Setting backtrack: conflict = %d, instead tid = %d\n",
174 prev->get_tid(), act->get_tid());
178 Backtrack *back = new Backtrack(prev, action_trace);
179 backtrack_list.push_back(back);
182 Backtrack * ModelChecker::get_next_backtrack()
185 if (backtrack_list.empty())
187 next = backtrack_list.back();
188 backtrack_list.pop_back();
192 void ModelChecker::check_current_action(void)
194 ModelAction *next = this->current_action;
197 DEBUG("trying to push NULL action...\n");
200 nextThread = advance_backtracking_state();
201 next->set_node(currentNode);
202 set_backtracking(next);
203 currentNode = currentNode->exploreChild(next->get_tid());
204 this->action_trace->push_back(next);
207 void ModelChecker::print_summary(void)
209 action_list_t::iterator it;
212 printf("---------------------------------------------------------------------\n");
213 printf("Number of executions: %d\n", num_executions);
214 printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
218 printf("Trace:\n\n");
220 for (it = action_trace->begin(); it != action_trace->end(); it++) {
224 printf("---------------------------------------------------------------------\n");
227 int ModelChecker::add_thread(Thread *t)
229 thread_map[t->get_id()] = t;
230 scheduler->add_thread(t);
234 void ModelChecker::remove_thread(Thread *t)
236 scheduler->remove_thread(t);
239 int ModelChecker::switch_to_master(ModelAction *act)
244 old = thread_current();
245 set_current_action(act);
246 old->set_state(THREAD_READY);
247 next = system_thread;
248 return old->swap(next);
251 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
253 Thread *t = thread_current();
254 ModelAction *act = this;
259 act->tid = t->get_id();
263 void ModelAction::print(void)
265 const char *type_str;
266 switch (this->type) {
268 type_str = "thread create";
271 type_str = "thread yield";
274 type_str = "thread join";
277 type_str = "atomic read";
280 type_str = "atomic write";
283 type_str = "unknown type";
286 printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);