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, bool *enabled)
25 num_threads(nthreads),
26 explored_children(num_threads),
27 backtrack(num_threads),
36 enabled_array=(bool *)MYMALLOC(sizeof(bool)*num_threads);
38 memcpy(enabled_array, enabled, sizeof(bool)*num_threads);
40 for(int i=0;i<num_threads;i++)
41 enabled_array[i]=false;
45 /** @brief Node desctructor */
50 MYFREE(enabled_array);
53 /** Prints debugging info for the ModelAction associated with this Node */
59 printf("******** empty action ********\n");
62 /** @brief Prints info about may_read_from set */
63 void Node::print_may_read_from()
65 for (unsigned int i = 0; i < may_read_from.size(); i++)
66 may_read_from[i]->print();
70 * Sets a promise to explore meeting with the given node.
71 * @param i is the promise index.
73 void Node::set_promise(unsigned int i) {
74 if (i >= promises.size())
75 promises.resize(i + 1, PROMISE_IGNORE);
76 if (promises[i] == PROMISE_IGNORE)
77 promises[i] = PROMISE_UNFULFILLED;
81 * Looks up whether a given promise should be satisfied by this node.
82 * @param i The promise index.
83 * @return true if the promise should be satisfied by the given model action.
85 bool Node::get_promise(unsigned int i) {
86 return (i < promises.size()) && (promises[i] == PROMISE_FULFILLED);
90 * Increments to the next combination of promises.
91 * @return true if we have a valid combination.
93 bool Node::increment_promise() {
94 for (unsigned int i = 0; i < promises.size(); i++) {
95 if (promises[i] == PROMISE_UNFULFILLED) {
96 promises[i] = PROMISE_FULFILLED;
99 if (promises[i] == PROMISE_FULFILLED)
100 promises[i] = PROMISE_UNFULFILLED;
109 * Returns whether the promise set is empty.
110 * @return true if we have explored all promise combinations.
112 bool Node::promise_empty() {
113 for (unsigned int i = 0; i < promises.size();i++)
114 if (promises[i] == PROMISE_UNFULFILLED)
120 * Adds a value from a weakly ordered future write to backtrack to.
121 * @param value is the value to backtrack to.
123 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
124 int suitableindex=-1;
125 for (unsigned int i = 0; i < future_values.size(); i++) {
126 if (future_values[i].value == value) {
127 if (future_values[i].expiration>=expiration)
129 if (future_index < i) {
135 if (suitableindex!=-1) {
136 future_values[suitableindex].expiration=expiration;
139 struct future_value newfv={value, expiration};
140 future_values.push_back(newfv);
145 * Checks whether the future_values set for this node is empty.
146 * @return true if the future_values set is empty.
148 bool Node::future_value_empty() {
149 return ((future_index + 1) >= future_values.size());
153 * Checks if the Thread associated with this thread ID has been explored from
155 * @param tid is the thread ID to check
156 * @return true if this thread choice has been explored already, false
159 bool Node::has_been_explored(thread_id_t tid)
161 int id = id_to_int(tid);
162 return explored_children[id];
166 * Checks if the backtracking set is empty.
167 * @return true if the backtracking set is empty
169 bool Node::backtrack_empty()
171 return (numBacktracks == 0);
175 * Checks whether the readsfrom set for this node is empty.
176 * @return true if the readsfrom set is empty.
178 bool Node::read_from_empty() {
179 return ((read_from_index+1) >= may_read_from.size());
183 * Mark the appropriate backtracking information for exploring a thread choice.
184 * @param act The ModelAction to explore
186 void Node::explore_child(ModelAction *act)
188 explore(act->get_tid());
192 * Records a backtracking reference for a thread choice within this Node.
193 * Provides feedback as to whether this thread choice is already set for
195 * @return false if the thread was already set to be backtracked, true
198 bool Node::set_backtrack(thread_id_t id)
200 int i = id_to_int(id);
208 thread_id_t Node::get_next_backtrack()
210 /** @todo Find next backtrack */
212 for (i = 0; i < backtrack.size(); i++)
213 if (backtrack[i] == true)
215 /* Backtrack set was empty? */
216 ASSERT(i != backtrack.size());
218 backtrack[i] = false;
223 bool Node::is_enabled(Thread *t)
225 int thread_id=id_to_int(t->get_id());
226 return thread_id < num_threads && enabled_array[thread_id];
229 bool Node::is_enabled(thread_id_t tid)
231 int thread_id=id_to_int(tid);
232 return thread_id < num_threads && enabled_array[thread_id];
236 * Add an action to the may_read_from set.
237 * @param act is the action to add
239 void Node::add_read_from(const ModelAction *act)
241 may_read_from.push_back(act);
245 * Gets the next 'future_value' value from this Node. Only valid for a node
246 * where this->action is a 'read'.
247 * @return The first element in future_values
249 uint64_t Node::get_future_value() {
250 ASSERT(future_index<future_values.size());
251 return future_values[future_index].value;
254 modelclock_t Node::get_future_value_expiration() {
255 ASSERT(future_index<future_values.size());
256 return future_values[future_index].expiration;
260 int Node::get_read_from_size() {
261 return may_read_from.size();
264 const ModelAction * Node::get_read_from_at(int i) {
265 return may_read_from[i];
269 * Gets the next 'may_read_from' action from this Node. Only valid for a node
270 * where this->action is a 'read'.
271 * @return The first element in may_read_from
273 const ModelAction * Node::get_read_from() {
274 if (read_from_index < may_read_from.size())
275 return may_read_from[read_from_index];
281 * Increments the index into the readsfrom set to explore the next item.
282 * @return Returns false if we have explored all items.
284 bool Node::increment_read_from() {
286 return (read_from_index < may_read_from.size());
290 * Increments the index into the future_values set to explore the next item.
291 * @return Returns false if we have explored all values.
293 bool Node::increment_future_value() {
295 return (future_index < future_values.size());
298 void Node::explore(thread_id_t tid)
300 int i = id_to_int(tid);
302 backtrack[i] = false;
305 explored_children[i] = true;
308 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
309 node_list_t::iterator end)
311 node_list_t::iterator it;
313 for (it = start; it != end; it++)
315 list->erase(start, end);
318 NodeStack::NodeStack()
321 node_list.push_back(new Node());
323 iter = node_list.begin();
326 NodeStack::~NodeStack()
328 clear_node_list(&node_list, node_list.begin(), node_list.end());
331 void NodeStack::print()
333 node_list_t::iterator it;
334 printf("............................................\n");
335 printf("NodeStack printing node_list:\n");
336 for (it = node_list.begin(); it != node_list.end(); it++) {
337 if (it == this->iter)
338 printf("vvv following action is the current iterator vvv\n");
341 printf("............................................\n");
344 ModelAction * NodeStack::explore_action(ModelAction *act, bool * is_enabled)
348 ASSERT(!node_list.empty());
349 node_list_t::iterator it=iter;
352 if (it != node_list.end()) {
354 return (*iter)->get_action();
358 get_head()->explore_child(act);
359 node_list.push_back(new Node(act, get_head(), model->get_num_threads(), is_enabled));
366 * Empties the stack of all trailing nodes after a given position and calls the
367 * destructor for each. This function is provided an offset which determines
368 * how many nodes (relative to the current replay state) to save before popping
370 * @param numAhead gives the number of Nodes (including this Node) to skip over
371 * before removing nodes.
373 void NodeStack::pop_restofstack(int numAhead)
375 /* Diverging from previous execution; clear out remainder of list */
376 node_list_t::iterator it = iter;
379 clear_node_list(&node_list, it, node_list.end());
382 Node * NodeStack::get_head()
384 if (node_list.empty())
389 Node * NodeStack::get_next()
391 node_list_t::iterator it = iter;
392 if (node_list.empty()) {
397 if (it == node_list.end()) {
404 void NodeStack::reset_execution()
406 iter = node_list.begin();