2 #include "threads-model.h"
5 #define COUNTER_THRESHOLD 1000
7 WaitObj::WaitObj(thread_id_t tid) :
13 thrd_action_counters()
18 for (uint i = 0;i < thrd_dist_maps.size();i++)
19 delete thrd_dist_maps[i];
21 for (uint i = 0;i < thrd_target_nodes.size();i++)
22 delete thrd_target_nodes[i];
25 void WaitObj::add_waiting_for(thread_id_t other, FuncNode * node, int dist)
27 waiting_for.add(other);
29 dist_map_t * dist_map = getDistMap(other);
30 dist_map->put(node, dist);
32 node_set_t * target_nodes = getTargetNodes(other);
33 target_nodes->add(node);
36 void WaitObj::add_waited_by(thread_id_t other)
42 * Stop waiting for the thread to reach the target node
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
49 bool WaitObj::remove_waiting_for_node(thread_id_t other, FuncNode * node)
51 dist_map_t * dist_map = getDistMap(other);
52 dist_map->remove(node);
54 node_set_t * target_nodes = getTargetNodes(other);
55 target_nodes->remove(node);
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);
69 /* Stop waiting for the thread */
70 void WaitObj::remove_waiting_for(thread_id_t other)
72 waiting_for.remove(other);
74 // TODO: clear dist_map or not?
75 /* dist_map_t * dist_map = getDistMap(other);
78 node_set_t * target_nodes = getTargetNodes(other);
79 target_nodes->reset();
81 int index = id_to_int(other);
82 thrd_action_counters[index] = 0;
85 void WaitObj::remove_waited_by(thread_id_t other)
87 waited_by.remove(other);
90 int WaitObj::lookup_dist(thread_id_t tid, FuncNode * target)
92 dist_map_t * map = getDistMap(tid);
93 node_set_t * node_set = getTargetNodes(tid);
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);
103 dist_map_t * WaitObj::getDistMap(thread_id_t tid)
105 int thread_id = id_to_int(tid);
106 int old_size = thrd_dist_maps.size();
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);
115 return thrd_dist_maps[thread_id];
118 node_set_t * WaitObj::getTargetNodes(thread_id_t tid)
120 int thread_id = id_to_int(tid);
121 int old_size = thrd_target_nodes.size();
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);
130 return thrd_target_nodes[thread_id];
134 * Increment action counter for thread tid
135 * @return true if the counter for tid expires
137 bool WaitObj::incr_counter(thread_id_t tid)
139 int thread_id = id_to_int(tid);
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);
146 thrd_action_counters[thread_id]++;
147 if (thrd_action_counters[thread_id] > COUNTER_THRESHOLD) {
148 thrd_action_counters[thread_id] = 0;
155 void WaitObj::clear_waiting_for()
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;
163 /* thrd_dist_maps are not reset because distances
164 * will be overwritten when node targets are added
165 * thrd_dist_maps[index]->reset(); */
167 node_set_t * target_nodes = getTargetNodes(tid);
168 target_nodes->reset();
172 /* waited_by relation should be kept */
175 void WaitObj::print_waiting_for(bool verbose)
177 if (waiting_for.getSize() == 0)
180 model_print("thread %d is waiting for: ", tid);
181 thrd_id_set_iter * it = waiting_for.iterator();
183 while (it->hasNext()) {
184 thread_id_t waiting_for_id = it->next();
185 model_print("%d ", waiting_for_id);
190 /* Print out the distances from each thread to target nodes */
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();
197 if (!node_set->isEmpty()) {
198 model_print("[thread %d](", int_to_id(i));
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);
212 void WaitObj::print_waited_by()
214 if (waited_by.getSize() == 0)
217 model_print("thread %d is waited by: ", tid);
218 thrd_id_set_iter * it = waited_by.iterator();
220 while (it->hasNext()) {
221 thread_id_t thread_id = it->next();
222 model_print("%d ", thread_id);