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),
36 /** @brief Node desctructor */
43 /** Prints debugging info for the ModelAction associated with this Node */
49 printf("******** empty action ********\n");
52 /** @brief Prints info about may_read_from set */
53 void Node::print_may_read_from()
55 readfrom_set_t::iterator it;
56 for (it = may_read_from.begin(); it != may_read_from.end(); it++)
61 * Adds a value from a weakly ordered future write to backtrack to.
62 * @param value is the value to backtrack to.
65 bool Node::add_future_value(uint64_t value) {
66 for(int i=0;i<future_values.size();i++)
67 if (future_values[i]==value)
69 future_values.push_back(value);
75 * Checks if the Thread associated with this thread ID has been explored from
77 * @param tid is the thread ID to check
78 * @return true if this thread choice has been explored already, false
81 bool Node::has_been_explored(thread_id_t tid)
83 int id = id_to_int(tid);
84 return explored_children[id];
88 * Checks if the backtracking set is empty.
89 * @return true if the backtracking set is empty
91 bool Node::backtrack_empty()
93 return (numBacktracks == 0);
98 * Checks whether the readsfrom set for this node is empty.
99 * @return true if the readsfrom set is empty.
101 bool Node::readsfrom_empty() {
102 return ((read_from_index+1)>=may_read_from.size());
106 * Checks whether the future_values set for this node is empty.
107 * @return true if the future_values set is empty.
110 bool Node::futurevalues_empty() {
111 return ((future_index+1)>=future_values.size());
115 * Mark the appropriate backtracking information for exploring a thread choice.
116 * @param act The ModelAction to explore
118 void Node::explore_child(ModelAction *act)
120 explore(act->get_tid());
124 * Records a backtracking reference for a thread choice within this Node.
125 * Provides feedback as to whether this thread choice is already set for
127 * @return false if the thread was already set to be backtracked, true
130 bool Node::set_backtrack(thread_id_t id)
132 int i = id_to_int(id);
140 thread_id_t Node::get_next_backtrack()
142 /** @todo Find next backtrack */
144 for (i = 0; i < backtrack.size(); i++)
145 if (backtrack[i] == true)
147 /* Backtrack set was empty? */
148 ASSERT(i != backtrack.size());
150 backtrack[i] = false;
155 bool Node::is_enabled(Thread *t)
157 return id_to_int(t->get_id()) < num_threads;
161 * Add an action to the may_read_from set.
162 * @param act is the action to add
164 void Node::add_read_from(const ModelAction *act)
166 may_read_from.push_back(act);
170 * Gets the next 'future_value' value from this Node. Only valid for a node
171 * where this->action is a 'read'.
172 * @return The first element in future_values
175 uint64_t Node::get_future_value() {
176 ASSERT(future_index<future_values.size());
177 return future_values[future_index];
181 * Gets the next 'may_read_from' action from this Node. Only valid for a node
182 * where this->action is a 'read'.
183 * @todo Perform reads_from backtracking/replay properly, so that this function
184 * may remove elements from may_read_from
185 * @return The first element in may_read_from
187 const ModelAction * Node::get_read_from() {
188 if (read_from_index<may_read_from.size())
189 return may_read_from[read_from_index];
195 * Increments the index into the readsfrom set to explore the next item.
196 * @return Returns false if we have explored all items.
198 bool Node::increment_read_from() {
200 return (read_from_index<may_read_from.size());
204 * Increments the index into the future_values set to explore the next item.
205 * @return Returns false if we have explored all values.
208 bool Node::increment_future_values() {
210 return (future_index<future_values.size());
213 void Node::explore(thread_id_t tid)
215 int i = id_to_int(tid);
217 backtrack[i] = false;
220 explored_children[i] = true;
223 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
224 node_list_t::iterator end)
226 node_list_t::iterator it;
228 for (it = start; it != end; it++)
230 list->erase(start, end);
233 NodeStack::NodeStack()
236 node_list.push_back(new Node());
238 iter = node_list.begin();
241 NodeStack::~NodeStack()
243 clear_node_list(&node_list, node_list.begin(), node_list.end());
246 void NodeStack::print()
248 node_list_t::iterator it;
249 printf("............................................\n");
250 printf("NodeStack printing node_list:\n");
251 for (it = node_list.begin(); it != node_list.end(); it++) {
252 if (it == this->iter)
253 printf("vvv following action is the current iterator vvv\n");
256 printf("............................................\n");
259 ModelAction * NodeStack::explore_action(ModelAction *act)
263 ASSERT(!node_list.empty());
264 node_list_t::iterator it=iter;
267 if (it != node_list.end()) {
269 return (*iter)->get_action();
273 get_head()->explore_child(act);
274 node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
281 * Empties the stack of all trailing nodes after a given position and calls the
282 * destructor for each. This function is provided an offset which determines
283 * how many nodes (relative to the current replay state) to save before popping
285 * @param numAhead gives the number of Nodes (including this Node) to skip over
286 * before removing nodes.
288 void NodeStack::pop_restofstack(int numAhead)
290 /* Diverging from previous execution; clear out remainder of list */
291 node_list_t::iterator it = iter;
294 clear_node_list(&node_list, it, node_list.end());
297 Node * NodeStack::get_head()
299 if (node_list.empty())
304 Node * NodeStack::get_next()
306 node_list_t::iterator it = iter;
307 if (node_list.empty()) {
312 if (it == node_list.end()) {
319 void NodeStack::reset_execution()
321 iter = node_list.begin();