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 std::list<class ModelAction *>();
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 std::list<class ModelAction *>::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());
95 //Backtrack *back = new Backtrack(prev, actionList);
96 //backtrackList->Append(back);
99 void ModelChecker::check_current_action(void)
101 ModelAction *next = this->current_action;
104 DEBUG("trying to push NULL action...\n");
107 next->set_node(currentNode);
108 set_backtracking(next);
109 currentNode = currentNode->exploreChild(next->get_tid());
110 this->action_trace->push_back(next);
113 void ModelChecker::print_trace(void)
115 std::list<class ModelAction *>::iterator it;
118 printf("---------------------------------------------------------------------\n");
119 printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
121 for (it = action_trace->begin(); it != action_trace->end(); it++) {
125 printf("---------------------------------------------------------------------\n");
128 int ModelChecker::add_thread(Thread *t)
130 thread_map[t->get_id()] = t;
134 int ModelChecker::switch_to_master(ModelAction *act)
139 old = thread_current();
140 set_current_action(act);
141 old->set_state(THREAD_READY);
142 next = system_thread;
143 return old->swap(next);
146 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
148 Thread *t = thread_current();
149 ModelAction *act = this;
154 act->tid = t->get_id();
158 void ModelAction::print(void)
160 const char *type_str;
161 switch (this->type) {
163 type_str = "thread create";
166 type_str = "thread yield";
169 type_str = "thread join";
172 type_str = "atomic read";
175 type_str = "atomic write";
178 type_str = "unknown type";
181 printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);