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:
23: /**
24: * a scheduler that simply enumerates all threads, picking the next one (from
25: * it's last choice) that is runnable
26: */
27: public class DefaultScheduler extends Scheduler {
28: private int thread;
29: private int random;
30: private boolean lastRandom;
31:
32: public DefaultScheduler(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: /**
45: * gets called in every cycle (i.e. we start from thread=0) by
46: * JVM.forward
47: * SystemState.prepareNext
48: */
49: public void initialize() {
50: thread = 0;
51: random = -1;
52: lastRandom = true;
53: }
54:
55: /**
56: * sets the thread index to the next runnable thread, called by
57: * JVM.forward
58: * SystemState.nextSuccessor
59: */
60: public ThreadInfo locateThread(SystemState ss) {
61: for (int n = ss.getThreadCount(); thread < n; thread++) {
62: ThreadInfo th = ss.getThreadInfo(thread);
63:
64: if (th.isRunnable()) {
65: return th;
66: }
67: }
68:
69: return null;
70: }
71:
72: /**
73: * this is NOT the next runnable thread index! it just makes sure we get
74: * another thread the next time (if there is a runnable one). Called by
75: * JVM.forward
76: * SystemState.scheduleNext
77: */
78: public void next() {
79: if (lastRandom) {
80: random = -1;
81: thread++;
82: } else {
83: random++;
84: }
85: }
86:
87: public int random(int max) {
88: if (random == -1) {
89: random = 0;
90: }
91:
92: lastRandom = (random == max - 1);
93:
94: return random;
95: }
96: }
|