split these defs out of other stuff
[c11tester.git] / nodestack.cc
1 #include "nodestack.h"
2 #include "action.h"
3 #include "common.h"
4 #include "model.h"
5
6 /**
7  * @brief Node constructor
8  *
9  * Constructs a single Node for use in a NodeStack. Each Node is associated
10  * with exactly one ModelAction (exception: the first Node should be created
11  * as an empty stub, to represent the first thread "choice") and up to one
12  * parent.
13  *
14  * @param act The ModelAction to associate with this Node. May be NULL.
15  * @param par The parent Node in the NodeStack. May be NULL if there is no
16  * parent.
17  * @param nthreads The number of threads which exist at this point in the
18  * execution trace.
19  */
20 Node::Node(ModelAction *act, Node *par, int nthreads)
21         : action(act),
22         parent(par),
23         num_threads(nthreads),
24         explored_children(num_threads),
25         backtrack(num_threads),
26         numBacktracks(0),
27         may_read_from(),
28         read_from_index(0)
29 {
30         if (act)
31                 act->set_node(this);
32 }
33
34 /** @brief Node desctructor */
35 Node::~Node()
36 {
37         if (action)
38                 delete action;
39 }
40
41 /** Prints debugging info for the ModelAction associated with this Node */
42 void Node::print()
43 {
44         if (action)
45                 action->print();
46         else
47                 printf("******** empty action ********\n");
48 }
49
50 /** @brief Prints info about may_read_from set */
51 void Node::print_may_read_from()
52 {
53         readfrom_set_t::iterator it;
54         for (it = may_read_from.begin(); it != may_read_from.end(); it++)
55                 (*it)->print();
56 }
57
58 /**
59  * Checks if the Thread associated with this thread ID has been explored from
60  * this Node already.
61  * @param tid is the thread ID to check
62  * @return true if this thread choice has been explored already, false
63  * otherwise
64  */
65 bool Node::has_been_explored(thread_id_t tid)
66 {
67         int id = id_to_int(tid);
68         return explored_children[id];
69 }
70
71 /**
72  * Checks if the backtracking set is empty.
73  * @return true if the backtracking set is empty
74  */
75 bool Node::backtrack_empty()
76 {
77         return (numBacktracks == 0);
78 }
79
80 bool Node::readsfrom_empty() {
81         return ((read_from_index+1)>=may_read_from.size());
82 }
83
84 /**
85  * Mark the appropriate backtracking infromation for exploring a thread choice.
86  * @param act The ModelAction to explore
87  */
88 void Node::explore_child(ModelAction *act)
89 {
90         explore(act->get_tid());
91 }
92
93 /**
94  * Records a backtracking reference for a thread choice within this Node.
95  * Provides feedback as to whether this thread choice is already set for
96  * backtracking.
97  * @return false if the thread was already set to be backtracked, true
98  * otherwise
99  */
100 bool Node::set_backtrack(thread_id_t id)
101 {
102         int i = id_to_int(id);
103         if (backtrack[i])
104                 return false;
105         backtrack[i] = true;
106         numBacktracks++;
107         return true;
108 }
109
110 thread_id_t Node::get_next_backtrack()
111 {
112         /* TODO: find next backtrack */
113         unsigned int i;
114         for (i = 0; i < backtrack.size(); i++)
115                 if (backtrack[i] == true)
116                         break;
117         /* Backtrack set was empty? */
118         ASSERT(i != backtrack.size());
119
120         backtrack[i] = false;
121         numBacktracks--;
122         return int_to_id(i);
123 }
124
125 bool Node::is_enabled(Thread *t)
126 {
127         return id_to_int(t->get_id()) < num_threads;
128 }
129
130 /**
131  * Add an action to the may_read_from set.
132  * @param act is the action to add
133  */
134 void Node::add_read_from(const ModelAction *act)
135 {
136         may_read_from.push_back(act);
137 }
138
139 /**
140  * Gets the next 'may_read_from' action from this Node. Only valid for a node
141  * where this->action is a 'read'.
142  * @todo Perform reads_from backtracking/replay properly, so that this function
143  * may remove elements from may_read_from
144  * @return The first element in may_read_from
145  */
146 const ModelAction * Node::get_read_from() {
147         ASSERT(read_from_index<may_read_from.size());
148         return may_read_from[read_from_index];
149 }
150
151 bool Node::increment_read_from() {
152         read_from_index++;
153         return (read_from_index<may_read_from.size());
154 }
155
156 void Node::explore(thread_id_t tid)
157 {
158         int i = id_to_int(tid);
159         if (backtrack[i]) {
160                 backtrack[i] = false;
161                 numBacktracks--;
162         }
163         explored_children[i] = true;
164 }
165
166 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
167                                                node_list_t::iterator end)
168 {
169         node_list_t::iterator it;
170
171         for (it = start; it != end; it++)
172                 delete (*it);
173         list->erase(start, end);
174 }
175
176 NodeStack::NodeStack()
177         : total_nodes(0)
178 {
179         node_list.push_back(new Node());
180         total_nodes++;
181         iter = node_list.begin();
182 }
183
184 NodeStack::~NodeStack()
185 {
186         clear_node_list(&node_list, node_list.begin(), node_list.end());
187 }
188
189 void NodeStack::print()
190 {
191         node_list_t::iterator it;
192         printf("............................................\n");
193         printf("NodeStack printing node_list:\n");
194         for (it = node_list.begin(); it != node_list.end(); it++) {
195                 if (it == this->iter)
196                         printf("vvv following action is the current iterator vvv\n");
197                 (*it)->print();
198         }
199         printf("............................................\n");
200 }
201
202 ModelAction * NodeStack::explore_action(ModelAction *act)
203 {
204         DBG();
205
206         ASSERT(!node_list.empty());
207         node_list_t::iterator it=iter;
208         it++;
209
210         if (it != node_list.end()) {
211                 iter++;
212                 return (*iter)->get_action();
213         }
214
215         /* Record action */
216         get_head()->explore_child(act);
217         node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
218         total_nodes++;
219         iter++;
220         return NULL;
221 }
222
223 /**
224  * Empties the stack of all trailing nodes after a given position and calls the
225  * destructor for each. This function is provided an offset which determines
226  * how many nodes (relative to the current replay state) to save before popping
227  * the stack.
228  * @param numAhead The number of nodes to skip forward before popping the stack.
229  */
230 void NodeStack::pop_restofstack(int numAhead)
231 {
232         /* Diverging from previous execution; clear out remainder of list */
233         node_list_t::iterator it = iter;
234         while (numAhead--)
235                 it++;
236         clear_node_list(&node_list, it, node_list.end());
237 }
238
239 Node * NodeStack::get_head()
240 {
241         if (node_list.empty())
242                 return NULL;
243         return *iter;
244 }
245
246 Node * NodeStack::get_next()
247 {
248         node_list_t::iterator it = iter;
249         if (node_list.empty()) {
250                 DEBUG("Empty\n");
251                 return NULL;
252         }
253         it++;
254         if (it == node_list.end()) {
255                 DEBUG("At end\n");
256                 return NULL;
257         }
258         return *it;
259 }
260
261 void NodeStack::reset_execution()
262 {
263         iter = node_list.begin();
264 }