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 void Node::set_promise(uint32_t i) {
61 if (i>=promises.size())
62 promises.resize(i+1,0);
66 bool Node::get_promise(uint32_t i) {
67 return (promises[i]==2);
70 bool Node::increment_promises() {
71 for (unsigned int i=0;i<promises.size();i++) {
85 bool Node::promises_empty() {
86 for (unsigned int i=0;i<promises.size();i++)
93 * Adds a value from a weakly ordered future write to backtrack to.
94 * @param value is the value to backtrack to.
97 bool Node::add_future_value(uint64_t value) {
98 for(unsigned int i=0;i<future_values.size();i++)
99 if (future_values[i]==value)
101 future_values.push_back(value);
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());
116 * Checks if the Thread associated with this thread ID has been explored from
118 * @param tid is the thread ID to check
119 * @return true if this thread choice has been explored already, false
122 bool Node::has_been_explored(thread_id_t tid)
124 int id = id_to_int(tid);
125 return explored_children[id];
129 * Checks if the backtracking set is empty.
130 * @return true if the backtracking set is empty
132 bool Node::backtrack_empty()
134 return (numBacktracks == 0);
139 * Checks whether the readsfrom set for this node is empty.
140 * @return true if the readsfrom set is empty.
142 bool Node::readsfrom_empty() {
143 return ((read_from_index+1)>=may_read_from.size());
149 * Mark the appropriate backtracking information for exploring a thread choice.
150 * @param act The ModelAction to explore
152 void Node::explore_child(ModelAction *act)
154 explore(act->get_tid());
158 * Records a backtracking reference for a thread choice within this Node.
159 * Provides feedback as to whether this thread choice is already set for
161 * @return false if the thread was already set to be backtracked, true
164 bool Node::set_backtrack(thread_id_t id)
166 int i = id_to_int(id);
174 thread_id_t Node::get_next_backtrack()
176 /** @todo Find next backtrack */
178 for (i = 0; i < backtrack.size(); i++)
179 if (backtrack[i] == true)
181 /* Backtrack set was empty? */
182 ASSERT(i != backtrack.size());
184 backtrack[i] = false;
189 bool Node::is_enabled(Thread *t)
191 return id_to_int(t->get_id()) < num_threads;
195 * Add an action to the may_read_from set.
196 * @param act is the action to add
198 void Node::add_read_from(const ModelAction *act)
200 may_read_from.push_back(act);
204 * Gets the next 'future_value' value from this Node. Only valid for a node
205 * where this->action is a 'read'.
206 * @return The first element in future_values
209 uint64_t Node::get_future_value() {
210 ASSERT(future_index<future_values.size());
211 return future_values[future_index];
215 * Gets the next 'may_read_from' action from this Node. Only valid for a node
216 * where this->action is a 'read'.
217 * @todo Perform reads_from backtracking/replay properly, so that this function
218 * may remove elements from may_read_from
219 * @return The first element in may_read_from
221 const ModelAction * Node::get_read_from() {
222 if (read_from_index<may_read_from.size())
223 return may_read_from[read_from_index];
229 * Increments the index into the readsfrom set to explore the next item.
230 * @return Returns false if we have explored all items.
232 bool Node::increment_read_from() {
234 return (read_from_index<may_read_from.size());
238 * Increments the index into the future_values set to explore the next item.
239 * @return Returns false if we have explored all values.
242 bool Node::increment_future_values() {
244 return (future_index<future_values.size());
247 void Node::explore(thread_id_t tid)
249 int i = id_to_int(tid);
251 backtrack[i] = false;
254 explored_children[i] = true;
257 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
258 node_list_t::iterator end)
260 node_list_t::iterator it;
262 for (it = start; it != end; it++)
264 list->erase(start, end);
267 NodeStack::NodeStack()
270 node_list.push_back(new Node());
272 iter = node_list.begin();
275 NodeStack::~NodeStack()
277 clear_node_list(&node_list, node_list.begin(), node_list.end());
280 void NodeStack::print()
282 node_list_t::iterator it;
283 printf("............................................\n");
284 printf("NodeStack printing node_list:\n");
285 for (it = node_list.begin(); it != node_list.end(); it++) {
286 if (it == this->iter)
287 printf("vvv following action is the current iterator vvv\n");
290 printf("............................................\n");
293 ModelAction * NodeStack::explore_action(ModelAction *act)
297 ASSERT(!node_list.empty());
298 node_list_t::iterator it=iter;
301 if (it != node_list.end()) {
303 return (*iter)->get_action();
307 get_head()->explore_child(act);
308 node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
315 * Empties the stack of all trailing nodes after a given position and calls the
316 * destructor for each. This function is provided an offset which determines
317 * how many nodes (relative to the current replay state) to save before popping
319 * @param numAhead gives the number of Nodes (including this Node) to skip over
320 * before removing nodes.
322 void NodeStack::pop_restofstack(int numAhead)
324 /* Diverging from previous execution; clear out remainder of list */
325 node_list_t::iterator it = iter;
328 clear_node_list(&node_list, it, node_list.end());
331 Node * NodeStack::get_head()
333 if (node_list.empty())
338 Node * NodeStack::get_next()
340 node_list_t::iterator it = iter;
341 if (node_list.empty()) {
346 if (it == node_list.end()) {
353 void NodeStack::reset_execution()
355 iter = node_list.begin();