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);
80 bool Node::readsfrom_empty() {
81 return ((read_from_index+1)>=may_read_from.size());
85 * Mark the appropriate backtracking infromation for exploring a thread choice.
86 * @param act The ModelAction to explore
88 void Node::explore_child(ModelAction *act)
90 explore(act->get_tid());
94 * Records a backtracking reference for a thread choice within this Node.
95 * Provides feedback as to whether this thread choice is already set for
97 * @return false if the thread was already set to be backtracked, true
100 bool Node::set_backtrack(thread_id_t id)
102 int i = id_to_int(id);
110 thread_id_t Node::get_next_backtrack()
112 /* TODO: find next backtrack */
114 for (i = 0; i < backtrack.size(); i++)
115 if (backtrack[i] == true)
117 /* Backtrack set was empty? */
118 ASSERT(i != backtrack.size());
120 backtrack[i] = false;
125 bool Node::is_enabled(Thread *t)
127 return id_to_int(t->get_id()) < num_threads;
131 * Add an action to the may_read_from set.
132 * @param act is the action to add
134 void Node::add_read_from(const ModelAction *act)
136 may_read_from.push_back(act);
140 * Gets the next 'may_read_from' action from this Node. Only valid for a node
141 * where this->action is a 'read'.
142 * @todo Perform reads_from backtracking/replay properly, so that this function
143 * may remove elements from may_read_from
144 * @return The first element in may_read_from
146 const ModelAction * Node::get_read_from() {
147 ASSERT(read_from_index<may_read_from.size());
148 return may_read_from[read_from_index];
151 bool Node::increment_read_from() {
153 return (read_from_index<may_read_from.size());
156 void Node::explore(thread_id_t tid)
158 int i = id_to_int(tid);
160 backtrack[i] = false;
163 explored_children[i] = true;
166 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
167 node_list_t::iterator end)
169 node_list_t::iterator it;
171 for (it = start; it != end; it++)
173 list->erase(start, end);
176 NodeStack::NodeStack()
179 node_list.push_back(new Node());
181 iter = node_list.begin();
184 NodeStack::~NodeStack()
186 clear_node_list(&node_list, node_list.begin(), node_list.end());
189 void NodeStack::print()
191 node_list_t::iterator it;
192 printf("............................................\n");
193 printf("NodeStack printing node_list:\n");
194 for (it = node_list.begin(); it != node_list.end(); it++) {
195 if (it == this->iter)
196 printf("vvv following action is the current iterator vvv\n");
199 printf("............................................\n");
202 ModelAction * NodeStack::explore_action(ModelAction *act)
206 ASSERT(!node_list.empty());
207 node_list_t::iterator it=iter;
210 if (it != node_list.end()) {
212 return (*iter)->get_action();
216 get_head()->explore_child(act);
217 node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
224 * Empties the stack of all trailing nodes after a given position and calls the
225 * destructor for each. This function is provided an offset which determines
226 * how many nodes (relative to the current replay state) to save before popping
228 * @param numAhead The number of nodes to skip forward before popping the stack.
230 void NodeStack::pop_restofstack(int numAhead)
232 /* Diverging from previous execution; clear out remainder of list */
233 node_list_t::iterator it = iter;
236 clear_node_list(&node_list, it, node_list.end());
239 Node * NodeStack::get_head()
241 if (node_list.empty())
246 Node * NodeStack::get_next()
248 node_list_t::iterator it = iter;
249 if (node_list.empty()) {
254 if (it == node_list.end()) {
261 void NodeStack::reset_execution()
263 iter = node_list.begin();