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++)
60 /** This method sets a promise to explore meeting with the given
62 * @param i is the promise index.
65 void Node::set_promise(uint32_t i) {
66 if (i>=promises.size())
67 promises.resize(i+1,0);
71 /** This method looks up whether a given promise should be satisfied
74 * @param i is the promise index.
75 * @return true if the promise should be satisfied by the given model action.
78 bool Node::get_promise(uint32_t i) {
79 return (i<promises.size())&&(promises[i]==2);
82 /** This method increments to the next combination of promises.
84 * @return true if we have a valid combination.
87 bool Node::increment_promise() {
88 for (unsigned int i=0;i<promises.size();i++) {
102 /** This method returns whether the promise set is empty.
104 * @return true if we have explored all promise combinations.
107 bool Node::promise_empty() {
108 for (unsigned int i=0;i<promises.size();i++)
115 * Adds a value from a weakly ordered future write to backtrack to.
116 * @param value is the value to backtrack to.
119 bool Node::add_future_value(uint64_t value) {
120 for(unsigned int i=0;i<future_values.size();i++)
121 if (future_values[i]==value)
124 future_values.push_back(value);
129 * Checks whether the future_values set for this node is empty.
130 * @return true if the future_values set is empty.
133 bool Node::future_value_empty() {
134 return ((future_index+1)>=future_values.size());
139 * Checks if the Thread associated with this thread ID has been explored from
141 * @param tid is the thread ID to check
142 * @return true if this thread choice has been explored already, false
145 bool Node::has_been_explored(thread_id_t tid)
147 int id = id_to_int(tid);
148 return explored_children[id];
152 * Checks if the backtracking set is empty.
153 * @return true if the backtracking set is empty
155 bool Node::backtrack_empty()
157 return (numBacktracks == 0);
162 * Checks whether the readsfrom set for this node is empty.
163 * @return true if the readsfrom set is empty.
165 bool Node::read_from_empty() {
166 return ((read_from_index+1)>=may_read_from.size());
172 * Mark the appropriate backtracking information for exploring a thread choice.
173 * @param act The ModelAction to explore
175 void Node::explore_child(ModelAction *act)
177 explore(act->get_tid());
181 * Records a backtracking reference for a thread choice within this Node.
182 * Provides feedback as to whether this thread choice is already set for
184 * @return false if the thread was already set to be backtracked, true
187 bool Node::set_backtrack(thread_id_t id)
189 int i = id_to_int(id);
197 thread_id_t Node::get_next_backtrack()
199 /** @todo Find next backtrack */
201 for (i = 0; i < backtrack.size(); i++)
202 if (backtrack[i] == true)
204 /* Backtrack set was empty? */
205 ASSERT(i != backtrack.size());
207 backtrack[i] = false;
212 bool Node::is_enabled(Thread *t)
214 return id_to_int(t->get_id()) < num_threads;
218 * Add an action to the may_read_from set.
219 * @param act is the action to add
221 void Node::add_read_from(const ModelAction *act)
223 may_read_from.push_back(act);
227 * Gets the next 'future_value' value from this Node. Only valid for a node
228 * where this->action is a 'read'.
229 * @return The first element in future_values
232 uint64_t Node::get_future_value() {
233 ASSERT(future_index<future_values.size());
234 return future_values[future_index];
238 * Gets the next 'may_read_from' action from this Node. Only valid for a node
239 * where this->action is a 'read'.
240 * @todo Perform reads_from backtracking/replay properly, so that this function
241 * may remove elements from may_read_from
242 * @return The first element in may_read_from
244 const ModelAction * Node::get_read_from() {
245 if (read_from_index<may_read_from.size())
246 return may_read_from[read_from_index];
252 * Increments the index into the readsfrom set to explore the next item.
253 * @return Returns false if we have explored all items.
255 bool Node::increment_read_from() {
257 return (read_from_index<may_read_from.size());
261 * Increments the index into the future_values set to explore the next item.
262 * @return Returns false if we have explored all values.
265 bool Node::increment_future_value() {
267 return (future_index<future_values.size());
270 void Node::explore(thread_id_t tid)
272 int i = id_to_int(tid);
274 backtrack[i] = false;
277 explored_children[i] = true;
280 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
281 node_list_t::iterator end)
283 node_list_t::iterator it;
285 for (it = start; it != end; it++)
287 list->erase(start, end);
290 NodeStack::NodeStack()
293 node_list.push_back(new Node());
295 iter = node_list.begin();
298 NodeStack::~NodeStack()
300 clear_node_list(&node_list, node_list.begin(), node_list.end());
303 void NodeStack::print()
305 node_list_t::iterator it;
306 printf("............................................\n");
307 printf("NodeStack printing node_list:\n");
308 for (it = node_list.begin(); it != node_list.end(); it++) {
309 if (it == this->iter)
310 printf("vvv following action is the current iterator vvv\n");
313 printf("............................................\n");
316 ModelAction * NodeStack::explore_action(ModelAction *act)
320 ASSERT(!node_list.empty());
321 node_list_t::iterator it=iter;
324 if (it != node_list.end()) {
326 return (*iter)->get_action();
330 get_head()->explore_child(act);
331 node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
338 * Empties the stack of all trailing nodes after a given position and calls the
339 * destructor for each. This function is provided an offset which determines
340 * how many nodes (relative to the current replay state) to save before popping
342 * @param numAhead gives the number of Nodes (including this Node) to skip over
343 * before removing nodes.
345 void NodeStack::pop_restofstack(int numAhead)
347 /* Diverging from previous execution; clear out remainder of list */
348 node_list_t::iterator it = iter;
351 clear_node_list(&node_list, it, node_list.end());
354 Node * NodeStack::get_head()
356 if (node_list.empty())
361 Node * NodeStack::get_next()
363 node_list_t::iterator it = iter;
364 if (node_list.empty()) {
369 if (it == node_list.end()) {
376 void NodeStack::reset_execution()
378 iter = node_list.begin();