131452d8f8e220200b04c43be926232348afadbd
[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 {
29         if (act)
30                 act->set_node(this);
31 }
32
33 /** @brief Node desctructor */
34 Node::~Node()
35 {
36         if (action)
37                 delete action;
38 }
39
40 /** Prints debugging info for the ModelAction associated with this Node */
41 void Node::print()
42 {
43         if (action)
44                 action->print();
45         else
46                 printf("******** empty action ********\n");
47 }
48
49 /** @brief Prints info about may_read_from set */
50 void Node::print_may_read_from()
51 {
52         readfrom_set_t::iterator it;
53         for (it = may_read_from.begin(); it != may_read_from.end(); it++)
54                 (*it)->print();
55 }
56
57 /**
58  * Checks if the Thread associated with this thread ID has been explored from
59  * this Node already.
60  * @param tid is the thread ID to check
61  * @return true if this thread choice has been explored already, false
62  * otherwise
63  */
64 bool Node::has_been_explored(thread_id_t tid)
65 {
66         int id = id_to_int(tid);
67         return explored_children[id];
68 }
69
70 /**
71  * Checks if the backtracking set is empty.
72  * @return true if the backtracking set is empty
73  */
74 bool Node::backtrack_empty()
75 {
76         return numBacktracks == 0;
77 }
78
79 /**
80  * Mark the appropriate backtracking infromation for exploring a thread choice.
81  * @param act The ModelAction to explore
82  */
83 void Node::explore_child(ModelAction *act)
84 {
85         explore(act->get_tid());
86 }
87
88 /**
89  * Records a backtracking reference for a thread choice within this Node.
90  * Provides feedback as to whether this thread choice is already set for
91  * backtracking.
92  * @return false if the thread was already set to be backtracked, true
93  * otherwise
94  */
95 bool Node::set_backtrack(thread_id_t id)
96 {
97         int i = id_to_int(id);
98         if (backtrack[i])
99                 return false;
100         backtrack[i] = true;
101         numBacktracks++;
102         return true;
103 }
104
105 thread_id_t Node::get_next_backtrack()
106 {
107         /* TODO: find next backtrack */
108         unsigned int i;
109         for (i = 0; i < backtrack.size(); i++)
110                 if (backtrack[i] == true)
111                         break;
112         backtrack[i] = false;
113         numBacktracks--;
114         return int_to_id(i);
115 }
116
117 bool Node::is_enabled(Thread *t)
118 {
119         return id_to_int(t->get_id()) < num_threads;
120 }
121
122 /**
123  * Add an action to the may_read_from set.
124  * @param act is the action to add
125  */
126 void Node::add_read_from(const ModelAction *act)
127 {
128         may_read_from.push_back(act);
129 }
130
131 /**
132  * Gets the next 'may_read_from' action from this Node. Only valid for a node
133  * where this->action is a 'read'.
134  * @todo Perform reads_from backtracking/replay properly, so that this function
135  * may remove elements from may_read_from
136  * @return The first element in may_read_from
137  */
138 const ModelAction * Node::get_next_read_from() {
139         const ModelAction *act;
140         ASSERT(!may_read_from.empty());
141         act = may_read_from.front();
142         /* TODO: perform reads_from replay properly */
143         /* may_read_from.pop_front(); */
144         return act;
145 }
146
147 void Node::explore(thread_id_t tid)
148 {
149         int i = id_to_int(tid);
150         if (backtrack[i]) {
151                 backtrack[i] = false;
152                 numBacktracks--;
153         }
154         explored_children[i] = true;
155 }
156
157 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
158                                                node_list_t::iterator end)
159 {
160         node_list_t::iterator it;
161
162         for (it = start; it != end; it++)
163                 delete (*it);
164         list->erase(start, end);
165 }
166
167 NodeStack::NodeStack()
168         : total_nodes(0)
169 {
170         node_list.push_back(new Node());
171         total_nodes++;
172         iter = node_list.begin();
173 }
174
175 NodeStack::~NodeStack()
176 {
177         clear_node_list(&node_list, node_list.begin(), node_list.end());
178 }
179
180 void NodeStack::print()
181 {
182         node_list_t::iterator it;
183         printf("............................................\n");
184         printf("NodeStack printing node_list:\n");
185         for (it = node_list.begin(); it != node_list.end(); it++) {
186                 if (it == this->iter)
187                         printf("vvv following action is the current iterator vvv\n");
188                 (*it)->print();
189         }
190         printf("............................................\n");
191 }
192
193 ModelAction * NodeStack::explore_action(ModelAction *act)
194 {
195         DBG();
196
197         ASSERT(!node_list.empty());
198
199         if (get_head()->has_been_explored(act->get_tid())) {
200                 iter++;
201                 return (*iter)->get_action();
202         }
203
204         /* Diverging from previous execution; clear out remainder of list */
205         node_list_t::iterator it = iter;
206         it++;
207         clear_node_list(&node_list, it, node_list.end());
208
209         /* Record action */
210         get_head()->explore_child(act);
211         node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
212         total_nodes++;
213         iter++;
214         return NULL;
215 }
216
217 Node * NodeStack::get_head()
218 {
219         if (node_list.empty())
220                 return NULL;
221         return *iter;
222 }
223
224 Node * NodeStack::get_next()
225 {
226         node_list_t::iterator it = iter;
227         if (node_list.empty()) {
228                 DEBUG("Empty\n");
229                 return NULL;
230         }
231         it++;
232         if (it == node_list.end()) {
233                 DEBUG("At end\n");
234                 return NULL;
235         }
236         return *it;
237 }
238
239 void NodeStack::reset_execution()
240 {
241         iter = node_list.begin();
242 }