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);
53 ModelAction *ModelChecker::get_last_conflict(ModelAction *act)
55 void *loc = act->get_location();
56 action_type type = act->get_type();
57 thread_id_t id = act->get_tid();
69 action_list_t::reverse_iterator rit;
70 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
71 ModelAction *prev = *rit;
72 if (prev->get_location() != loc)
74 if (type == ATOMIC_READ && prev->get_type() != ATOMIC_WRITE)
76 /* Conflict from the same thread is not really a conflict */
77 if (id == prev->get_tid())
84 void ModelChecker::set_backtracking(ModelAction *act)
89 prev = get_last_conflict(act);
93 node = prev->get_node();
95 /* Check if this has been explored already */
96 if (node->hasBeenExplored(act->get_tid()))
98 /* If this is a new backtracking point, mark the tree */
99 if (node->setBacktrack(act->get_tid()) != 0)
102 printf("Setting backtrack: conflict = %d, instead tid = %d\n",
103 prev->get_tid(), act->get_tid());
107 Backtrack *back = new Backtrack(prev, action_trace);
108 backtrack_list.push_back(back);
111 void ModelChecker::check_current_action(void)
113 ModelAction *next = this->current_action;
116 DEBUG("trying to push NULL action...\n");
119 next->set_node(currentNode);
120 set_backtracking(next);
121 currentNode = currentNode->exploreChild(next->get_tid());
122 this->action_trace->push_back(next);
125 void ModelChecker::print_trace(void)
127 action_list_t::iterator it;
130 printf("---------------------------------------------------------------------\n");
131 printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
133 for (it = action_trace->begin(); it != action_trace->end(); it++) {
137 printf("---------------------------------------------------------------------\n");
140 int ModelChecker::add_thread(Thread *t)
142 thread_map[t->get_id()] = t;
146 int ModelChecker::switch_to_master(ModelAction *act)
151 old = thread_current();
152 set_current_action(act);
153 old->set_state(THREAD_READY);
154 next = system_thread;
155 return old->swap(next);
158 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
160 Thread *t = thread_current();
161 ModelAction *act = this;
166 act->tid = t->get_id();
170 void ModelAction::print(void)
172 const char *type_str;
173 switch (this->type) {
175 type_str = "thread create";
178 type_str = "thread yield";
181 type_str = "thread join";
184 type_str = "atomic read";
187 type_str = "atomic write";
190 type_str = "unknown type";
193 printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);