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;
22 ModelChecker::~ModelChecker()
24 delete this->scheduler;
28 void ModelChecker::assign_id(Thread *t)
30 t->set_id(++used_thread_id);
33 void ModelChecker::add_system_thread(Thread *t)
35 this->system_thread = t;
38 ModelAction *ModelChecker::get_last_conflict(ModelAction *act)
40 void *loc = act->get_location();
41 action_type type = act->get_type();
42 thread_id_t id = act->get_tid();
54 std::list<class ModelAction *>::reverse_iterator rit;
55 for (rit = action_trace.rbegin(); rit != action_trace.rend(); rit++) {
56 ModelAction *prev = *rit;
57 if (prev->get_location() != loc)
59 if (type == ATOMIC_READ && prev->get_type() != ATOMIC_WRITE)
61 /* Conflict from the same thread is not really a conflict */
62 if (id == prev->get_tid())
69 void ModelChecker::set_backtracking(ModelAction *act)
74 prev = get_last_conflict(act);
78 node = prev->get_node();
80 /* Check if this has been explored already */
81 if (node->hasBeenExplored(act->get_tid()))
83 /* If this is a new backtracking point, mark the tree */
84 if (node->setBacktrack(act->get_tid()) != 0)
87 printf("Setting backtrack: conflict = %d, instead tid = %d\n",
88 prev->get_tid(), act->get_tid());
93 //Backtrack *back = new Backtrack(prev, actionList);
94 //backtrackList->Append(back);
97 void ModelChecker::check_current_action(void)
99 ModelAction *next = this->current_action;
102 DEBUG("trying to push NULL action...\n");
105 next->set_node(currentNode);
106 set_backtracking(next);
107 currentNode = currentNode->exploreChild(next->get_tid());
108 this->action_trace.push_back(next);
111 void ModelChecker::print_trace(void)
113 std::list<class ModelAction *>::iterator it;
115 for (it = action_trace.begin(); it != action_trace.end(); it++) {
121 int ModelChecker::add_thread(Thread *t)
123 thread_map[t->get_id()] = t;
127 int ModelChecker::switch_to_master(ModelAction *act)
132 old = thread_current();
133 set_current_action(act);
134 old->set_state(THREAD_READY);
135 next = system_thread;
136 return old->swap(next);
139 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
141 Thread *t = thread_current();
142 ModelAction *act = this;
147 act->tid = t->get_id();
151 void ModelAction::print(void)
153 const char *type_str;
154 switch (this->type) {
156 type_str = "thread create";
159 type_str = "thread yield";
162 type_str = "thread join";
165 type_str = "atomic read";
168 type_str = "atomic write";
171 type_str = "unknown type";
174 printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);