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::assign_id(Thread *t)
35 t->set_id(++used_thread_id);
38 void ModelChecker::add_system_thread(Thread *t)
40 this->system_thread = t;
43 Thread *ModelChecker::schedule_next_thread()
46 if (nextThread == THREAD_ID_T_NONE)
48 t = thread_map[nextThread];
50 DEBUG("*** error: thread not in thread_map: id = %d\n", nextThread);
55 * get_next_replay_thread() - Choose the next thread in the replay sequence
57 * If we've reached the 'diverge' point, then we pick a thread from the
59 * Otherwise, we simply return the next thread in the sequence.
61 thread_id_t ModelChecker::get_next_replay_thread()
66 next = exploring->get_state();
68 if (next == exploring->get_diverge()) {
69 TreeNode *node = next->get_node();
71 /* Reached divergence point; discard our current 'exploring' */
72 DEBUG("*** Discard 'Backtrack' object ***\n");
73 tid = node->getNextBacktrack();
77 tid = next->get_tid();
79 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
83 thread_id_t ModelChecker::advance_backtracking_state()
85 /* Have we completed exploring the preselected path? */
86 if (exploring == NULL)
87 return THREAD_ID_T_NONE;
89 /* Else, we are trying to replay an execution */
90 exploring->advance_state();
91 if (exploring->get_state() == NULL)
92 DEBUG("*** error: reached end of backtrack trace\n");
94 return get_next_replay_thread();
97 bool ModelChecker::next_execution()
102 ModelAction *ModelChecker::get_last_conflict(ModelAction *act)
104 void *loc = act->get_location();
105 action_type type = act->get_type();
106 thread_id_t id = act->get_tid();
118 /* linear search: from most recent to oldest */
119 action_list_t::reverse_iterator rit;
120 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
121 ModelAction *prev = *rit;
122 if (prev->get_location() != loc)
124 if (type == ATOMIC_READ && prev->get_type() != ATOMIC_WRITE)
126 /* Conflict from the same thread is not really a conflict */
127 if (id == prev->get_tid())
134 void ModelChecker::set_backtracking(ModelAction *act)
139 prev = get_last_conflict(act);
143 node = prev->get_node();
145 /* Check if this has been explored already */
146 if (node->hasBeenExplored(act->get_tid()))
148 /* If this is a new backtracking point, mark the tree */
149 if (node->setBacktrack(act->get_tid()) != 0)
152 printf("Setting backtrack: conflict = %d, instead tid = %d\n",
153 prev->get_tid(), act->get_tid());
157 Backtrack *back = new Backtrack(prev, action_trace);
158 backtrack_list.push_back(back);
161 void ModelChecker::check_current_action(void)
163 ModelAction *next = this->current_action;
166 DEBUG("trying to push NULL action...\n");
169 nextThread = advance_backtracking_state();
170 next->set_node(currentNode);
171 set_backtracking(next);
172 currentNode = currentNode->exploreChild(next->get_tid());
173 this->action_trace->push_back(next);
176 void ModelChecker::print_trace(void)
178 action_list_t::iterator it;
181 printf("---------------------------------------------------------------------\n");
182 printf("Number of executions: %d\n", num_executions);
183 printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
187 printf("\nTrace:\n\n");
189 for (it = action_trace->begin(); it != action_trace->end(); it++) {
193 printf("---------------------------------------------------------------------\n");
196 int ModelChecker::add_thread(Thread *t)
198 thread_map[t->get_id()] = t;
202 int ModelChecker::switch_to_master(ModelAction *act)
207 old = thread_current();
208 set_current_action(act);
209 old->set_state(THREAD_READY);
210 next = system_thread;
211 return old->swap(next);
214 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
216 Thread *t = thread_current();
217 ModelAction *act = this;
222 act->tid = t->get_id();
226 void ModelAction::print(void)
228 const char *type_str;
229 switch (this->type) {
231 type_str = "thread create";
234 type_str = "thread yield";
237 type_str = "thread join";
240 type_str = "atomic read";
243 type_str = "atomic write";
246 type_str = "unknown type";
249 printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);