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 void 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);
74 * Checks if the Thread associated with this thread ID has been explored from
76 * @param tid is the thread ID to check
77 * @return true if this thread choice has been explored already, false
80 bool Node::has_been_explored(thread_id_t tid)
82 int id = id_to_int(tid);
83 return explored_children[id];
87 * Checks if the backtracking set is empty.
88 * @return true if the backtracking set is empty
90 bool Node::backtrack_empty()
92 return (numBacktracks == 0);
97 * Checks whether the readsfrom set for this node is empty.
98 * @return true if the readsfrom set is empty.
100 bool Node::readsfrom_empty() {
101 return ((read_from_index+1)>=may_read_from.size());
105 * Checks whether the future_values set for this node is empty.
106 * @return true if the future_values set is empty.
109 bool Node::futurevalues_empty() {
110 return ((future_index+1)>=future_values.size());
114 * Mark the appropriate backtracking information for exploring a thread choice.
115 * @param act The ModelAction to explore
117 void Node::explore_child(ModelAction *act)
119 explore(act->get_tid());
123 * Records a backtracking reference for a thread choice within this Node.
124 * Provides feedback as to whether this thread choice is already set for
126 * @return false if the thread was already set to be backtracked, true
129 bool Node::set_backtrack(thread_id_t id)
131 int i = id_to_int(id);
139 thread_id_t Node::get_next_backtrack()
141 /** @todo Find next backtrack */
143 for (i = 0; i < backtrack.size(); i++)
144 if (backtrack[i] == true)
146 /* Backtrack set was empty? */
147 ASSERT(i != backtrack.size());
149 backtrack[i] = false;
154 bool Node::is_enabled(Thread *t)
156 return id_to_int(t->get_id()) < num_threads;
160 * Add an action to the may_read_from set.
161 * @param act is the action to add
163 void Node::add_read_from(const ModelAction *act)
165 may_read_from.push_back(act);
169 * Gets the next 'future_value' value from this Node. Only valid for a node
170 * where this->action is a 'read'.
171 * @return The first element in future_values
174 uint64_t Node::get_future_value() {
175 ASSERT(future_index<future_values.size());
176 return future_values[future_index];
180 * Gets the next 'may_read_from' action from this Node. Only valid for a node
181 * where this->action is a 'read'.
182 * @todo Perform reads_from backtracking/replay properly, so that this function
183 * may remove elements from may_read_from
184 * @return The first element in may_read_from
186 const ModelAction * Node::get_read_from() {
187 ASSERT(read_from_index<may_read_from.size());
188 return may_read_from[read_from_index];
192 * Increments the index into the readsfrom set to explore the next item.
193 * @return Returns false if we have explored all items.
195 bool Node::increment_read_from() {
197 return (read_from_index<may_read_from.size());
201 * Increments the index into the future_values set to explore the next item.
202 * @return Returns false if we have explored all values.
205 bool Node::increment_future_values() {
207 return (future_index<future_values.size());
210 void Node::explore(thread_id_t tid)
212 int i = id_to_int(tid);
214 backtrack[i] = false;
217 explored_children[i] = true;
220 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
221 node_list_t::iterator end)
223 node_list_t::iterator it;
225 for (it = start; it != end; it++)
227 list->erase(start, end);
230 NodeStack::NodeStack()
233 node_list.push_back(new Node());
235 iter = node_list.begin();
238 NodeStack::~NodeStack()
240 clear_node_list(&node_list, node_list.begin(), node_list.end());
243 void NodeStack::print()
245 node_list_t::iterator it;
246 printf("............................................\n");
247 printf("NodeStack printing node_list:\n");
248 for (it = node_list.begin(); it != node_list.end(); it++) {
249 if (it == this->iter)
250 printf("vvv following action is the current iterator vvv\n");
253 printf("............................................\n");
256 ModelAction * NodeStack::explore_action(ModelAction *act)
260 ASSERT(!node_list.empty());
261 node_list_t::iterator it=iter;
264 if (it != node_list.end()) {
266 return (*iter)->get_action();
270 get_head()->explore_child(act);
271 node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
278 * Empties the stack of all trailing nodes after a given position and calls the
279 * destructor for each. This function is provided an offset which determines
280 * how many nodes (relative to the current replay state) to save before popping
282 * @param numAhead gives the number of Nodes (including this Node) to skip over
283 * before removing nodes.
285 void NodeStack::pop_restofstack(int numAhead)
287 /* Diverging from previous execution; clear out remainder of list */
288 node_list_t::iterator it = iter;
291 clear_node_list(&node_list, it, node_list.end());
294 Node * NodeStack::get_head()
296 if (node_list.empty())
301 Node * NodeStack::get_next()
303 node_list_t::iterator it = iter;
304 if (node_list.empty()) {
309 if (it == node_list.end()) {
316 void NodeStack::reset_execution()
318 iter = node_list.begin();