7 * @brief Node constructor
9 * Constructs a single Node for use in a NodeStack. Each Node is associated
10 * with exactly one ModelAction (exception: the first Node should be created
11 * as an empty stub, to represent the first thread "choice") and up to one
14 * @param act The ModelAction to associate with this Node. May be NULL.
15 * @param par The parent Node in the NodeStack. May be NULL if there is no
17 * @param nthreads The number of threads which exist at this point in the
20 Node::Node(ModelAction *act, Node *par, int nthreads)
23 num_threads(nthreads),
24 explored_children(num_threads),
25 backtrack(num_threads),
31 if (act && act->is_read())
32 may_read_from = new action_set_t();
35 /** @brief Node desctructor */
42 /** Prints debugging info for the ModelAction associated with this Node */
48 printf("******** empty action ********\n");
52 * Checks if the Thread associated with this thread ID has been explored from
54 * @param tid is the thread ID to check
55 * @return true if this thread choice has been explored already, false
58 bool Node::has_been_explored(thread_id_t tid)
60 int id = id_to_int(tid);
61 return explored_children[id];
65 * Checks if the backtracking set is empty.
66 * @return true if the backtracking set is empty
68 bool Node::backtrack_empty()
70 return numBacktracks == 0;
74 * Mark the appropriate backtracking infromation for exploring a thread choice.
75 * @param act The ModelAction to explore
77 void Node::explore_child(ModelAction *act)
79 explore(act->get_tid());
83 * Records a backtracking reference for a thread choice within this Node.
84 * Provides feedback as to whether this thread choice is already set for
86 * @return false if the thread was already set to be backtracked, true
89 bool Node::set_backtrack(thread_id_t id)
91 int i = id_to_int(id);
99 thread_id_t Node::get_next_backtrack()
101 /* TODO: find next backtrack */
103 for (i = 0; i < backtrack.size(); i++)
104 if (backtrack[i] == true)
106 if (i >= backtrack.size())
107 return THREAD_ID_T_NONE;
108 backtrack[i] = false;
113 bool Node::is_enabled(Thread *t)
115 return id_to_int(t->get_id()) < num_threads;
119 * Add an action to the may_read_from set.
120 * @param act is the action to add
122 void Node::add_read_from(ModelAction *act)
124 ASSERT(may_read_from);
125 may_read_from->insert(act);
128 void Node::explore(thread_id_t tid)
130 int i = id_to_int(tid);
132 backtrack[i] = false;
135 explored_children[i] = true;
138 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
139 node_list_t::iterator end)
141 node_list_t::iterator it;
143 for (it = start; it != end; it++)
145 list->erase(start, end);
148 NodeStack::NodeStack()
151 node_list.push_back(new Node());
153 iter = node_list.begin();
156 NodeStack::~NodeStack()
158 clear_node_list(&node_list, node_list.begin(), node_list.end());
161 void NodeStack::print()
163 node_list_t::iterator it;
164 printf("............................................\n");
165 printf("NodeStack printing node_list:\n");
166 for (it = node_list.begin(); it != node_list.end(); it++) {
167 if (it == this->iter)
168 printf("vvv following action is the current iterator vvv\n");
171 printf("............................................\n");
174 ModelAction * NodeStack::explore_action(ModelAction *act)
178 ASSERT(!node_list.empty());
180 if (get_head()->has_been_explored(act->get_tid())) {
182 return (*iter)->get_action();
185 /* Diverging from previous execution; clear out remainder of list */
186 node_list_t::iterator it = iter;
188 clear_node_list(&node_list, it, node_list.end());
191 get_head()->explore_child(act);
192 node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
198 Node * NodeStack::get_head()
200 if (node_list.empty())
205 Node * NodeStack::get_next()
207 node_list_t::iterator it = iter;
208 if (node_list.empty()) {
213 if (it == node_list.end()) {
220 void NodeStack::reset_execution()
222 iter = node_list.begin();