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 ModelAction *ModelChecker::get_last_conflict(ModelAction *act)
98 void *loc = act->get_location();
99 action_type type = act->get_type();
100 thread_id_t id = act->get_tid();
112 /* linear search: from most recent to oldest */
113 action_list_t::reverse_iterator rit;
114 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
115 ModelAction *prev = *rit;
116 if (prev->get_location() != loc)
118 if (type == ATOMIC_READ && prev->get_type() != ATOMIC_WRITE)
120 /* Conflict from the same thread is not really a conflict */
121 if (id == prev->get_tid())
128 void ModelChecker::set_backtracking(ModelAction *act)
133 prev = get_last_conflict(act);
137 node = prev->get_node();
139 /* Check if this has been explored already */
140 if (node->hasBeenExplored(act->get_tid()))
142 /* If this is a new backtracking point, mark the tree */
143 if (node->setBacktrack(act->get_tid()) != 0)
146 printf("Setting backtrack: conflict = %d, instead tid = %d\n",
147 prev->get_tid(), act->get_tid());
151 Backtrack *back = new Backtrack(prev, action_trace);
152 backtrack_list.push_back(back);
155 void ModelChecker::check_current_action(void)
157 ModelAction *next = this->current_action;
160 DEBUG("trying to push NULL action...\n");
163 nextThread = advance_backtracking_state();
164 next->set_node(currentNode);
165 set_backtracking(next);
166 currentNode = currentNode->exploreChild(next->get_tid());
167 this->action_trace->push_back(next);
170 void ModelChecker::print_trace(void)
172 action_list_t::iterator it;
175 printf("---------------------------------------------------------------------\n");
176 printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
180 printf("\nTrace:\n\n");
182 for (it = action_trace->begin(); it != action_trace->end(); it++) {
186 printf("---------------------------------------------------------------------\n");
189 int ModelChecker::add_thread(Thread *t)
191 thread_map[t->get_id()] = t;
195 int ModelChecker::switch_to_master(ModelAction *act)
200 old = thread_current();
201 set_current_action(act);
202 old->set_state(THREAD_READY);
203 next = system_thread;
204 return old->swap(next);
207 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
209 Thread *t = thread_current();
210 ModelAction *act = this;
215 act->tid = t->get_id();
219 void ModelAction::print(void)
221 const char *type_str;
222 switch (this->type) {
224 type_str = "thread create";
227 type_str = "thread yield";
230 type_str = "thread join";
233 type_str = "atomic read";
236 type_str = "atomic write";
239 type_str = "unknown type";
242 printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);