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 model_free(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() {
126 for (unsigned int i = 0; i < promises.size(); i++) {
127 if (promises[i] == PROMISE_UNFULFILLED) {
128 promises[i] = PROMISE_FULFILLED;
131 if (promises[i] == PROMISE_FULFILLED)
132 promises[i] = PROMISE_UNFULFILLED;
141 * Returns whether the promise set is empty.
142 * @return true if we have explored all promise combinations.
144 bool Node::promise_empty() {
145 for (unsigned int i = 0; i < promises.size();i++)
146 if (promises[i] == PROMISE_UNFULFILLED)
152 * Adds a value from a weakly ordered future write to backtrack to.
153 * @param value is the value to backtrack to.
155 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
156 int suitableindex=-1;
157 for (unsigned int i = 0; i < future_values.size(); i++) {
158 if (future_values[i].value == value) {
159 if (future_values[i].expiration>=expiration)
161 if (future_index < ((int) i)) {
167 if (suitableindex!=-1) {
168 future_values[suitableindex].expiration=expiration;
171 struct future_value newfv={value, expiration};
172 future_values.push_back(newfv);
177 * Checks whether the future_values set for this node is empty.
178 * @return true if the future_values set is empty.
180 bool Node::future_value_empty() {
181 return ((future_index + 1) >= ((int)future_values.size()));
185 * Checks if the Thread associated with this thread ID has been explored from
187 * @param tid is the thread ID to check
188 * @return true if this thread choice has been explored already, false
191 bool Node::has_been_explored(thread_id_t tid)
193 int id = id_to_int(tid);
194 return explored_children[id];
198 * Checks if the backtracking set is empty.
199 * @return true if the backtracking set is empty
201 bool Node::backtrack_empty()
203 return (numBacktracks == 0);
207 * Checks whether the readsfrom set for this node is empty.
208 * @return true if the readsfrom set is empty.
210 bool Node::read_from_empty() {
211 return ((read_from_index+1) >= may_read_from.size());
215 * Mark the appropriate backtracking information for exploring a thread choice.
216 * @param act The ModelAction to explore
218 void Node::explore_child(ModelAction *act, bool * is_enabled)
220 if ( ! enabled_array )
221 enabled_array=(bool *)model_malloc(sizeof(bool)*num_threads);
222 if (is_enabled != NULL)
223 memcpy(enabled_array, is_enabled, sizeof(bool)*num_threads);
225 for(int i=0;i<num_threads;i++)
226 enabled_array[i]=false;
229 explore(act->get_tid());
233 * Records a backtracking reference for a thread choice within this Node.
234 * Provides feedback as to whether this thread choice is already set for
236 * @return false if the thread was already set to be backtracked, true
239 bool Node::set_backtrack(thread_id_t id)
241 int i = id_to_int(id);
249 thread_id_t Node::get_next_backtrack()
251 /** @todo Find next backtrack */
253 for (i = 0; i < backtrack.size(); i++)
254 if (backtrack[i] == true)
256 /* Backtrack set was empty? */
257 ASSERT(i != backtrack.size());
259 backtrack[i] = false;
264 bool Node::is_enabled(Thread *t)
266 int thread_id=id_to_int(t->get_id());
267 return thread_id < num_threads && enabled_array[thread_id];
270 bool Node::is_enabled(thread_id_t tid)
272 int thread_id=id_to_int(tid);
273 return thread_id < num_threads && enabled_array[thread_id];
277 * Add an action to the may_read_from set.
278 * @param act is the action to add
280 void Node::add_read_from(const ModelAction *act)
282 may_read_from.push_back(act);
286 * Gets the next 'future_value' value from this Node. Only valid for a node
287 * where this->action is a 'read'.
288 * @return The first element in future_values
290 uint64_t Node::get_future_value() {
291 ASSERT(future_index<((int)future_values.size()));
292 return future_values[future_index].value;
295 modelclock_t Node::get_future_value_expiration() {
296 ASSERT(future_index<((int)future_values.size()));
297 return future_values[future_index].expiration;
301 int Node::get_read_from_size() {
302 return may_read_from.size();
305 const ModelAction * Node::get_read_from_at(int i) {
306 return may_read_from[i];
310 * Gets the next 'may_read_from' action from this Node. Only valid for a node
311 * where this->action is a 'read'.
312 * @return The first element in may_read_from
314 const ModelAction * Node::get_read_from() {
315 if (read_from_index < may_read_from.size())
316 return may_read_from[read_from_index];
322 * Increments the index into the readsfrom set to explore the next item.
323 * @return Returns false if we have explored all items.
325 bool Node::increment_read_from() {
328 if (read_from_index < may_read_from.size()) {
330 return read_from_index < may_read_from.size();
336 * Increments the index into the future_values set to explore the next item.
337 * @return Returns false if we have explored all values.
339 bool Node::increment_future_value() {
342 if (future_index < ((int)future_values.size())) {
344 return (future_index < ((int)future_values.size()));
349 void Node::explore(thread_id_t tid)
351 int i = id_to_int(tid);
353 backtrack[i] = false;
356 explored_children[i] = true;
359 NodeStack::NodeStack()
362 node_list.push_back(new Node());
367 NodeStack::~NodeStack()
371 void NodeStack::print()
373 printf("............................................\n");
374 printf("NodeStack printing node_list:\n");
375 for (unsigned int it = 0; it < node_list.size(); it++) {
376 if (it == this->iter)
377 printf("vvv following action is the current iterator vvv\n");
378 node_list[it]->print();
380 printf("............................................\n");
383 /** Note: The is_enabled set contains what actions were enabled when
386 ModelAction * NodeStack::explore_action(ModelAction *act, bool * is_enabled)
390 ASSERT(!node_list.empty());
392 if ((iter+1) < node_list.size()) {
394 return node_list[iter]->get_action();
398 get_head()->explore_child(act, is_enabled);
399 Node *prevfairness = NULL;
400 if ( model->params.fairwindow != 0 && iter > model->params.fairwindow ) {
401 prevfairness = node_list[iter-model->params.fairwindow];
403 node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
410 * Empties the stack of all trailing nodes after a given position and calls the
411 * destructor for each. This function is provided an offset which determines
412 * how many nodes (relative to the current replay state) to save before popping
414 * @param numAhead gives the number of Nodes (including this Node) to skip over
415 * before removing nodes.
417 void NodeStack::pop_restofstack(int numAhead)
419 /* Diverging from previous execution; clear out remainder of list */
420 unsigned int it=iter+numAhead;
421 for(unsigned int i=it;i<node_list.size();i++)
423 node_list.resize(it);
426 Node * NodeStack::get_head()
428 if (node_list.empty())
430 return node_list[iter];
433 Node * NodeStack::get_next()
435 if (node_list.empty()) {
439 unsigned int it=iter+1;
440 if (it == node_list.size()) {
444 return node_list[it];
447 void NodeStack::reset_execution()