9 #define INITIAL_THREAD_ID 0
13 ModelChecker::ModelChecker()
15 /* First thread created will have id (INITIAL_THREAD_ID + 1) */
16 this->used_thread_id = INITIAL_THREAD_ID;
17 /* Initialize default scheduler */
18 this->scheduler = new Scheduler();
21 this->current_action = NULL;
22 this->exploring = NULL;
23 this->nextThread = THREAD_ID_T_NONE;
25 rootNode = new TreeNode(NULL);
26 currentNode = rootNode;
27 action_trace = new action_list_t();
30 ModelChecker::~ModelChecker()
33 delete this->scheduler;
37 void ModelChecker::reset_to_initial_state()
39 DEBUG("+++ Resetting to initial state +++\n");
40 std::map<thread_id_t, class Thread *>::iterator it;
41 for (it = thread_map.begin(); it != thread_map.end(); it++) {
45 action_trace = new action_list_t();
46 currentNode = rootNode;
47 current_action = NULL;
48 used_thread_id = INITIAL_THREAD_ID;
49 /* scheduler reset ? */
52 int ModelChecker::get_next_id()
54 return ++used_thread_id;
57 Thread * ModelChecker::schedule_next_thread()
60 if (nextThread == THREAD_ID_T_NONE)
62 t = thread_map[nextThread];
64 DEBUG("*** error: thread not in thread_map: id = %d\n", nextThread);
69 * get_next_replay_thread() - Choose the next thread in the replay sequence
71 * If we've reached the 'diverge' point, then we pick a thread from the
73 * Otherwise, we simply return the next thread in the sequence.
75 thread_id_t ModelChecker::get_next_replay_thread()
80 next = exploring->get_state();
82 if (next == exploring->get_diverge()) {
83 TreeNode *node = next->get_node();
85 /* Reached divergence point; discard our current 'exploring' */
86 DEBUG("*** Discard 'Backtrack' object ***\n");
87 tid = node->getNextBacktrack();
91 tid = next->get_tid();
93 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
97 thread_id_t ModelChecker::advance_backtracking_state()
99 /* Have we completed exploring the preselected path? */
100 if (exploring == NULL)
101 return THREAD_ID_T_NONE;
103 /* Else, we are trying to replay an execution */
104 exploring->advance_state();
105 if (exploring->get_state() == NULL)
106 DEBUG("*** error: reached end of backtrack trace\n");
108 return get_next_replay_thread();
111 bool ModelChecker::next_execution()
115 if ((exploring = model->get_next_backtrack()) == NULL)
117 model->reset_to_initial_state();
118 nextThread = get_next_replay_thread();
122 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
124 action_type type = act->get_type();
136 /* linear search: from most recent to oldest */
137 action_list_t::reverse_iterator rit;
138 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
139 ModelAction *prev = *rit;
140 if (act->is_dependent(prev))
146 void ModelChecker::set_backtracking(ModelAction *act)
151 prev = get_last_conflict(act);
155 node = prev->get_node();
157 /* Check if this has been explored already */
158 if (node->hasBeenExplored(act->get_tid()))
160 /* If this is a new backtracking point, mark the tree */
161 if (node->setBacktrack(act->get_tid()) != 0)
164 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
165 prev->get_tid(), act->get_tid());
171 Backtrack *back = new Backtrack(prev, action_trace);
172 backtrack_list.push_back(back);
175 Backtrack * ModelChecker::get_next_backtrack()
178 if (backtrack_list.empty())
180 next = backtrack_list.back();
181 backtrack_list.pop_back();
185 void ModelChecker::check_current_action(void)
187 ModelAction *next = this->current_action;
190 DEBUG("trying to push NULL action...\n");
193 current_action = NULL;
194 nextThread = advance_backtracking_state();
195 next->set_node(currentNode);
196 set_backtracking(next);
197 currentNode = currentNode->exploreChild(next->get_tid());
198 this->action_trace->push_back(next);
201 void ModelChecker::print_summary(void)
203 action_list_t::iterator it;
206 printf("---------------------------------------------------------------------\n");
207 printf("Number of executions: %d\n", num_executions);
208 printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
212 printf("Trace:\n\n");
214 for (it = action_trace->begin(); it != action_trace->end(); it++) {
218 printf("---------------------------------------------------------------------\n");
221 int ModelChecker::add_thread(Thread *t)
223 thread_map[t->get_id()] = t;
224 scheduler->add_thread(t);
228 void ModelChecker::remove_thread(Thread *t)
230 scheduler->remove_thread(t);
233 int ModelChecker::switch_to_master(ModelAction *act)
238 old = thread_current();
239 set_current_action(act);
240 old->set_state(THREAD_READY);
241 return Thread::swap(old, get_system_context());
244 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
246 Thread *t = thread_current();
247 ModelAction *act = this;
252 act->tid = t->get_id();
256 bool ModelAction::is_read()
258 return type == ATOMIC_READ;
261 bool ModelAction::is_write()
263 return type == ATOMIC_WRITE;
266 bool ModelAction::is_acquire()
269 case memory_order_acquire:
270 case memory_order_acq_rel:
271 case memory_order_seq_cst:
278 bool ModelAction::is_release()
281 case memory_order_release:
282 case memory_order_acq_rel:
283 case memory_order_seq_cst:
290 bool ModelAction::same_var(ModelAction *act)
292 return location == act->location;
295 bool ModelAction::same_thread(ModelAction *act)
297 return tid == act->tid;
300 bool ModelAction::is_dependent(ModelAction *act)
302 if (!is_read() && !is_write())
304 if (!act->is_read() && !act->is_write())
306 if (same_var(act) && !same_thread(act) &&
307 (is_write() || act->is_write()))
312 void ModelAction::print(void)
314 const char *type_str;
315 switch (this->type) {
317 type_str = "thread create";
320 type_str = "thread yield";
323 type_str = "thread join";
326 type_str = "atomic read";
329 type_str = "atomic write";
332 type_str = "unknown type";
335 printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);