00001 package gov.nasa.arc.ase.jpf.jvm;
00002
00003 import gov.nasa.arc.ase.jpf.iSystemState;
00004 import gov.nasa.arc.ase.jpf.iThreadInfo;
00005 import gov.nasa.arc.ase.jpf.JPFErrorException;
00006 import java.util.Random;
00007
00008 public class RandomScheduler extends Scheduler {
00009 private static Random rand = new Random();
00010 private int thread;
00011 private int random;
00012
00013 public RandomScheduler(iSystemState ss) {
00014 initialize(ss);
00015 }
00016 public RandomScheduler(RandomScheduler rs) {
00017 thread = rs.thread;
00018 random = rs.random;
00019 ss = null;
00020 }
00021 public Object clone() {
00022 return new RandomScheduler(this);
00023 }
00024 public int getRandom() {
00025 return random;
00026 }
00027 public int getThread() {
00028 return thread;
00029 }
00030 public void initialize(iSystemState ss) {
00031 this.ss = (SystemState)ss;
00032 thread = 0;
00033 random = -1;
00034 }
00035 public iThreadInfo locateThread(iSystemState ss) {
00036 int n = ss.getThreadCount();
00037 int nthreads = 0;
00038 iThreadInfo[] threads = new iThreadInfo[n];
00039 int[] refs = new int[n];
00040
00041 for(int i = 0; i < n; i++) {
00042 iThreadInfo th = ss.getThreadInfo(i);
00043 if(th.isRunnable()) {
00044 refs[nthreads] = i;
00045 threads[nthreads++] = th;
00046 }
00047 }
00048
00049 if(nthreads == 0) return null;
00050
00051
00052
00053
00054 int r = rand.nextInt(nthreads);
00055
00056
00057 thread = refs[r];
00058
00059 return threads[r];
00060 }
00061 public void next() {
00062 initialize(ss);
00063 }
00064 public int random(int max) {
00065
00066
00067
00068 return random = rand.nextInt(max);
00069
00070 }
00071 }