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