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;
43 /** @brief Node desctructor */
48 MYFREE(enabled_array);
51 /** Prints debugging info for the ModelAction associated with this Node */
57 printf("******** empty action ********\n");
60 /** @brief Prints info about may_read_from set */
61 void Node::print_may_read_from()
63 for (unsigned int i = 0; i < may_read_from.size(); i++)
64 may_read_from[i]->print();
68 * Sets a promise to explore meeting with the given node.
69 * @param i is the promise index.
71 void Node::set_promise(unsigned int i) {
72 if (i >= promises.size())
73 promises.resize(i + 1, PROMISE_IGNORE);
74 if (promises[i] == PROMISE_IGNORE)
75 promises[i] = PROMISE_UNFULFILLED;
79 * Looks up whether a given promise should be satisfied by this node.
80 * @param i The promise index.
81 * @return true if the promise should be satisfied by the given model action.
83 bool Node::get_promise(unsigned int i) {
84 return (i < promises.size()) && (promises[i] == PROMISE_FULFILLED);
88 * Increments to the next combination of promises.
89 * @return true if we have a valid combination.
91 bool Node::increment_promise() {
92 for (unsigned int i = 0; i < promises.size(); i++) {
93 if (promises[i] == PROMISE_UNFULFILLED) {
94 promises[i] = PROMISE_FULFILLED;
97 if (promises[i] == PROMISE_FULFILLED)
98 promises[i] = PROMISE_UNFULFILLED;
107 * Returns whether the promise set is empty.
108 * @return true if we have explored all promise combinations.
110 bool Node::promise_empty() {
111 for (unsigned int i = 0; i < promises.size();i++)
112 if (promises[i] == PROMISE_UNFULFILLED)
118 * Adds a value from a weakly ordered future write to backtrack to.
119 * @param value is the value to backtrack to.
121 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
122 int suitableindex=-1;
123 for (unsigned int i = 0; i < future_values.size(); i++) {
124 if (future_values[i].value == value) {
125 if (future_values[i].expiration>=expiration)
127 if (future_index < i) {
133 if (suitableindex!=-1) {
134 future_values[suitableindex].expiration=expiration;
137 struct future_value newfv={value, expiration};
138 future_values.push_back(newfv);
143 * Checks whether the future_values set for this node is empty.
144 * @return true if the future_values set is empty.
146 bool Node::future_value_empty() {
147 return ((future_index + 1) >= future_values.size());
151 * Checks if the Thread associated with this thread ID has been explored from
153 * @param tid is the thread ID to check
154 * @return true if this thread choice has been explored already, false
157 bool Node::has_been_explored(thread_id_t tid)
159 int id = id_to_int(tid);
160 return explored_children[id];
164 * Checks if the backtracking set is empty.
165 * @return true if the backtracking set is empty
167 bool Node::backtrack_empty()
169 return (numBacktracks == 0);
173 * Checks whether the readsfrom set for this node is empty.
174 * @return true if the readsfrom set is empty.
176 bool Node::read_from_empty() {
177 return ((read_from_index+1) >= may_read_from.size());
181 * Mark the appropriate backtracking information for exploring a thread choice.
182 * @param act The ModelAction to explore
184 void Node::explore_child(ModelAction *act)
186 explore(act->get_tid());
190 * Records a backtracking reference for a thread choice within this Node.
191 * Provides feedback as to whether this thread choice is already set for
193 * @return false if the thread was already set to be backtracked, true
196 bool Node::set_backtrack(thread_id_t id)
198 int i = id_to_int(id);
206 thread_id_t Node::get_next_backtrack()
208 /** @todo Find next backtrack */
210 for (i = 0; i < backtrack.size(); i++)
211 if (backtrack[i] == true)
213 /* Backtrack set was empty? */
214 ASSERT(i != backtrack.size());
216 backtrack[i] = false;
221 bool Node::is_enabled(Thread *t)
223 int thread_id=id_to_int(t->get_id());
224 return thread_id < num_threads && enabled_array[thread_id];
228 * Add an action to the may_read_from set.
229 * @param act is the action to add
231 void Node::add_read_from(const ModelAction *act)
233 may_read_from.push_back(act);
237 * Gets the next 'future_value' value from this Node. Only valid for a node
238 * where this->action is a 'read'.
239 * @return The first element in future_values
241 uint64_t Node::get_future_value() {
242 ASSERT(future_index<future_values.size());
243 return future_values[future_index].value;
246 modelclock_t Node::get_future_value_expiration() {
247 ASSERT(future_index<future_values.size());
248 return future_values[future_index].expiration;
252 int Node::get_read_from_size() {
253 return may_read_from.size();
256 const ModelAction * Node::get_read_from_at(int i) {
257 return may_read_from[i];
261 * Gets the next 'may_read_from' action from this Node. Only valid for a node
262 * where this->action is a 'read'.
263 * @return The first element in may_read_from
265 const ModelAction * Node::get_read_from() {
266 if (read_from_index < may_read_from.size())
267 return may_read_from[read_from_index];
273 * Increments the index into the readsfrom set to explore the next item.
274 * @return Returns false if we have explored all items.
276 bool Node::increment_read_from() {
278 return (read_from_index < may_read_from.size());
282 * Increments the index into the future_values set to explore the next item.
283 * @return Returns false if we have explored all values.
285 bool Node::increment_future_value() {
287 return (future_index < future_values.size());
290 void Node::explore(thread_id_t tid)
292 int i = id_to_int(tid);
294 backtrack[i] = false;
297 explored_children[i] = true;
300 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
301 node_list_t::iterator end)
303 node_list_t::iterator it;
305 for (it = start; it != end; it++)
307 list->erase(start, end);
310 NodeStack::NodeStack()
313 node_list.push_back(new Node());
315 iter = node_list.begin();
318 NodeStack::~NodeStack()
320 clear_node_list(&node_list, node_list.begin(), node_list.end());
323 void NodeStack::print()
325 node_list_t::iterator it;
326 printf("............................................\n");
327 printf("NodeStack printing node_list:\n");
328 for (it = node_list.begin(); it != node_list.end(); it++) {
329 if (it == this->iter)
330 printf("vvv following action is the current iterator vvv\n");
333 printf("............................................\n");
336 ModelAction * NodeStack::explore_action(ModelAction *act, bool * is_enabled)
340 ASSERT(!node_list.empty());
341 node_list_t::iterator it=iter;
344 if (it != node_list.end()) {
346 return (*iter)->get_action();
350 get_head()->explore_child(act);
351 node_list.push_back(new Node(act, get_head(), model->get_num_threads(), is_enabled));
358 * Empties the stack of all trailing nodes after a given position and calls the
359 * destructor for each. This function is provided an offset which determines
360 * how many nodes (relative to the current replay state) to save before popping
362 * @param numAhead gives the number of Nodes (including this Node) to skip over
363 * before removing nodes.
365 void NodeStack::pop_restofstack(int numAhead)
367 /* Diverging from previous execution; clear out remainder of list */
368 node_list_t::iterator it = iter;
371 clear_node_list(&node_list, it, node_list.end());
374 Node * NodeStack::get_head()
376 if (node_list.empty())
381 Node * NodeStack::get_next()
383 node_list_t::iterator it = iter;
384 if (node_list.empty()) {
389 if (it == node_list.end()) {
396 void NodeStack::reset_execution()
398 iter = node_list.begin();