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),
36 relseq_break_writes(),
41 int currtid=id_to_int(act->get_tid());
42 int prevtid=(prevfairness != NULL)?id_to_int(prevfairness->action->get_tid()):0;
44 if ( model->params.fairwindow != 0 ) {
45 for(int i=0;i<nthreads;i++) {
46 ASSERT(i<((int)fairness.size()));
47 struct fairness_info * fi=& fairness[i];
48 struct fairness_info * prevfi=(par!=NULL)&&(i<par->get_num_threads())?&par->fairness[i]:NULL;
52 if (parent->enabled_array[i]==THREAD_ENABLED) {
59 //Do window processing
60 if (prevfairness != NULL) {
61 if (prevfairness -> parent->enabled_array[i] == THREAD_ENABLED)
66 //Need full window to start evaluating conditions
67 //If we meet the enabled count and have no turns, give us priority
68 if ((fi->enabled_count >= model->params.enabledcount) &&
77 /** @brief Node desctructor */
83 model_free(enabled_array);
86 /** Prints debugging info for the ModelAction associated with this Node */
92 printf("******** empty action ********\n");
95 /** @brief Prints info about may_read_from set */
96 void Node::print_may_read_from()
98 for (unsigned int i = 0; i < may_read_from.size(); i++)
99 may_read_from[i]->print();
103 * Sets a promise to explore meeting with the given node.
104 * @param i is the promise index.
106 void Node::set_promise(unsigned int i) {
107 if (i >= promises.size())
108 promises.resize(i + 1, PROMISE_IGNORE);
109 if (promises[i] == PROMISE_IGNORE)
110 promises[i] = PROMISE_UNFULFILLED;
114 * Looks up whether a given promise should be satisfied by this node.
115 * @param i The promise index.
116 * @return true if the promise should be satisfied by the given model action.
118 bool Node::get_promise(unsigned int i) {
119 return (i < promises.size()) && (promises[i] == PROMISE_FULFILLED);
123 * Increments to the next combination of promises.
124 * @return true if we have a valid combination.
126 bool Node::increment_promise() {
129 for (unsigned int i = 0; i < promises.size(); i++) {
130 if (promises[i] == PROMISE_UNFULFILLED) {
131 promises[i] = PROMISE_FULFILLED;
134 if (promises[i] == PROMISE_FULFILLED)
135 promises[i] = PROMISE_UNFULFILLED;
144 * Returns whether the promise set is empty.
145 * @return true if we have explored all promise combinations.
147 bool Node::promise_empty() {
148 for (unsigned int i = 0; i < promises.size();i++)
149 if (promises[i] == PROMISE_UNFULFILLED)
155 * Adds a value from a weakly ordered future write to backtrack to.
156 * @param value is the value to backtrack to.
158 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
159 int suitableindex=-1;
160 for (unsigned int i = 0; i < future_values.size(); i++) {
161 if (future_values[i].value == value) {
162 if (future_values[i].expiration>=expiration)
164 if (future_index < ((int) i)) {
170 if (suitableindex!=-1) {
171 future_values[suitableindex].expiration=expiration;
174 struct future_value newfv={value, expiration};
175 future_values.push_back(newfv);
180 * Checks whether the future_values set for this node is empty.
181 * @return true if the future_values set is empty.
183 bool Node::future_value_empty() {
184 return ((future_index + 1) >= ((int)future_values.size()));
188 * Checks if the Thread associated with this thread ID has been explored from
190 * @param tid is the thread ID to check
191 * @return true if this thread choice has been explored already, false
194 bool Node::has_been_explored(thread_id_t tid)
196 int id = id_to_int(tid);
197 return explored_children[id];
201 * Checks if the backtracking set is empty.
202 * @return true if the backtracking set is empty
204 bool Node::backtrack_empty()
206 return (numBacktracks == 0);
210 * Checks whether the readsfrom set for this node is empty.
211 * @return true if the readsfrom set is empty.
213 bool Node::read_from_empty() {
214 return ((read_from_index+1) >= may_read_from.size());
218 * Mark the appropriate backtracking information for exploring a thread choice.
219 * @param act The ModelAction to explore
221 void Node::explore_child(ModelAction *act, enabled_type_t * is_enabled)
223 if ( ! enabled_array )
224 enabled_array=(enabled_type_t *)model_malloc(sizeof(enabled_type_t)*num_threads);
225 if (is_enabled != NULL)
226 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t)*num_threads);
228 for(int i=0;i<num_threads;i++)
229 enabled_array[i]=THREAD_DISABLED;
232 explore(act->get_tid());
236 * Records a backtracking reference for a thread choice within this Node.
237 * Provides feedback as to whether this thread choice is already set for
239 * @return false if the thread was already set to be backtracked, true
242 bool Node::set_backtrack(thread_id_t id)
244 int i = id_to_int(id);
252 thread_id_t Node::get_next_backtrack()
254 /** @todo Find next backtrack */
256 for (i = 0; i < backtrack.size(); i++)
257 if (backtrack[i] == true)
259 /* Backtrack set was empty? */
260 ASSERT(i != backtrack.size());
262 backtrack[i] = false;
267 bool Node::is_enabled(Thread *t)
269 int thread_id=id_to_int(t->get_id());
270 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
273 bool Node::is_enabled(thread_id_t tid)
275 int thread_id=id_to_int(tid);
276 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
279 bool Node::has_priority(thread_id_t tid)
281 return fairness[id_to_int(tid)].priority;
285 * Add an action to the may_read_from set.
286 * @param act is the action to add
288 void Node::add_read_from(const ModelAction *act)
290 may_read_from.push_back(act);
294 * Gets the next 'future_value' value from this Node. Only valid for a node
295 * where this->action is a 'read'.
296 * @return The first element in future_values
298 uint64_t Node::get_future_value() {
299 ASSERT(future_index<((int)future_values.size()));
300 return future_values[future_index].value;
303 modelclock_t Node::get_future_value_expiration() {
304 ASSERT(future_index<((int)future_values.size()));
305 return future_values[future_index].expiration;
309 int Node::get_read_from_size() {
310 return may_read_from.size();
313 const ModelAction * Node::get_read_from_at(int i) {
314 return may_read_from[i];
318 * Gets the next 'may_read_from' action from this Node. Only valid for a node
319 * where this->action is a 'read'.
320 * @return The first element in may_read_from
322 const ModelAction * Node::get_read_from() {
323 if (read_from_index < may_read_from.size())
324 return may_read_from[read_from_index];
330 * Increments the index into the readsfrom set to explore the next item.
331 * @return Returns false if we have explored all items.
333 bool Node::increment_read_from() {
336 if (read_from_index < may_read_from.size()) {
338 return read_from_index < may_read_from.size();
344 * Increments the index into the future_values set to explore the next item.
345 * @return Returns false if we have explored all values.
347 bool Node::increment_future_value() {
350 if (future_index < ((int)future_values.size())) {
352 return (future_index < ((int)future_values.size()));
358 * Add a write ModelAction to the set of writes that may break the release
359 * sequence. This is used during replay exploration of pending release
360 * sequences. This Node must correspond to a release sequence fixup action.
362 * @param write The write that may break the release sequence. NULL means we
363 * allow the release sequence to synchronize.
365 void Node::add_relseq_break(const ModelAction *write)
367 relseq_break_writes.push_back(write);
371 * Get the write that may break the current pending release sequence,
372 * according to the replay / divergence pattern.
374 * @return A write that may break the release sequence. If NULL, that means
375 * the release sequence should not be broken.
377 const ModelAction * Node::get_relseq_break()
379 if (relseq_break_index < (int)relseq_break_writes.size())
380 return relseq_break_writes[relseq_break_index];
386 * Increments the index into the relseq_break_writes set to explore the next
388 * @return Returns false if we have explored all values.
390 bool Node::increment_relseq_break()
394 if (relseq_break_index < ((int)relseq_break_writes.size())) {
395 relseq_break_index++;
396 return (relseq_break_index < ((int)relseq_break_writes.size()));
402 * @return True if all writes that may break the release sequence have been
405 bool Node::relseq_break_empty() {
406 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
409 void Node::explore(thread_id_t tid)
411 int i = id_to_int(tid);
413 backtrack[i] = false;
416 explored_children[i] = true;
419 NodeStack::NodeStack() :
420 node_list(1, new Node()),
427 NodeStack::~NodeStack()
429 for (unsigned int i = 0; i < node_list.size(); i++)
433 void NodeStack::print()
435 printf("............................................\n");
436 printf("NodeStack printing node_list:\n");
437 for (unsigned int it = 0; it < node_list.size(); it++) {
438 if (it == this->iter)
439 printf("vvv following action is the current iterator vvv\n");
440 node_list[it]->print();
442 printf("............................................\n");
445 /** Note: The is_enabled set contains what actions were enabled when
448 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t * is_enabled)
452 ASSERT(!node_list.empty());
454 if ((iter+1) < node_list.size()) {
456 return node_list[iter]->get_action();
460 get_head()->explore_child(act, is_enabled);
461 Node *prevfairness = NULL;
462 if ( model->params.fairwindow != 0 && iter > model->params.fairwindow ) {
463 prevfairness = node_list[iter-model->params.fairwindow];
465 node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
472 * Empties the stack of all trailing nodes after a given position and calls the
473 * destructor for each. This function is provided an offset which determines
474 * how many nodes (relative to the current replay state) to save before popping
476 * @param numAhead gives the number of Nodes (including this Node) to skip over
477 * before removing nodes.
479 void NodeStack::pop_restofstack(int numAhead)
481 /* Diverging from previous execution; clear out remainder of list */
482 unsigned int it=iter+numAhead;
483 for(unsigned int i=it;i<node_list.size();i++)
485 node_list.resize(it);
488 Node * NodeStack::get_head()
490 if (node_list.empty())
492 return node_list[iter];
495 Node * NodeStack::get_next()
497 if (node_list.empty()) {
501 unsigned int it=iter+1;
502 if (it == node_list.size()) {
506 return node_list[it];
509 void NodeStack::reset_execution()