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;
18 rootNode = new TreeNode(NULL);
19 currentNode = rootNode;
20 action_trace = new action_list_t();
23 ModelChecker::~ModelChecker()
26 delete this->scheduler;
30 void ModelChecker::assign_id(Thread *t)
32 t->set_id(++used_thread_id);
35 void ModelChecker::add_system_thread(Thread *t)
37 this->system_thread = t;
40 ModelAction *ModelChecker::get_last_conflict(ModelAction *act)
42 void *loc = act->get_location();
43 action_type type = act->get_type();
44 thread_id_t id = act->get_tid();
56 action_list_t::reverse_iterator rit;
57 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
58 ModelAction *prev = *rit;
59 if (prev->get_location() != loc)
61 if (type == ATOMIC_READ && prev->get_type() != ATOMIC_WRITE)
63 /* Conflict from the same thread is not really a conflict */
64 if (id == prev->get_tid())
71 void ModelChecker::set_backtracking(ModelAction *act)
76 prev = get_last_conflict(act);
80 node = prev->get_node();
82 /* Check if this has been explored already */
83 if (node->hasBeenExplored(act->get_tid()))
85 /* If this is a new backtracking point, mark the tree */
86 if (node->setBacktrack(act->get_tid()) != 0)
89 printf("Setting backtrack: conflict = %d, instead tid = %d\n",
90 prev->get_tid(), act->get_tid());
94 Backtrack *back = new Backtrack(prev, action_trace);
95 backtrack_list.push_back(back);
98 void ModelChecker::check_current_action(void)
100 ModelAction *next = this->current_action;
103 DEBUG("trying to push NULL action...\n");
106 next->set_node(currentNode);
107 set_backtracking(next);
108 currentNode = currentNode->exploreChild(next->get_tid());
109 this->action_trace->push_back(next);
112 void ModelChecker::print_trace(void)
114 action_list_t::iterator it;
117 printf("---------------------------------------------------------------------\n");
118 printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
120 for (it = action_trace->begin(); it != action_trace->end(); it++) {
124 printf("---------------------------------------------------------------------\n");
127 int ModelChecker::add_thread(Thread *t)
129 thread_map[t->get_id()] = t;
133 int ModelChecker::switch_to_master(ModelAction *act)
138 old = thread_current();
139 set_current_action(act);
140 old->set_state(THREAD_READY);
141 next = system_thread;
142 return old->swap(next);
145 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
147 Thread *t = thread_current();
148 ModelAction *act = this;
153 act->tid = t->get_id();
157 void ModelAction::print(void)
159 const char *type_str;
160 switch (this->type) {
162 type_str = "thread create";
165 type_str = "thread yield";
168 type_str = "thread join";
171 type_str = "atomic read";
174 type_str = "atomic write";
177 type_str = "unknown type";
180 printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);