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),
33 /** @brief Node desctructor */
40 /** Prints debugging info for the ModelAction associated with this Node */
46 printf("******** empty action ********\n");
49 /** @brief Prints info about may_read_from set */
50 void Node::print_may_read_from()
52 readfrom_set_t::iterator it;
53 for (it = may_read_from.begin(); it != may_read_from.end(); it++)
58 * Checks if the Thread associated with this thread ID has been explored from
60 * @param tid is the thread ID to check
61 * @return true if this thread choice has been explored already, false
64 bool Node::has_been_explored(thread_id_t tid)
66 int id = id_to_int(tid);
67 return explored_children[id];
71 * Checks if the backtracking set is empty.
72 * @return true if the backtracking set is empty
74 bool Node::backtrack_empty()
76 return numBacktracks == 0;
80 * Mark the appropriate backtracking infromation for exploring a thread choice.
81 * @param act The ModelAction to explore
83 void Node::explore_child(ModelAction *act)
85 explore(act->get_tid());
89 * Records a backtracking reference for a thread choice within this Node.
90 * Provides feedback as to whether this thread choice is already set for
92 * @return false if the thread was already set to be backtracked, true
95 bool Node::set_backtrack(thread_id_t id)
97 int i = id_to_int(id);
105 thread_id_t Node::get_next_backtrack()
107 /* TODO: find next backtrack */
109 for (i = 0; i < backtrack.size(); i++)
110 if (backtrack[i] == true)
112 /* Backtrack set was empty? */
113 ASSERT(i != backtrack.size());
115 backtrack[i] = false;
120 bool Node::is_enabled(Thread *t)
122 return id_to_int(t->get_id()) < num_threads;
126 * Add an action to the may_read_from set.
127 * @param act is the action to add
129 void Node::add_read_from(const ModelAction *act)
131 may_read_from.push_back(act);
135 * Gets the next 'may_read_from' action from this Node. Only valid for a node
136 * where this->action is a 'read'.
137 * @todo Perform reads_from backtracking/replay properly, so that this function
138 * may remove elements from may_read_from
139 * @return The first element in may_read_from
141 const ModelAction * Node::get_next_read_from() {
142 const ModelAction *act;
143 ASSERT(!may_read_from.empty());
144 act = may_read_from.front();
145 /* TODO: perform reads_from replay properly */
146 /* may_read_from.pop_front(); */
150 void Node::explore(thread_id_t tid)
152 int i = id_to_int(tid);
154 backtrack[i] = false;
157 explored_children[i] = true;
160 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
161 node_list_t::iterator end)
163 node_list_t::iterator it;
165 for (it = start; it != end; it++)
167 list->erase(start, end);
170 NodeStack::NodeStack()
173 node_list.push_back(new Node());
175 iter = node_list.begin();
178 NodeStack::~NodeStack()
180 clear_node_list(&node_list, node_list.begin(), node_list.end());
183 void NodeStack::print()
185 node_list_t::iterator it;
186 printf("............................................\n");
187 printf("NodeStack printing node_list:\n");
188 for (it = node_list.begin(); it != node_list.end(); it++) {
189 if (it == this->iter)
190 printf("vvv following action is the current iterator vvv\n");
193 printf("............................................\n");
196 ModelAction * NodeStack::explore_action(ModelAction *act)
200 ASSERT(!node_list.empty());
202 if (get_head()->has_been_explored(act->get_tid())) {
204 return (*iter)->get_action();
207 /* Diverging from previous execution; clear out remainder of list */
208 node_list_t::iterator it = iter;
210 clear_node_list(&node_list, it, node_list.end());
213 get_head()->explore_child(act);
214 node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
220 Node * NodeStack::get_head()
222 if (node_list.empty())
227 Node * NodeStack::get_next()
229 node_list_t::iterator it = iter;
230 if (node_list.empty()) {
235 if (it == node_list.end()) {
242 void NodeStack::reset_execution()
244 iter = node_list.begin();