2 #include "threads-model.h"
5 WaitObj::WaitObj(thread_id_t tid) :
15 for (uint i = 0; i < thrd_dist_maps.size(); i++)
16 delete thrd_dist_maps[i];
18 for (uint i = 0; i < thrd_target_nodes.size(); i++)
19 delete thrd_target_nodes[i];
22 void WaitObj::add_waiting_for(thread_id_t other, FuncNode * node, int dist)
24 waiting_for.add(other);
26 dist_map_t * dist_map = getDistMap(other);
27 dist_map->put(node, dist);
29 node_set_t * target_nodes = getTargetNodes(other);
30 target_nodes->add(node);
33 void WaitObj::add_waited_by(thread_id_t other)
39 * Stop waiting for the thread to reach the target node
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
46 bool WaitObj::remove_waiting_for(thread_id_t other, FuncNode * node)
48 dist_map_t * dist_map = getDistMap(other);
49 dist_map->remove(node);
51 node_set_t * target_nodes = getTargetNodes(other);
52 target_nodes->remove(node);
54 /* The thread has no nodes to reach */
55 if (target_nodes->isEmpty()) {
56 waiting_for.remove(other);
63 void WaitObj::remove_waited_by(thread_id_t other)
65 waited_by.remove(other);
68 int WaitObj::lookup_dist(thread_id_t tid, FuncNode * target)
70 dist_map_t * map = getDistMap(tid);
71 if (map->contains(target))
72 return map->get(target);
77 dist_map_t * WaitObj::getDistMap(thread_id_t tid)
79 int thread_id = id_to_int(tid);
80 int old_size = thrd_dist_maps.size();
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);
89 return thrd_dist_maps[thread_id];
92 node_set_t * WaitObj::getTargetNodes(thread_id_t tid)
94 int thread_id = id_to_int(tid);
95 int old_size = thrd_target_nodes.size();
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);
104 return thrd_target_nodes[thread_id];
107 void WaitObj::clear_waiting_for()
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 */
119 /* waited_by relation should be kept */
122 void WaitObj::print_waiting_for(bool verbose)
124 if (waiting_for.getSize() == 0)
127 model_print("thread %d is waiting for: ", tid);
128 thrd_id_set_iter * it = waiting_for.iterator();
130 while (it->hasNext()) {
131 thread_id_t waiting_for_id = it->next();
132 model_print("%d ", waiting_for_id);
137 /* Print out the distances from each thread to target nodes */
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();
144 if (!node_set->isEmpty()) {
145 model_print("[thread %d](", int_to_id(i));
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);
159 void WaitObj::print_waited_by()
161 if (waited_by.getSize() == 0)
164 model_print("thread %d is waited by: ", tid);
165 thrd_id_set_iter * it = waited_by.iterator();
167 while (it->hasNext()) {
168 thread_id_t thread_id = it->next();
169 model_print("%d ", thread_id);