9 * @brief Node constructor
11 * Constructs a single Node for use in a NodeStack. Each Node is associated
12 * with exactly one ModelAction (exception: the first Node should be created
13 * as an empty stub, to represent the first thread "choice") and up to one
16 * @param act The ModelAction to associate with this Node. May be NULL.
17 * @param par The parent Node in the NodeStack. May be NULL if there is no
19 * @param nthreads The number of threads which exist at this point in the
22 Node::Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness)
25 num_threads(nthreads),
26 explored_children(num_threads),
27 backtrack(num_threads),
28 fairness(num_threads),
38 int currtid=id_to_int(act->get_tid());
39 int prevtid=(prevfairness != NULL)?id_to_int(prevfairness->action->get_tid()):0;
41 if ( model->params.fairwindow != 0 ) {
42 for(int i=0;i<nthreads;i++) {
43 ASSERT(i<((int)fairness.size()));
44 struct fairness_info * fi=& fairness[i];
45 struct fairness_info * prevfi=(par!=NULL)&&(i<par->get_num_threads())?&par->fairness[i]:NULL;
49 if (parent->enabled_array[i]) {
56 //Do window processing
57 if (prevfairness != NULL) {
58 if (prevfairness -> parent->enabled_array[i])
63 //Need full window to start evaluating conditions
64 //If we meet the enabled count and have no turns, give us priority
65 if ((fi->enabled_count >= model->params.enabledcount) &&
74 /** @brief Node desctructor */
80 MYFREE(enabled_array);
83 /** Prints debugging info for the ModelAction associated with this Node */
89 printf("******** empty action ********\n");
92 /** @brief Prints info about may_read_from set */
93 void Node::print_may_read_from()
95 for (unsigned int i = 0; i < may_read_from.size(); i++)
96 may_read_from[i]->print();
100 * Sets a promise to explore meeting with the given node.
101 * @param i is the promise index.
103 void Node::set_promise(unsigned int i) {
104 if (i >= promises.size())
105 promises.resize(i + 1, PROMISE_IGNORE);
106 if (promises[i] == PROMISE_IGNORE)
107 promises[i] = PROMISE_UNFULFILLED;
111 * Looks up whether a given promise should be satisfied by this node.
112 * @param i The promise index.
113 * @return true if the promise should be satisfied by the given model action.
115 bool Node::get_promise(unsigned int i) {
116 return (i < promises.size()) && (promises[i] == PROMISE_FULFILLED);
120 * Increments to the next combination of promises.
121 * @return true if we have a valid combination.
123 bool Node::increment_promise() {
124 for (unsigned int i = 0; i < promises.size(); i++) {
125 if (promises[i] == PROMISE_UNFULFILLED) {
126 promises[i] = PROMISE_FULFILLED;
129 if (promises[i] == PROMISE_FULFILLED)
130 promises[i] = PROMISE_UNFULFILLED;
139 * Returns whether the promise set is empty.
140 * @return true if we have explored all promise combinations.
142 bool Node::promise_empty() {
143 for (unsigned int i = 0; i < promises.size();i++)
144 if (promises[i] == PROMISE_UNFULFILLED)
150 * Adds a value from a weakly ordered future write to backtrack to.
151 * @param value is the value to backtrack to.
153 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
154 int suitableindex=-1;
155 for (unsigned int i = 0; i < future_values.size(); i++) {
156 if (future_values[i].value == value) {
157 if (future_values[i].expiration>=expiration)
159 if (future_index < i) {
165 if (suitableindex!=-1) {
166 future_values[suitableindex].expiration=expiration;
169 struct future_value newfv={value, expiration};
170 future_values.push_back(newfv);
175 * Checks whether the future_values set for this node is empty.
176 * @return true if the future_values set is empty.
178 bool Node::future_value_empty() {
179 return ((future_index + 1) >= future_values.size());
183 * Checks if the Thread associated with this thread ID has been explored from
185 * @param tid is the thread ID to check
186 * @return true if this thread choice has been explored already, false
189 bool Node::has_been_explored(thread_id_t tid)
191 int id = id_to_int(tid);
192 return explored_children[id];
196 * Checks if the backtracking set is empty.
197 * @return true if the backtracking set is empty
199 bool Node::backtrack_empty()
201 return (numBacktracks == 0);
205 * Checks whether the readsfrom set for this node is empty.
206 * @return true if the readsfrom set is empty.
208 bool Node::read_from_empty() {
209 return ((read_from_index+1) >= may_read_from.size());
213 * Mark the appropriate backtracking information for exploring a thread choice.
214 * @param act The ModelAction to explore
216 void Node::explore_child(ModelAction *act, bool * is_enabled)
218 if ( ! enabled_array )
219 enabled_array=(bool *)MYMALLOC(sizeof(bool)*num_threads);
220 if (is_enabled != NULL)
221 memcpy(enabled_array, is_enabled, sizeof(bool)*num_threads);
223 for(int i=0;i<num_threads;i++)
224 enabled_array[i]=false;
227 explore(act->get_tid());
231 * Records a backtracking reference for a thread choice within this Node.
232 * Provides feedback as to whether this thread choice is already set for
234 * @return false if the thread was already set to be backtracked, true
237 bool Node::set_backtrack(thread_id_t id)
239 int i = id_to_int(id);
247 thread_id_t Node::get_next_backtrack()
249 /** @todo Find next backtrack */
251 for (i = 0; i < backtrack.size(); i++)
252 if (backtrack[i] == true)
254 /* Backtrack set was empty? */
255 ASSERT(i != backtrack.size());
257 backtrack[i] = false;
262 bool Node::is_enabled(Thread *t)
264 int thread_id=id_to_int(t->get_id());
265 return thread_id < num_threads && enabled_array[thread_id];
268 bool Node::is_enabled(thread_id_t tid)
270 int thread_id=id_to_int(tid);
271 return thread_id < num_threads && enabled_array[thread_id];
275 * Add an action to the may_read_from set.
276 * @param act is the action to add
278 void Node::add_read_from(const ModelAction *act)
280 may_read_from.push_back(act);
284 * Gets the next 'future_value' value from this Node. Only valid for a node
285 * where this->action is a 'read'.
286 * @return The first element in future_values
288 uint64_t Node::get_future_value() {
289 ASSERT(future_index<future_values.size());
290 return future_values[future_index].value;
293 modelclock_t Node::get_future_value_expiration() {
294 ASSERT(future_index<future_values.size());
295 return future_values[future_index].expiration;
299 int Node::get_read_from_size() {
300 return may_read_from.size();
303 const ModelAction * Node::get_read_from_at(int i) {
304 return may_read_from[i];
308 * Gets the next 'may_read_from' action from this Node. Only valid for a node
309 * where this->action is a 'read'.
310 * @return The first element in may_read_from
312 const ModelAction * Node::get_read_from() {
313 if (read_from_index < may_read_from.size())
314 return may_read_from[read_from_index];
320 * Increments the index into the readsfrom set to explore the next item.
321 * @return Returns false if we have explored all items.
323 bool Node::increment_read_from() {
325 return (read_from_index < may_read_from.size());
329 * Increments the index into the future_values set to explore the next item.
330 * @return Returns false if we have explored all values.
332 bool Node::increment_future_value() {
334 return (future_index < future_values.size());
337 void Node::explore(thread_id_t tid)
339 int i = id_to_int(tid);
341 backtrack[i] = false;
344 explored_children[i] = true;
347 NodeStack::NodeStack()
350 node_list.push_back(new Node());
355 NodeStack::~NodeStack()
359 void NodeStack::print()
361 printf("............................................\n");
362 printf("NodeStack printing node_list:\n");
363 for (unsigned int it = 0; it < node_list.size(); it++) {
364 if (it == this->iter)
365 printf("vvv following action is the current iterator vvv\n");
366 node_list[it]->print();
368 printf("............................................\n");
371 /** Note: The is_enabled set contains what actions were enabled when
374 ModelAction * NodeStack::explore_action(ModelAction *act, bool * is_enabled)
378 ASSERT(!node_list.empty());
380 if ((iter+1) < node_list.size()) {
382 return node_list[iter]->get_action();
386 get_head()->explore_child(act, is_enabled);
387 Node *prevfairness = NULL;
388 if ( model->params.fairwindow != 0 && iter > model->params.fairwindow ) {
389 prevfairness = node_list[iter-model->params.fairwindow];
391 node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
398 * Empties the stack of all trailing nodes after a given position and calls the
399 * destructor for each. This function is provided an offset which determines
400 * how many nodes (relative to the current replay state) to save before popping
402 * @param numAhead gives the number of Nodes (including this Node) to skip over
403 * before removing nodes.
405 void NodeStack::pop_restofstack(int numAhead)
407 /* Diverging from previous execution; clear out remainder of list */
408 unsigned int it=iter+numAhead;
409 node_list.resize(it);
412 Node * NodeStack::get_head()
414 if (node_list.empty())
416 return node_list[iter];
419 Node * NodeStack::get_next()
421 if (node_list.empty()) {
425 unsigned int it=iter+1;
426 if (it == node_list.size()) {
430 return node_list[it];
433 void NodeStack::reset_execution()