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 waiting_for.remove(other);
64 /* Stop waiting for the thread */
65 void WaitObj::remove_waiting_for(thread_id_t other)
67 // TODO: clear dist_map or not?
68 waiting_for.remove(other);
70 node_set_t * target_nodes = getTargetNodes(other);
71 target_nodes->reset();
74 void WaitObj::remove_waited_by(thread_id_t other)
76 waited_by.remove(other);
79 int WaitObj::lookup_dist(thread_id_t tid, FuncNode * target)
81 dist_map_t * map = getDistMap(tid);
82 node_set_t * node_set = getTargetNodes(tid);
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);
92 dist_map_t * WaitObj::getDistMap(thread_id_t tid)
94 int thread_id = id_to_int(tid);
95 int old_size = thrd_dist_maps.size();
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);
104 return thrd_dist_maps[thread_id];
107 node_set_t * WaitObj::getTargetNodes(thread_id_t tid)
109 int thread_id = id_to_int(tid);
110 int old_size = thrd_target_nodes.size();
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);
119 return thrd_target_nodes[thread_id];
123 SnapVector<thread_id_t> WaitObj::incr_waiting_for_counter()
125 SnapVector<thread_id_t> expire_thrds;
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);
133 expire_thrds.push_back(waiting_for_id);
141 * Increment action counter for thread tid
142 * @return true if the counter for tid expires
144 bool WaitObj::incr_counter(thread_id_t tid)
146 int thread_id = id_to_int(tid);
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);
153 thrd_action_counters[thread_id]++;
155 if (thrd_action_counters[thread_id] > 1000)
161 void WaitObj::clear_waiting_for()
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 */
173 /* waited_by relation should be kept */
176 void WaitObj::print_waiting_for(bool verbose)
178 if (waiting_for.getSize() == 0)
181 model_print("thread %d is waiting for: ", tid);
182 thrd_id_set_iter * it = waiting_for.iterator();
184 while (it->hasNext()) {
185 thread_id_t waiting_for_id = it->next();
186 model_print("%d ", waiting_for_id);
191 /* Print out the distances from each thread to target nodes */
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();
198 if (!node_set->isEmpty()) {
199 model_print("[thread %d](", int_to_id(i));
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);
213 void WaitObj::print_waited_by()
215 if (waited_by.getSize() == 0)
218 model_print("thread %d is waited by: ", tid);
219 thrd_id_set_iter * it = waited_by.iterator();
221 while (it->hasNext()) {
222 thread_id_t thread_id = it->next();
223 model_print("%d ", thread_id);