nodestack: add stub 'get_next_read_from()' function
[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         if (i >= backtrack.size())
113                 return THREAD_ID_T_NONE;
114         backtrack[i] = false;
115         numBacktracks--;
116         return int_to_id(i);
117 }
118
119 bool Node::is_enabled(Thread *t)
120 {
121         return id_to_int(t->get_id()) < num_threads;
122 }
123
124 /**
125  * Add an action to the may_read_from set.
126  * @param act is the action to add
127  */
128 void Node::add_read_from(const ModelAction *act)
129 {
130         may_read_from.push_back(act);
131 }
132
133 /**
134  * Gets the next 'may_read_from' action from this Node. Only valid for a node
135  * where this->action is a 'read'.
136  * @todo Perform reads_from backtracking/replay properly, so that this function
137  * may remove elements from may_read_from
138  * @return The first element in may_read_from
139  */
140 const ModelAction * Node::get_next_read_from() {
141         const ModelAction *act;
142         ASSERT(!may_read_from.empty());
143         act = may_read_from.front();
144         /* TODO: perform reads_from replay properly */
145         /* may_read_from.pop_front(); */
146         return act;
147 }
148
149 void Node::explore(thread_id_t tid)
150 {
151         int i = id_to_int(tid);
152         if (backtrack[i]) {
153                 backtrack[i] = false;
154                 numBacktracks--;
155         }
156         explored_children[i] = true;
157 }
158
159 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
160                                                node_list_t::iterator end)
161 {
162         node_list_t::iterator it;
163
164         for (it = start; it != end; it++)
165                 delete (*it);
166         list->erase(start, end);
167 }
168
169 NodeStack::NodeStack()
170         : total_nodes(0)
171 {
172         node_list.push_back(new Node());
173         total_nodes++;
174         iter = node_list.begin();
175 }
176
177 NodeStack::~NodeStack()
178 {
179         clear_node_list(&node_list, node_list.begin(), node_list.end());
180 }
181
182 void NodeStack::print()
183 {
184         node_list_t::iterator it;
185         printf("............................................\n");
186         printf("NodeStack printing node_list:\n");
187         for (it = node_list.begin(); it != node_list.end(); it++) {
188                 if (it == this->iter)
189                         printf("vvv following action is the current iterator vvv\n");
190                 (*it)->print();
191         }
192         printf("............................................\n");
193 }
194
195 ModelAction * NodeStack::explore_action(ModelAction *act)
196 {
197         DBG();
198
199         ASSERT(!node_list.empty());
200
201         if (get_head()->has_been_explored(act->get_tid())) {
202                 iter++;
203                 return (*iter)->get_action();
204         }
205
206         /* Diverging from previous execution; clear out remainder of list */
207         node_list_t::iterator it = iter;
208         it++;
209         clear_node_list(&node_list, it, node_list.end());
210
211         /* Record action */
212         get_head()->explore_child(act);
213         node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
214         total_nodes++;
215         iter++;
216         return NULL;
217 }
218
219 Node * NodeStack::get_head()
220 {
221         if (node_list.empty())
222                 return NULL;
223         return *iter;
224 }
225
226 Node * NodeStack::get_next()
227 {
228         node_list_t::iterator it = iter;
229         if (node_list.empty()) {
230                 DEBUG("Empty\n");
231                 return NULL;
232         }
233         it++;
234         if (it == node_list.end()) {
235                 DEBUG("At end\n");
236                 return NULL;
237         }
238         return *it;
239 }
240
241 void NodeStack::reset_execution()
242 {
243         iter = node_list.begin();
244 }