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();
16 this->current_action = NULL;
17 this->exploring = NULL;
18 this->nextThread = THREAD_ID_T_NONE;
20 rootNode = new TreeNode(NULL);
21 currentNode = rootNode;
22 action_trace = new action_list_t();
25 ModelChecker::~ModelChecker()
28 delete this->scheduler;
32 void ModelChecker::assign_id(Thread *t)
34 t->set_id(++used_thread_id);
37 void ModelChecker::add_system_thread(Thread *t)
39 this->system_thread = t;
42 Thread *ModelChecker::schedule_next_thread()
45 if (nextThread == THREAD_ID_T_NONE)
47 t = thread_map[nextThread];
49 DEBUG("*** error: thread not in thread_map: id = %d\n", nextThread);
54 * get_next_replay_thread() - Choose the next thread in the replay sequence
56 * If we've reached the 'diverge' point, then we pick a thread from the
58 * Otherwise, we simply return the next thread in the sequence.
60 thread_id_t ModelChecker::get_next_replay_thread()
65 next = exploring->get_state();
67 if (next == exploring->get_diverge()) {
68 TreeNode *node = next->get_node();
70 /* Reached divergence point; discard our current 'exploring' */
71 DEBUG("*** Discard 'Backtrack' object ***\n");
72 tid = node->getNextBacktrack();
76 tid = next->get_tid();
78 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
82 thread_id_t ModelChecker::advance_backtracking_state()
84 /* Have we completed exploring the preselected path? */
85 if (exploring == NULL)
86 return THREAD_ID_T_NONE;
88 /* Else, we are trying to replay an execution */
89 exploring->advance_state();
90 if (exploring->get_state() == NULL)
91 DEBUG("*** error: reached end of backtrack trace\n");
93 return get_next_replay_thread();
96 bool ModelChecker::next_execution()
101 ModelAction *ModelChecker::get_last_conflict(ModelAction *act)
103 void *loc = act->get_location();
104 action_type type = act->get_type();
105 thread_id_t id = act->get_tid();
117 /* linear search: from most recent to oldest */
118 action_list_t::reverse_iterator rit;
119 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
120 ModelAction *prev = *rit;
121 if (prev->get_location() != loc)
123 if (type == ATOMIC_READ && prev->get_type() != ATOMIC_WRITE)
125 /* Conflict from the same thread is not really a conflict */
126 if (id == prev->get_tid())
133 void ModelChecker::set_backtracking(ModelAction *act)
138 prev = get_last_conflict(act);
142 node = prev->get_node();
144 /* Check if this has been explored already */
145 if (node->hasBeenExplored(act->get_tid()))
147 /* If this is a new backtracking point, mark the tree */
148 if (node->setBacktrack(act->get_tid()) != 0)
151 printf("Setting backtrack: conflict = %d, instead tid = %d\n",
152 prev->get_tid(), act->get_tid());
156 Backtrack *back = new Backtrack(prev, action_trace);
157 backtrack_list.push_back(back);
160 void ModelChecker::check_current_action(void)
162 ModelAction *next = this->current_action;
165 DEBUG("trying to push NULL action...\n");
168 nextThread = advance_backtracking_state();
169 next->set_node(currentNode);
170 set_backtracking(next);
171 currentNode = currentNode->exploreChild(next->get_tid());
172 this->action_trace->push_back(next);
175 void ModelChecker::print_trace(void)
177 action_list_t::iterator it;
180 printf("---------------------------------------------------------------------\n");
181 printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
185 printf("\nTrace:\n\n");
187 for (it = action_trace->begin(); it != action_trace->end(); it++) {
191 printf("---------------------------------------------------------------------\n");
194 int ModelChecker::add_thread(Thread *t)
196 thread_map[t->get_id()] = t;
200 int ModelChecker::switch_to_master(ModelAction *act)
205 old = thread_current();
206 set_current_action(act);
207 old->set_state(THREAD_READY);
208 next = system_thread;
209 return old->swap(next);
212 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
214 Thread *t = thread_current();
215 ModelAction *act = this;
220 act->tid = t->get_id();
224 void ModelAction::print(void)
226 const char *type_str;
227 switch (this->type) {
229 type_str = "thread create";
232 type_str = "thread yield";
235 type_str = "thread join";
238 type_str = "atomic read";
241 type_str = "atomic write";
244 type_str = "unknown type";
247 printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);