10 * @brief Node constructor
12 * Constructs a single Node for use in a NodeStack. Each Node is associated
13 * with exactly one ModelAction (exception: the first Node should be created
14 * as an empty stub, to represent the first thread "choice") and up to one
17 * @param act The ModelAction to associate with this Node. May be NULL.
18 * @param par The parent Node in the NodeStack. May be NULL if there is no
20 * @param nthreads The number of threads which exist at this point in the
23 Node::Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness)
26 num_threads(nthreads),
27 explored_children(num_threads),
28 backtrack(num_threads),
29 fairness(num_threads),
39 int currtid=id_to_int(act->get_tid());
40 int prevtid=(prevfairness != NULL)?id_to_int(prevfairness->action->get_tid()):0;
42 if ( model->params.fairwindow != 0 ) {
43 for(int i=0;i<nthreads;i++) {
44 ASSERT(i<((int)fairness.size()));
45 struct fairness_info * fi=& fairness[i];
46 struct fairness_info * prevfi=(par!=NULL)&&(i<par->get_num_threads())?&par->fairness[i]:NULL;
50 if (parent->enabled_array[i]) {
57 //Do window processing
58 if (prevfairness != NULL) {
59 if (prevfairness -> parent->enabled_array[i])
64 //Need full window to start evaluating conditions
65 //If we meet the enabled count and have no turns, give us priority
66 if ((fi->enabled_count >= model->params.enabledcount) &&
75 /** @brief Node desctructor */
81 model_free(enabled_array);
84 /** Prints debugging info for the ModelAction associated with this Node */
90 printf("******** empty action ********\n");
93 /** @brief Prints info about may_read_from set */
94 void Node::print_may_read_from()
96 for (unsigned int i = 0; i < may_read_from.size(); i++)
97 may_read_from[i]->print();
101 * Sets a promise to explore meeting with the given node.
102 * @param i is the promise index.
104 void Node::set_promise(unsigned int i) {
105 if (i >= promises.size())
106 promises.resize(i + 1, PROMISE_IGNORE);
107 if (promises[i] == PROMISE_IGNORE)
108 promises[i] = PROMISE_UNFULFILLED;
112 * Looks up whether a given promise should be satisfied by this node.
113 * @param i The promise index.
114 * @return true if the promise should be satisfied by the given model action.
116 bool Node::get_promise(unsigned int i) {
117 return (i < promises.size()) && (promises[i] == PROMISE_FULFILLED);
121 * Increments to the next combination of promises.
122 * @return true if we have a valid combination.
124 bool Node::increment_promise() {
127 for (unsigned int i = 0; i < promises.size(); i++) {
128 if (promises[i] == PROMISE_UNFULFILLED) {
129 promises[i] = PROMISE_FULFILLED;
132 if (promises[i] == PROMISE_FULFILLED)
133 promises[i] = PROMISE_UNFULFILLED;
142 * Returns whether the promise set is empty.
143 * @return true if we have explored all promise combinations.
145 bool Node::promise_empty() {
146 for (unsigned int i = 0; i < promises.size();i++)
147 if (promises[i] == PROMISE_UNFULFILLED)
153 * Adds a value from a weakly ordered future write to backtrack to.
154 * @param value is the value to backtrack to.
156 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
157 int suitableindex=-1;
158 for (unsigned int i = 0; i < future_values.size(); i++) {
159 if (future_values[i].value == value) {
160 if (future_values[i].expiration>=expiration)
162 if (future_index < ((int) i)) {
168 if (suitableindex!=-1) {
169 future_values[suitableindex].expiration=expiration;
172 struct future_value newfv={value, expiration};
173 future_values.push_back(newfv);
178 * Checks whether the future_values set for this node is empty.
179 * @return true if the future_values set is empty.
181 bool Node::future_value_empty() {
182 return ((future_index + 1) >= ((int)future_values.size()));
186 * Checks if the Thread associated with this thread ID has been explored from
188 * @param tid is the thread ID to check
189 * @return true if this thread choice has been explored already, false
192 bool Node::has_been_explored(thread_id_t tid)
194 int id = id_to_int(tid);
195 return explored_children[id];
199 * Checks if the backtracking set is empty.
200 * @return true if the backtracking set is empty
202 bool Node::backtrack_empty()
204 return (numBacktracks == 0);
208 * Checks whether the readsfrom set for this node is empty.
209 * @return true if the readsfrom set is empty.
211 bool Node::read_from_empty() {
212 return ((read_from_index+1) >= may_read_from.size());
216 * Mark the appropriate backtracking information for exploring a thread choice.
217 * @param act The ModelAction to explore
219 void Node::explore_child(ModelAction *act, bool * is_enabled)
221 if ( ! enabled_array )
222 enabled_array=(bool *)model_malloc(sizeof(bool)*num_threads);
223 if (is_enabled != NULL)
224 memcpy(enabled_array, is_enabled, sizeof(bool)*num_threads);
226 for(int i=0;i<num_threads;i++)
227 enabled_array[i]=false;
230 explore(act->get_tid());
234 * Records a backtracking reference for a thread choice within this Node.
235 * Provides feedback as to whether this thread choice is already set for
237 * @return false if the thread was already set to be backtracked, true
240 bool Node::set_backtrack(thread_id_t id)
242 int i = id_to_int(id);
250 thread_id_t Node::get_next_backtrack()
252 /** @todo Find next backtrack */
254 for (i = 0; i < backtrack.size(); i++)
255 if (backtrack[i] == true)
257 /* Backtrack set was empty? */
258 ASSERT(i != backtrack.size());
260 backtrack[i] = false;
265 bool Node::is_enabled(Thread *t)
267 int thread_id=id_to_int(t->get_id());
268 return thread_id < num_threads && enabled_array[thread_id];
271 bool Node::is_enabled(thread_id_t tid)
273 int thread_id=id_to_int(tid);
274 return thread_id < num_threads && enabled_array[thread_id];
277 bool Node::has_priority(thread_id_t tid)
279 return fairness[id_to_int(tid)].priority;
283 * Add an action to the may_read_from set.
284 * @param act is the action to add
286 void Node::add_read_from(const ModelAction *act)
288 may_read_from.push_back(act);
292 * Gets the next 'future_value' value from this Node. Only valid for a node
293 * where this->action is a 'read'.
294 * @return The first element in future_values
296 uint64_t Node::get_future_value() {
297 ASSERT(future_index<((int)future_values.size()));
298 return future_values[future_index].value;
301 modelclock_t Node::get_future_value_expiration() {
302 ASSERT(future_index<((int)future_values.size()));
303 return future_values[future_index].expiration;
307 int Node::get_read_from_size() {
308 return may_read_from.size();
311 const ModelAction * Node::get_read_from_at(int i) {
312 return may_read_from[i];
316 * Gets the next 'may_read_from' action from this Node. Only valid for a node
317 * where this->action is a 'read'.
318 * @return The first element in may_read_from
320 const ModelAction * Node::get_read_from() {
321 if (read_from_index < may_read_from.size())
322 return may_read_from[read_from_index];
328 * Increments the index into the readsfrom set to explore the next item.
329 * @return Returns false if we have explored all items.
331 bool Node::increment_read_from() {
334 if (read_from_index < may_read_from.size()) {
336 return read_from_index < may_read_from.size();
342 * Increments the index into the future_values set to explore the next item.
343 * @return Returns false if we have explored all values.
345 bool Node::increment_future_value() {
348 if (future_index < ((int)future_values.size())) {
350 return (future_index < ((int)future_values.size()));
355 void Node::explore(thread_id_t tid)
357 int i = id_to_int(tid);
359 backtrack[i] = false;
362 explored_children[i] = true;
365 NodeStack::NodeStack()
368 node_list.push_back(new Node());
373 NodeStack::~NodeStack()
377 void NodeStack::print()
379 printf("............................................\n");
380 printf("NodeStack printing node_list:\n");
381 for (unsigned int it = 0; it < node_list.size(); it++) {
382 if (it == this->iter)
383 printf("vvv following action is the current iterator vvv\n");
384 node_list[it]->print();
386 printf("............................................\n");
389 /** Note: The is_enabled set contains what actions were enabled when
392 ModelAction * NodeStack::explore_action(ModelAction *act, bool * is_enabled)
396 ASSERT(!node_list.empty());
398 if ((iter+1) < node_list.size()) {
400 return node_list[iter]->get_action();
404 get_head()->explore_child(act, is_enabled);
405 Node *prevfairness = NULL;
406 if ( model->params.fairwindow != 0 && iter > model->params.fairwindow ) {
407 prevfairness = node_list[iter-model->params.fairwindow];
409 node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
416 * Empties the stack of all trailing nodes after a given position and calls the
417 * destructor for each. This function is provided an offset which determines
418 * how many nodes (relative to the current replay state) to save before popping
420 * @param numAhead gives the number of Nodes (including this Node) to skip over
421 * before removing nodes.
423 void NodeStack::pop_restofstack(int numAhead)
425 /* Diverging from previous execution; clear out remainder of list */
426 unsigned int it=iter+numAhead;
427 for(unsigned int i=it;i<node_list.size();i++)
429 node_list.resize(it);
432 Node * NodeStack::get_head()
434 if (node_list.empty())
436 return node_list[iter];
439 Node * NodeStack::get_next()
441 if (node_list.empty()) {
445 unsigned int it=iter+1;
446 if (it == node_list.size()) {
450 return node_list[it];
453 void NodeStack::reset_execution()