1 package gov.nasa.jpf.jvm; 20 21 import gov.nasa.jpf.Config; 22 23 27 public class ThreadList implements Storable { 28 31 private ThreadInfo[] threads; 32 33 36 public KernelState ks; 37 38 41 public ThreadList (Config config, KernelState ks) { 42 this.ks = ks; 43 threads = new ThreadInfo[0]; 44 } 45 46 public ThreadList (ThreadList tl) { 47 int l = tl.threads.length; 48 threads = new ThreadInfo[l]; 49 50 for (int i = 0; i < l; i++) { 51 threads[i] = (ThreadInfo) tl.threads[i].clone(); 52 threads[i].list = this; 53 threads[i].index = i; 54 } 55 56 ks = null; 57 } 58 59 62 public Object getBacktrackData () { 63 int length = threads.length; 64 65 Object [] data = new Object [length]; 66 67 for (int i = 0; i < length; i++) { 68 if (threads[i] != null) { 69 data[i] = threads[i].getBacktrackData(); 70 } else { 71 data[i] = null; 72 } 73 } 74 75 return data; 76 } 77 78 81 public int[] getStoringData () { 82 int length = threads.length; 83 84 int size = 0; 85 int[][] threadData = new int[length][]; 86 87 for (int i = 0; i < length; i++) { 88 if (threads[i] != null) { 89 size += (threadData[i] = threads[i].getStoringData()).length; 90 } else { 91 threadData[i] = null; 92 size++; 93 } 94 } 95 96 int[] data = new int[size + 1]; 97 data[0] = length; 98 99 for (int i = 0, j = 1; i < length; i++) { 100 if (threadData[i] != null) { 101 int[] d = threadData[i]; 102 int s = d.length; 103 104 System.arraycopy(d, 0, data, j, s); 105 j += s; 106 } else { 107 data[j] = -1; 108 size++; 109 j++; 110 } 111 } 112 113 return data; 114 } 115 116 119 public void add (int index, ThreadInfo th) { 120 th.list = this; 121 th.index = index; 122 123 if (index >= threads.length) { 124 ThreadInfo[] n = new ThreadInfo[index + 1]; 125 System.arraycopy(threads, 0, n, 0, threads.length); 126 threads = n; 127 } 128 129 threads[index] = th; 130 ks.data = null; 131 } 132 133 public boolean anyAliveThread () { 134 for (int i = 0, l = threads.length; i < l; i++) { 135 if (threads[i].isAlive()) { 136 return true; 137 } 138 } 139 140 return false; 141 } 142 143 146 public void backtrackTo (ArrayOffset storing, Object backtrack) { 147 Object [] b = (Object []) backtrack; 148 149 int length = storing.get(); 150 int l = threads.length; 151 152 if (l != length) { 153 if (l > length) { 154 l = length; 155 } 156 157 ThreadInfo[] n = new ThreadInfo[length]; 158 System.arraycopy(threads, 0, n, 0, l); 159 threads = n; 160 } 161 162 for (int i = 0; i < length; i++) { 163 ThreadInfo ti = threads[i]; 164 165 if (storing.peek() != -1) { 166 if (ti == null) { 167 ti = threads[i] = new ThreadInfo(this, i); 168 } 169 170 ti.backtrackTo(storing, b[i]); 171 } else { 172 threads[i] = null; 173 storing.get(); 174 } 175 } 176 } 177 178 public Object clone () { 179 return new ThreadList(this); 180 } 181 182 185 public ThreadInfo get (int index) { 186 return threads[index]; 187 } 188 189 192 public int length () { 193 return threads.length; 194 } 195 196 public ThreadInfo locate (int objref) { 197 for (int i = 0, l = threads.length; i < l; i++) { 198 if (threads[i].getObjectReference() == objref) { 199 return threads[i]; 200 } 201 } 202 203 return null; 204 } 205 206 public void markRoots () { 207 for (int i = 0, l = threads.length; i < l; i++) { 208 if (threads[i].isAlive()) { 209 threads[i].markRoots(); 210 } 211 } 212 } 213 214 public int getNonDaemonThreadCount () { 215 int nd = 0; 216 217 for (int i = 0; i < threads.length; i++) { 218 if (!threads[i].isDaemon()) { 219 nd++; 220 } 221 } 222 223 return nd; 224 } 225 226 public int getRunnableThreadCount () { 227 int n = 0; 228 229 for (int i = 0; i < threads.length; i++) { 230 if (threads[i].isRunnable()) { 231 n++; 232 } 233 } 234 235 return n; 236 } 237 238 boolean hasOtherRunnablesThan (int idx) { 239 int i; 240 int n = threads.length; 241 242 for (i=0; i<n; i++) { 243 if (i != idx) { 244 if (threads[i].isRunnable()) { 245 return true; 246 } 247 } 248 } 249 250 return false; 251 } 252 253 256 public void remove (int index) { 257 threads[index].list = null; 258 threads[index].index = -1; 259 threads[index] = null; 260 ks.data = null; 261 } 262 } 263 | Popular Tags |