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, bool *enabled)
23 num_threads(nthreads),
24 explored_children(num_threads),
25 backtrack(num_threads),
34 enabled_array=(bool *)MYMALLOC(sizeof(bool)*num_threads);
36 memcpy(enabled_array, enabled, sizeof(bool)*num_threads);
38 for(int i=0;i<num_threads;i++)
39 enabled_array[i]=false;
42 /** @brief Node desctructor */
47 MYFREE(enabled_array);
50 /** Prints debugging info for the ModelAction associated with this Node */
56 printf("******** empty action ********\n");
59 /** @brief Prints info about may_read_from set */
60 void Node::print_may_read_from()
62 for (unsigned int i = 0; i < may_read_from.size(); i++)
63 may_read_from[i]->print();
67 * Sets a promise to explore meeting with the given node.
68 * @param i is the promise index.
70 void Node::set_promise(unsigned int i) {
71 if (i >= promises.size())
72 promises.resize(i + 1, PROMISE_IGNORE);
73 if (promises[i] == PROMISE_IGNORE)
74 promises[i] = PROMISE_UNFULFILLED;
78 * Looks up whether a given promise should be satisfied by this node.
79 * @param i The promise index.
80 * @return true if the promise should be satisfied by the given model action.
82 bool Node::get_promise(unsigned int i) {
83 return (i < promises.size()) && (promises[i] == PROMISE_FULFILLED);
87 * Increments to the next combination of promises.
88 * @return true if we have a valid combination.
90 bool Node::increment_promise() {
91 for (unsigned int i = 0; i < promises.size(); i++) {
92 if (promises[i] == PROMISE_UNFULFILLED) {
93 promises[i] = PROMISE_FULFILLED;
96 if (promises[i] == PROMISE_FULFILLED)
97 promises[i] = PROMISE_UNFULFILLED;
106 * Returns whether the promise set is empty.
107 * @return true if we have explored all promise combinations.
109 bool Node::promise_empty() {
110 for (unsigned int i = 0; i < promises.size();i++)
111 if (promises[i] == PROMISE_UNFULFILLED)
117 * Adds a value from a weakly ordered future write to backtrack to.
118 * @param value is the value to backtrack to.
120 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
121 int suitableindex=-1;
122 for (unsigned int i = 0; i < future_values.size(); i++) {
123 if (future_values[i].value == value) {
124 if (future_values[i].expiration>=expiration)
126 if (future_index < i) {
132 if (suitableindex!=-1) {
133 future_values[suitableindex].expiration=expiration;
136 struct future_value newfv={value, expiration};
137 future_values.push_back(newfv);
142 * Checks whether the future_values set for this node is empty.
143 * @return true if the future_values set is empty.
145 bool Node::future_value_empty() {
146 return ((future_index + 1) >= future_values.size());
150 * Checks if the Thread associated with this thread ID has been explored from
152 * @param tid is the thread ID to check
153 * @return true if this thread choice has been explored already, false
156 bool Node::has_been_explored(thread_id_t tid)
158 int id = id_to_int(tid);
159 return explored_children[id];
163 * Checks if the backtracking set is empty.
164 * @return true if the backtracking set is empty
166 bool Node::backtrack_empty()
168 return (numBacktracks == 0);
172 * Checks whether the readsfrom set for this node is empty.
173 * @return true if the readsfrom set is empty.
175 bool Node::read_from_empty() {
176 return ((read_from_index+1) >= may_read_from.size());
180 * Mark the appropriate backtracking information for exploring a thread choice.
181 * @param act The ModelAction to explore
183 void Node::explore_child(ModelAction *act)
185 explore(act->get_tid());
189 * Records a backtracking reference for a thread choice within this Node.
190 * Provides feedback as to whether this thread choice is already set for
192 * @return false if the thread was already set to be backtracked, true
195 bool Node::set_backtrack(thread_id_t id)
197 int i = id_to_int(id);
205 thread_id_t Node::get_next_backtrack()
207 /** @todo Find next backtrack */
209 for (i = 0; i < backtrack.size(); i++)
210 if (backtrack[i] == true)
212 /* Backtrack set was empty? */
213 ASSERT(i != backtrack.size());
215 backtrack[i] = false;
220 bool Node::is_enabled(Thread *t)
222 int thread_id=id_to_int(t->get_id());
223 return thread_id < num_threads && enabled_array[thread_id];
227 * Add an action to the may_read_from set.
228 * @param act is the action to add
230 void Node::add_read_from(const ModelAction *act)
232 may_read_from.push_back(act);
236 * Gets the next 'future_value' value from this Node. Only valid for a node
237 * where this->action is a 'read'.
238 * @return The first element in future_values
240 uint64_t Node::get_future_value() {
241 ASSERT(future_index<future_values.size());
242 return future_values[future_index].value;
245 modelclock_t Node::get_future_value_expiration() {
246 ASSERT(future_index<future_values.size());
247 return future_values[future_index].expiration;
251 int Node::get_read_from_size() {
252 return may_read_from.size();
255 const ModelAction * Node::get_read_from_at(int i) {
256 return may_read_from[i];
260 * Gets the next 'may_read_from' action from this Node. Only valid for a node
261 * where this->action is a 'read'.
262 * @return The first element in may_read_from
264 const ModelAction * Node::get_read_from() {
265 if (read_from_index < may_read_from.size())
266 return may_read_from[read_from_index];
272 * Increments the index into the readsfrom set to explore the next item.
273 * @return Returns false if we have explored all items.
275 bool Node::increment_read_from() {
277 return (read_from_index < may_read_from.size());
281 * Increments the index into the future_values set to explore the next item.
282 * @return Returns false if we have explored all values.
284 bool Node::increment_future_value() {
286 return (future_index < future_values.size());
289 void Node::explore(thread_id_t tid)
291 int i = id_to_int(tid);
293 backtrack[i] = false;
296 explored_children[i] = true;
299 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
300 node_list_t::iterator end)
302 node_list_t::iterator it;
304 for (it = start; it != end; it++)
306 list->erase(start, end);
309 NodeStack::NodeStack()
312 node_list.push_back(new Node());
314 iter = node_list.begin();
317 NodeStack::~NodeStack()
319 clear_node_list(&node_list, node_list.begin(), node_list.end());
322 void NodeStack::print()
324 node_list_t::iterator it;
325 printf("............................................\n");
326 printf("NodeStack printing node_list:\n");
327 for (it = node_list.begin(); it != node_list.end(); it++) {
328 if (it == this->iter)
329 printf("vvv following action is the current iterator vvv\n");
332 printf("............................................\n");
335 ModelAction * NodeStack::explore_action(ModelAction *act, bool * is_enabled)
339 ASSERT(!node_list.empty());
340 node_list_t::iterator it=iter;
343 if (it != node_list.end()) {
345 return (*iter)->get_action();
349 get_head()->explore_child(act);
350 node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
357 * Empties the stack of all trailing nodes after a given position and calls the
358 * destructor for each. This function is provided an offset which determines
359 * how many nodes (relative to the current replay state) to save before popping
361 * @param numAhead gives the number of Nodes (including this Node) to skip over
362 * before removing nodes.
364 void NodeStack::pop_restofstack(int numAhead)
366 /* Diverging from previous execution; clear out remainder of list */
367 node_list_t::iterator it = iter;
370 clear_node_list(&node_list, it, node_list.end());
373 Node * NodeStack::get_head()
375 if (node_list.empty())
380 Node * NodeStack::get_next()
382 node_list_t::iterator it = iter;
383 if (node_list.empty()) {
388 if (it == node_list.end()) {
395 void NodeStack::reset_execution()
397 iter = node_list.begin();