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),
34 /** @brief Node desctructor */
41 /** Prints debugging info for the ModelAction associated with this Node */
47 printf("******** empty action ********\n");
50 /** @brief Prints info about may_read_from set */
51 void Node::print_may_read_from()
53 readfrom_set_t::iterator it;
54 for (it = may_read_from.begin(); it != may_read_from.end(); it++)
59 * Checks if the Thread associated with this thread ID has been explored from
61 * @param tid is the thread ID to check
62 * @return true if this thread choice has been explored already, false
65 bool Node::has_been_explored(thread_id_t tid)
67 int id = id_to_int(tid);
68 return explored_children[id];
72 * Checks if the backtracking set is empty.
73 * @return true if the backtracking set is empty
75 bool Node::backtrack_empty()
77 return (numBacktracks == 0);
82 * Checks whether the readsfrom set for this node is empty.
83 * @return true if the readsfrom set is empty.
85 bool Node::readsfrom_empty() {
86 return ((read_from_index+1)>=may_read_from.size());
90 * Mark the appropriate backtracking infromation for exploring a thread choice.
91 * @param act The ModelAction to explore
93 void Node::explore_child(ModelAction *act)
95 explore(act->get_tid());
99 * Records a backtracking reference for a thread choice within this Node.
100 * Provides feedback as to whether this thread choice is already set for
102 * @return false if the thread was already set to be backtracked, true
105 bool Node::set_backtrack(thread_id_t id)
107 int i = id_to_int(id);
115 thread_id_t Node::get_next_backtrack()
117 /** @todo Find next backtrack */
119 for (i = 0; i < backtrack.size(); i++)
120 if (backtrack[i] == true)
122 /* Backtrack set was empty? */
123 ASSERT(i != backtrack.size());
125 backtrack[i] = false;
130 bool Node::is_enabled(Thread *t)
132 return id_to_int(t->get_id()) < num_threads;
136 * Add an action to the may_read_from set.
137 * @param act is the action to add
139 void Node::add_read_from(const ModelAction *act)
141 may_read_from.push_back(act);
145 * Gets the next 'may_read_from' action from this Node. Only valid for a node
146 * where this->action is a 'read'.
147 * @todo Perform reads_from backtracking/replay properly, so that this function
148 * may remove elements from may_read_from
149 * @return The first element in may_read_from
151 const ModelAction * Node::get_read_from() {
152 ASSERT(read_from_index<may_read_from.size());
153 return may_read_from[read_from_index];
157 * Increments the index into the readsfrom set to explore the next item.
158 * @return Returns false if we have explored all items.
160 bool Node::increment_read_from() {
162 return (read_from_index<may_read_from.size());
165 void Node::explore(thread_id_t tid)
167 int i = id_to_int(tid);
169 backtrack[i] = false;
172 explored_children[i] = true;
175 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
176 node_list_t::iterator end)
178 node_list_t::iterator it;
180 for (it = start; it != end; it++)
182 list->erase(start, end);
185 NodeStack::NodeStack()
188 node_list.push_back(new Node());
190 iter = node_list.begin();
193 NodeStack::~NodeStack()
195 clear_node_list(&node_list, node_list.begin(), node_list.end());
198 void NodeStack::print()
200 node_list_t::iterator it;
201 printf("............................................\n");
202 printf("NodeStack printing node_list:\n");
203 for (it = node_list.begin(); it != node_list.end(); it++) {
204 if (it == this->iter)
205 printf("vvv following action is the current iterator vvv\n");
208 printf("............................................\n");
211 ModelAction * NodeStack::explore_action(ModelAction *act)
215 ASSERT(!node_list.empty());
216 node_list_t::iterator it=iter;
219 if (it != node_list.end()) {
221 return (*iter)->get_action();
225 get_head()->explore_child(act);
226 node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
233 * Empties the stack of all trailing nodes after a given position and calls the
234 * destructor for each. This function is provided an offset which determines
235 * how many nodes (relative to the current replay state) to save before popping
237 * @param numAhead gives the number of Nodes (including this Node) to skip over
238 * before removing nodes.
240 void NodeStack::pop_restofstack(int numAhead)
242 /* Diverging from previous execution; clear out remainder of list */
243 node_list_t::iterator it = iter;
246 clear_node_list(&node_list, it, node_list.end());
249 Node * NodeStack::get_head()
251 if (node_list.empty())
256 Node * NodeStack::get_next()
258 node_list_t::iterator it = iter;
259 if (node_list.empty()) {
264 if (it == node_list.end()) {
271 void NodeStack::reset_execution()
273 iter = node_list.begin();