Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

ThreadList.java

00001 package gov.nasa.arc.ase.jpf.jvm;
00002 
00003 /**
00004  * Contains the list of the threads.
00005  * It extends the storage interface.
00006  */
00007 public class ThreadList implements Storable {
00008   /**
00009    * The threads.
00010    */
00011   private ThreadInfo[] threads;
00012 
00013   /**
00014    * Reference of the kernel state this dynamic area is in.
00015    */
00016   public KernelState ks;
00017 
00018   /**
00019    * Creates a new empty thread list.
00020    */
00021   public ThreadList(KernelState ks) {
00022     this.ks = ks;
00023     threads = new ThreadInfo[0];
00024   }  
00025   public ThreadList(ThreadList tl) {
00026     int l = tl.threads.length;
00027     threads = new ThreadInfo[l];
00028     for(int i = 0; i < l; i++) {
00029       threads[i] = (ThreadInfo)tl.threads[i].clone();
00030       threads[i].list = this;
00031       threads[i].index = i;
00032     }
00033     ks = null;
00034   }  
00035   /**
00036    * Adds a new thread to the list.
00037    */
00038   public void add(int index, ThreadInfo th) {
00039     th.list = this;
00040     th.index = index;
00041 
00042     if(index >= threads.length) {
00043       ThreadInfo[] n = new ThreadInfo[index+1];
00044       System.arraycopy(threads, 0, n, 0, threads.length);
00045       threads = n;
00046     }
00047 
00048     threads[index] = th;
00049     ks.data = null;
00050   }  
00051   public boolean anyAliveThread() {
00052     for(int i = 0, l = threads.length; i < l; i++)
00053       if(threads[i].isAlive()) return true;
00054 
00055     return false;
00056   }  
00057   /**
00058    * Restores the state.
00059    */
00060   public void backtrackTo(ArrayOffset storing, Object backtrack) {
00061     Object[] b = (Object[])backtrack;
00062 
00063     int length = storing.get();
00064     int l = threads.length;
00065 
00066     if(l != length) {
00067       if(l > length) l = length;
00068 
00069       ThreadInfo[] n = new ThreadInfo[length];
00070       System.arraycopy(threads, 0, n, 0, l);
00071       threads = n;
00072     }
00073 
00074     for(int i = 0; i < length; i++) {
00075       ThreadInfo th = threads[i];
00076 
00077       if(storing.peek() != -1) {
00078     if(th == null) {
00079       th = threads[i] = new ThreadInfo();
00080       th.list = this;
00081       th.index = i;
00082     }
00083 
00084     th.backtrackTo(storing, b[i]);
00085       } else {
00086     threads[i] = null;
00087     storing.get();
00088       }
00089     }
00090   }  
00091   public Object clone() {
00092     return new ThreadList(this);
00093   }  
00094   /**
00095    * Returns a specific thread.
00096    */
00097   public ThreadInfo get(int index) {
00098     return threads[index];
00099   }  
00100   /**
00101    * Returns the data used for restoring the state.
00102    */
00103   public Object getBacktrackData() {
00104     int length = threads.length;
00105 
00106     Object[] data = new Object[length];
00107 
00108     for(int i = 0; i < length; i++)
00109       if(threads[i] != null)
00110     data[i] = threads[i].getBacktrackData();
00111       else
00112     data[i] = null;
00113 
00114     return data;
00115   }  
00116   /**
00117    * Returns the data used for storing the state.
00118    */
00119   public int[] getStoringData() {
00120     int length = threads.length;
00121 
00122     int size = 0;
00123     int[][] threadData = new int[length][];
00124     
00125     for(int i = 0; i < length; i++)
00126       if(threads[i] != null)
00127     size += (threadData[i] = threads[i].getStoringData()).length;
00128       else {
00129     threadData[i] = null;
00130     size++;
00131       }
00132 
00133     int[] data = new int[size + 1];
00134     data[0] = length;
00135 
00136     for(int i = 0, j = 1; i < length; i++) {
00137       if(threadData[i] != null) {
00138     int[] d = threadData[i];
00139     int s = d.length;
00140 
00141     System.arraycopy(d, 0, data, j, s);
00142     j += s;
00143       } else {
00144     data[j] = -1;
00145     size++;
00146     j++;
00147       }
00148     }
00149 
00150     return data;
00151   }  
00152   /**
00153    * Returns the length of the list.
00154    */
00155   public int length() {
00156     return threads.length;
00157   }  
00158 //#endif MARK_N_SWEEP
00159 
00160   public ThreadInfo locate(int objref) {
00161     for(int i = 0, l = threads.length; i < l; i++)
00162       if(threads[i].getObjectReference() == objref)
00163     return threads[i];
00164 
00165     return null;
00166   }  
00167 //#ifdef MARK_N_SWEEP
00168   public void mark() {
00169     for(int i = 0, l = threads.length; i < l; i++)
00170       threads[i].mark();
00171   }  
00172   /**
00173    * Removes a thread from the list.
00174    */
00175   public void remove(int index) {
00176     threads[index].list = null;
00177     threads[index].index = -1;
00178     threads[index] = null;
00179     ks.data = null;
00180   }  
00181 }

Generated at Thu Feb 7 06:57:05 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001