2 #include "threads-model.h"
5 WaitObj::WaitObj(thread_id_t tid) :
11 thrd_action_counters()
16 for (uint i = 0; i < thrd_dist_maps.size(); i++)
17 delete thrd_dist_maps[i];
19 for (uint i = 0; i < thrd_target_nodes.size(); i++)
20 delete thrd_target_nodes[i];
23 void WaitObj::add_waiting_for(thread_id_t other, FuncNode * node, int dist)
25 waiting_for.add(other);
27 dist_map_t * dist_map = getDistMap(other);
28 dist_map->put(node, dist);
30 node_set_t * target_nodes = getTargetNodes(other);
31 target_nodes->add(node);
34 void WaitObj::add_waited_by(thread_id_t other)
40 * Stop waiting for the thread to reach the target node
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
47 bool WaitObj::remove_waiting_for_node(thread_id_t other, FuncNode * node)
49 dist_map_t * dist_map = getDistMap(other);
50 dist_map->remove(node);
52 node_set_t * target_nodes = getTargetNodes(other);
53 target_nodes->remove(node);
55 /* The thread has no nodes to reach */
56 if (target_nodes->isEmpty()) {
57 int index = id_to_int(other);
58 thrd_action_counters[index] = 0;
59 waiting_for.remove(other);
67 /* Stop waiting for the thread */
68 void WaitObj::remove_waiting_for(thread_id_t other)
70 waiting_for.remove(other);
72 // TODO: clear dist_map or not?
73 /* dist_map_t * dist_map = getDistMap(other);
76 node_set_t * target_nodes = getTargetNodes(other);
77 target_nodes->reset();
79 int index = id_to_int(other);
80 thrd_action_counters[index] = 0;
83 void WaitObj::remove_waited_by(thread_id_t other)
85 waited_by.remove(other);
88 int WaitObj::lookup_dist(thread_id_t tid, FuncNode * target)
90 dist_map_t * map = getDistMap(tid);
91 node_set_t * node_set = getTargetNodes(tid);
93 /* thrd_dist_maps is not reset when clear_waiting_for is called,
94 * so node_set should be checked */
95 if (node_set->contains(target) && map->contains(target))
96 return map->get(target);
101 dist_map_t * WaitObj::getDistMap(thread_id_t tid)
103 int thread_id = id_to_int(tid);
104 int old_size = thrd_dist_maps.size();
106 if (old_size <= thread_id) {
107 thrd_dist_maps.resize(thread_id + 1);
108 for (int i = old_size; i < thread_id + 1; i++) {
109 thrd_dist_maps[i] = new dist_map_t(16);
113 return thrd_dist_maps[thread_id];
116 node_set_t * WaitObj::getTargetNodes(thread_id_t tid)
118 int thread_id = id_to_int(tid);
119 int old_size = thrd_target_nodes.size();
121 if (old_size <= thread_id) {
122 thrd_target_nodes.resize(thread_id + 1);
123 for (int i = old_size; i < thread_id + 1; i++) {
124 thrd_target_nodes[i] = new node_set_t(16);
128 return thrd_target_nodes[thread_id];
132 * Increment action counter for thread tid
133 * @return true if the counter for tid expires
135 bool WaitObj::incr_counter(thread_id_t tid)
137 int thread_id = id_to_int(tid);
139 /* thrd_action_counters.resize does not work here */
140 while (thrd_action_counters.size() <= (uint) thread_id) {
141 thrd_action_counters.push_back(0);
144 thrd_action_counters[thread_id]++;
145 if (thrd_action_counters[thread_id] > 1000)
151 void WaitObj::clear_waiting_for()
153 thrd_id_set_iter * iter = waiting_for.iterator();
154 while (iter->hasNext()) {
155 thread_id_t tid = iter->next();
156 int index = id_to_int(tid);
157 thrd_action_counters[index] = 0;
159 /* thrd_dist_maps are not reset because distances
160 * will be overwritten when node targets are added
161 * thrd_dist_maps[index]->reset(); */
163 node_set_t * target_nodes = getTargetNodes(tid);
164 target_nodes->reset();
168 /* waited_by relation should be kept */
171 void WaitObj::print_waiting_for(bool verbose)
173 if (waiting_for.getSize() == 0)
176 model_print("thread %d is waiting for: ", tid);
177 thrd_id_set_iter * it = waiting_for.iterator();
179 while (it->hasNext()) {
180 thread_id_t waiting_for_id = it->next();
181 model_print("%d ", waiting_for_id);
186 /* Print out the distances from each thread to target nodes */
188 for (uint i = 0; i < thrd_target_nodes.size(); i++) {
189 dist_map_t * dist_map = getDistMap(i);
190 node_set_t * node_set = getTargetNodes(i);
191 node_set_iter * node_iter = node_set->iterator();
193 if (!node_set->isEmpty()) {
194 model_print("[thread %d](", int_to_id(i));
196 while (node_iter->hasNext()){
197 FuncNode * node = node_iter->next();
198 int dist = dist_map->get(node);
199 model_print("node %d: %d, ", node->get_func_id(), dist);
208 void WaitObj::print_waited_by()
210 if (waited_by.getSize() == 0)
213 model_print("thread %d is waited by: ", tid);
214 thrd_id_set_iter * it = waited_by.iterator();
216 while (it->hasNext()) {
217 thread_id_t thread_id = it->next();
218 model_print("%d ", thread_id);