Add some documentation for WaitObj
[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 {}
12
13 WaitObj::~WaitObj()
14 {
15         for (uint i = 0; i < thrd_dist_maps.size(); i++)
16                 delete thrd_dist_maps[i];
17
18         for (uint i = 0; i < thrd_target_nodes.size(); i++)
19                 delete thrd_target_nodes[i];
20 }
21
22 void WaitObj::add_waiting_for(thread_id_t other, FuncNode * node, int dist)
23 {
24         waiting_for.add(other);
25
26         dist_map_t * dist_map = getDistMap(other);
27         dist_map->put(node, dist);
28
29         node_set_t * target_nodes = getTargetNodes(other);
30         target_nodes->add(node);
31 }
32
33 void WaitObj::add_waited_by(thread_id_t other)
34 {
35         waited_by.add(other);
36 }
37
38 /**
39  * Stop waiting for the thread to reach the target node
40  *
41  * @param other The thread to be removed
42  * @param node The target node
43  * @return true if "other" is removed from waiting_for set
44  *         false if only a target node of "other" is removed
45  */
46 bool WaitObj::remove_waiting_for(thread_id_t other, FuncNode * node)
47 {
48         dist_map_t * dist_map = getDistMap(other);
49         dist_map->remove(node);
50
51         node_set_t * target_nodes = getTargetNodes(other);
52         target_nodes->remove(node);
53
54         /* The thread has no nodes to reach */
55         if (target_nodes->isEmpty()) {
56                 waiting_for.remove(other);
57                 return true;
58         }
59
60         return false;
61 }
62
63 void WaitObj::remove_waited_by(thread_id_t other)
64 {
65         waited_by.remove(other);
66 }
67
68 int WaitObj::lookup_dist(thread_id_t tid, FuncNode * target)
69 {
70         dist_map_t * map = getDistMap(tid);
71         if (map->contains(target))
72                 return map->get(target);
73
74         return -1;
75 }
76
77 dist_map_t * WaitObj::getDistMap(thread_id_t tid)
78 {
79         int thread_id = id_to_int(tid);
80         int old_size = thrd_dist_maps.size();
81
82         if (old_size <= thread_id) {
83                 thrd_dist_maps.resize(thread_id + 1);
84                 for (int i = old_size; i < thread_id + 1; i++) {
85                         thrd_dist_maps[i] = new dist_map_t(16);
86                 }
87         }
88
89         return thrd_dist_maps[thread_id];
90 }
91
92 node_set_t * WaitObj::getTargetNodes(thread_id_t tid)
93 {
94         int thread_id = id_to_int(tid);
95         int old_size = thrd_target_nodes.size();
96
97         if (old_size <= thread_id) {
98                 thrd_target_nodes.resize(thread_id + 1);
99                 for (int i = old_size; i < thread_id + 1; i++) {
100                         thrd_target_nodes[i] = new node_set_t(16);
101                 }
102         }
103
104         return thrd_target_nodes[thread_id];
105 }
106
107 void WaitObj::clear_waiting_for()
108 {
109         thrd_id_set_iter * iter = waiting_for.iterator();
110         while (iter->hasNext()) {
111                 thread_id_t tid = iter->next();
112                 int index = id_to_int(tid);
113                 thrd_target_nodes[index]->reset();
114                 /* thrd_dist_maps are not reset because distances
115                  * will be overwritten when node targets are added */
116         }
117
118         waiting_for.reset();
119         /* waited_by relation should be kept */
120 }
121
122 void WaitObj::print_waiting_for(bool verbose)
123 {
124         if (waiting_for.getSize() == 0)
125                 return;
126
127         model_print("thread %d is waiting for: ", tid);
128         thrd_id_set_iter * it = waiting_for.iterator();
129
130         while (it->hasNext()) {
131                 thread_id_t waiting_for_id = it->next();
132                 model_print("%d ", waiting_for_id);
133         }
134         model_print("\n");
135
136         if (verbose) {
137                 /* Print out the distances from each thread to target nodes */
138                 model_print("\t");
139                 for (uint i = 0; i < thrd_target_nodes.size(); i++) {
140                         dist_map_t * dist_map = getDistMap(i);
141                         node_set_t * node_set = getTargetNodes(i);
142                         node_set_iter * node_iter = node_set->iterator();
143
144                         if (!node_set->isEmpty()) {
145                                 model_print("[thread %d](", int_to_id(i));
146
147                                 while (node_iter->hasNext()){
148                                         FuncNode * node = node_iter->next();
149                                         int dist = dist_map->get(node);
150                                         model_print("node %d: %d, ", node->get_func_id(), dist);
151                                 }
152                                 model_print(") ");
153                         }
154                 }
155                 model_print("\n");
156         }
157 }
158
159 void WaitObj::print_waited_by()
160 {
161         if (waited_by.getSize() == 0)
162                 return;
163
164         model_print("thread %d is waited by: ", tid);
165         thrd_id_set_iter * it = waited_by.iterator();
166
167         while (it->hasNext()) {
168                 thread_id_t thread_id = it->next();
169                 model_print("%d ", thread_id);
170         }
171         model_print("\n");
172
173 }