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++)
44 action_trace = new action_list_t();
45 currentNode = rootNode;
46 current_action = NULL;
47 used_thread_id = INITIAL_THREAD_ID;
48 /* scheduler reset ? */
51 int ModelChecker::get_next_id()
53 return ++used_thread_id;
56 Thread * ModelChecker::schedule_next_thread()
59 if (nextThread == THREAD_ID_T_NONE)
61 t = thread_map[nextThread];
63 DEBUG("*** error: thread not in thread_map: id = %d\n", nextThread);
68 * get_next_replay_thread() - Choose the next thread in the replay sequence
70 * If we've reached the 'diverge' point, then we pick a thread from the
72 * Otherwise, we simply return the next thread in the sequence.
74 thread_id_t ModelChecker::get_next_replay_thread()
79 next = exploring->get_state();
81 if (next == exploring->get_diverge()) {
82 TreeNode *node = next->get_node();
84 /* Reached divergence point; discard our current 'exploring' */
85 DEBUG("*** Discard 'Backtrack' object ***\n");
86 tid = node->getNextBacktrack();
90 tid = next->get_tid();
92 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
96 thread_id_t ModelChecker::advance_backtracking_state()
98 /* Have we completed exploring the preselected path? */
99 if (exploring == NULL)
100 return THREAD_ID_T_NONE;
102 /* Else, we are trying to replay an execution */
103 exploring->advance_state();
104 if (exploring->get_state() == NULL)
105 DEBUG("*** error: reached end of backtrack trace\n");
107 return get_next_replay_thread();
110 bool ModelChecker::next_execution()
114 if ((exploring = model->get_next_backtrack()) == NULL)
116 model->reset_to_initial_state();
117 nextThread = get_next_replay_thread();
121 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
123 action_type type = act->get_type();
135 /* linear search: from most recent to oldest */
136 action_list_t::reverse_iterator rit;
137 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
138 ModelAction *prev = *rit;
139 if (act->is_dependent(prev))
145 void ModelChecker::set_backtracking(ModelAction *act)
150 prev = get_last_conflict(act);
154 node = prev->get_node();
156 /* Check if this has been explored already */
157 if (node->hasBeenExplored(act->get_tid()))
159 /* If this is a new backtracking point, mark the tree */
160 if (node->setBacktrack(act->get_tid()) != 0)
163 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
164 prev->get_tid(), act->get_tid());
170 Backtrack *back = new Backtrack(prev, action_trace);
171 backtrack_list.push_back(back);
174 Backtrack * ModelChecker::get_next_backtrack()
177 if (backtrack_list.empty())
179 next = backtrack_list.back();
180 backtrack_list.pop_back();
184 void ModelChecker::check_current_action(void)
186 ModelAction *next = this->current_action;
189 DEBUG("trying to push NULL action...\n");
192 current_action = NULL;
193 nextThread = advance_backtracking_state();
194 next->set_node(currentNode);
195 set_backtracking(next);
196 currentNode = currentNode->exploreChild(next->get_tid());
197 this->action_trace->push_back(next);
200 void ModelChecker::print_summary(void)
202 action_list_t::iterator it;
205 printf("---------------------------------------------------------------------\n");
206 printf("Number of executions: %d\n", num_executions);
207 printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
211 printf("Trace:\n\n");
213 for (it = action_trace->begin(); it != action_trace->end(); it++) {
217 printf("---------------------------------------------------------------------\n");
220 int ModelChecker::add_thread(Thread *t)
222 thread_map[t->get_id()] = t;
223 scheduler->add_thread(t);
227 void ModelChecker::remove_thread(Thread *t)
229 scheduler->remove_thread(t);
232 int ModelChecker::switch_to_master(ModelAction *act)
237 old = thread_current();
238 set_current_action(act);
239 old->set_state(THREAD_READY);
240 return Thread::swap(old, get_system_context());
243 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
245 Thread *t = thread_current();
246 ModelAction *act = this;
251 act->tid = t->get_id();
255 bool ModelAction::is_read()
257 return type == ATOMIC_READ;
260 bool ModelAction::is_write()
262 return type == ATOMIC_WRITE;
265 bool ModelAction::is_acquire()
268 case memory_order_acquire:
269 case memory_order_acq_rel:
270 case memory_order_seq_cst:
277 bool ModelAction::is_release()
280 case memory_order_release:
281 case memory_order_acq_rel:
282 case memory_order_seq_cst:
289 bool ModelAction::same_var(ModelAction *act)
291 return location == act->location;
294 bool ModelAction::same_thread(ModelAction *act)
296 return tid == act->tid;
299 bool ModelAction::is_dependent(ModelAction *act)
301 if (!is_read() && !is_write())
303 if (!act->is_read() && !act->is_write())
305 if (same_var(act) && !same_thread(act) &&
306 (is_write() || act->is_write()))
311 void ModelAction::print(void)
313 const char *type_str;
314 switch (this->type) {
316 type_str = "thread create";
319 type_str = "thread yield";
322 type_str = "thread join";
325 type_str = "atomic read";
328 type_str = "atomic write";
331 type_str = "unknown type";
334 printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);