5a1bc12f1f3c68f0bda956fd2557c85a69e47fe9
[c11tester.git] / waitobj.cc
1 #include "waitobj.h"
2 #include "threads-model.h"
3 #include "funcnode.h"
4
5 WaitObj::WaitObj(thread_id_t tid) :
6         tid(tid),
7         waiting_for(32),
8         waited_by(32),
9         thrd_dist_maps(),
10         thrd_target_nodes(),
11         thrd_action_counters()
12 {}
13
14 WaitObj::~WaitObj()
15 {
16         for (uint i = 0; i < thrd_dist_maps.size(); i++)
17                 delete thrd_dist_maps[i];
18
19         for (uint i = 0; i < thrd_target_nodes.size(); i++)
20                 delete thrd_target_nodes[i];
21 }
22
23 void WaitObj::add_waiting_for(thread_id_t other, FuncNode * node, int dist)
24 {
25         waiting_for.add(other);
26
27         dist_map_t * dist_map = getDistMap(other);
28         dist_map->put(node, dist);
29
30         node_set_t * target_nodes = getTargetNodes(other);
31         target_nodes->add(node);
32 }
33
34 void WaitObj::add_waited_by(thread_id_t other)
35 {
36         waited_by.add(other);
37 }
38
39 /**
40  * Stop waiting for the thread to reach the target node
41  *
42  * @param other The thread to be removed
43  * @param node The target node
44  * @return true if "other" is removed from waiting_for set
45  *         false if only a target node of "other" is removed
46  */
47 bool WaitObj::remove_waiting_for_node(thread_id_t other, FuncNode * node)
48 {
49         dist_map_t * dist_map = getDistMap(other);
50         dist_map->remove(node);
51
52         node_set_t * target_nodes = getTargetNodes(other);
53         target_nodes->remove(node);
54
55         /* The thread has no nodes to reach */
56         if (target_nodes->isEmpty()) {
57                 waiting_for.remove(other);
58                 return true;
59         }
60
61         return false;
62 }
63
64 /* Stop waiting for the thread */
65 void WaitObj::remove_waiting_for(thread_id_t other)
66 {
67         // TODO: clear dist_map or not?
68         waiting_for.remove(other);
69
70         node_set_t * target_nodes = getTargetNodes(other);
71         target_nodes->reset();
72 }
73
74 void WaitObj::remove_waited_by(thread_id_t other)
75 {
76         waited_by.remove(other);
77 }
78
79 int WaitObj::lookup_dist(thread_id_t tid, FuncNode * target)
80 {
81         dist_map_t * map = getDistMap(tid);
82         node_set_t * node_set = getTargetNodes(tid);
83
84         /* thrd_dist_maps is not reset when clear_waiting_for is called,
85          * so node_set should be checked */
86         if (node_set->contains(target) && map->contains(target))
87                 return map->get(target);
88
89         return -1;
90 }
91
92 dist_map_t * WaitObj::getDistMap(thread_id_t tid)
93 {
94         int thread_id = id_to_int(tid);
95         int old_size = thrd_dist_maps.size();
96
97         if (old_size <= thread_id) {
98                 thrd_dist_maps.resize(thread_id + 1);
99                 for (int i = old_size; i < thread_id + 1; i++) {
100                         thrd_dist_maps[i] = new dist_map_t(16);
101                 }
102         }
103
104         return thrd_dist_maps[thread_id];
105 }
106
107 node_set_t * WaitObj::getTargetNodes(thread_id_t tid)
108 {
109         int thread_id = id_to_int(tid);
110         int old_size = thrd_target_nodes.size();
111
112         if (old_size <= thread_id) {
113                 thrd_target_nodes.resize(thread_id + 1);
114                 for (int i = old_size; i < thread_id + 1; i++) {
115                         thrd_target_nodes[i] = new node_set_t(16);
116                 }
117         }
118
119         return thrd_target_nodes[thread_id];
120 }
121
122 /*
123 SnapVector<thread_id_t> WaitObj::incr_waiting_for_counter()
124 {
125         SnapVector<thread_id_t> expire_thrds;
126
127         thrd_id_set_iter * iter = waiting_for.iterator();
128         while (iter->hasNext()) {
129                 thread_id_t waiting_for_id = iter->next();
130                 bool expire = incr_counter(waiting_for_id);
131
132                 if (expire) {
133                         expire_thrds.push_back(waiting_for_id);
134                 }
135         }
136
137         return expire_thrds;
138 }*/
139
140 /**
141  * Increment action counter for thread tid
142  * @return true if the counter for tid expires
143  */
144 bool WaitObj::incr_counter(thread_id_t tid)
145 {
146         int thread_id = id_to_int(tid);
147
148         /* thrd_action_counters.resize does not work here */
149         while (thrd_action_counters.size() <= (uint) thread_id) {
150                 thrd_action_counters.push_back(0);
151         }
152
153         thrd_action_counters[thread_id]++;
154
155         if (thrd_action_counters[thread_id] > 1000)
156                 return true;
157
158         return false;
159 }
160
161 void WaitObj::clear_waiting_for()
162 {
163         thrd_id_set_iter * iter = waiting_for.iterator();
164         while (iter->hasNext()) {
165                 thread_id_t tid = iter->next();
166                 int index = id_to_int(tid);
167                 thrd_target_nodes[index]->reset();
168                 /* thrd_dist_maps are not reset because distances
169                  * will be overwritten when node targets are added */
170         }
171
172         waiting_for.reset();
173         /* waited_by relation should be kept */
174 }
175
176 void WaitObj::print_waiting_for(bool verbose)
177 {
178         if (waiting_for.getSize() == 0)
179                 return;
180
181         model_print("thread %d is waiting for: ", tid);
182         thrd_id_set_iter * it = waiting_for.iterator();
183
184         while (it->hasNext()) {
185                 thread_id_t waiting_for_id = it->next();
186                 model_print("%d ", waiting_for_id);
187         }
188         model_print("\n");
189
190         if (verbose) {
191                 /* Print out the distances from each thread to target nodes */
192                 model_print("\t");
193                 for (uint i = 0; i < thrd_target_nodes.size(); i++) {
194                         dist_map_t * dist_map = getDistMap(i);
195                         node_set_t * node_set = getTargetNodes(i);
196                         node_set_iter * node_iter = node_set->iterator();
197
198                         if (!node_set->isEmpty()) {
199                                 model_print("[thread %d](", int_to_id(i));
200
201                                 while (node_iter->hasNext()){
202                                         FuncNode * node = node_iter->next();
203                                         int dist = dist_map->get(node);
204                                         model_print("node %d: %d, ", node->get_func_id(), dist);
205                                 }
206                                 model_print(") ");
207                         }
208                 }
209                 model_print("\n");
210         }
211 }
212
213 void WaitObj::print_waited_by()
214 {
215         if (waited_by.getSize() == 0)
216                 return;
217
218         model_print("thread %d is waited by: ", tid);
219         thrd_id_set_iter * it = waited_by.iterator();
220
221         while (it->hasNext()) {
222                 thread_id_t thread_id = it->next();
223                 model_print("%d ", thread_id);
224         }
225         model_print("\n");
226 }