01: //
02: // Copyright (C) 2005 United States Government as represented by the
03: // Administrator of the National Aeronautics and Space Administration
04: // (NASA). All Rights Reserved.
05: //
06: // This software is distributed under the NASA Open Source Agreement
07: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
08: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
09: // directory tree for the complete NOSA document.
10: //
11: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18: //
19: package gov.nasa.jpf.jvm;
20:
21: import gov.nasa.jpf.Config;
22: import java.util.Random;
23:
24: /**
25: * <2do> pcm - that should be the deterministic scheduler (it's used in Simulation)
26: */
27: public class RandomScheduler extends Scheduler {
28: private static Random rand = new Random();
29: private int thread;
30: private int random;
31:
32: public RandomScheduler(Config config) {
33: initialize();
34: }
35:
36: public int getRandom() {
37: return random;
38: }
39:
40: public int getThread() {
41: return thread;
42: }
43:
44: public void initialize() {
45: thread = 0;
46: random = -1;
47: }
48:
49: public ThreadInfo locateThread(SystemState ss) {
50: int n = ss.getThreadCount();
51: int nthreads = 0;
52: ThreadInfo[] threads = new ThreadInfo[n];
53: int[] refs = new int[n];
54:
55: for (int i = 0; i < n; i++) {
56: ThreadInfo th = ss.getThreadInfo(i);
57:
58: if (th.isRunnable()) {
59: refs[nthreads] = i;
60: threads[nthreads++] = th;
61: }
62: }
63:
64: if (nthreads == 0) {
65: return null;
66: }
67:
68: int r = rand.nextInt(nthreads);
69:
70: thread = refs[r];
71:
72: return threads[r];
73: }
74:
75: public void next() {
76: initialize();
77: }
78:
79: public int random(int max) {
80: random = rand.nextInt(max);
81:
82: return random;
83: }
84: }
|