KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > InteractiveScheduler


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// 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 gov.nasa.jpf.JPFException;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27
28 import java.util.BitSet JavaDoc;
29
30
31 /**
32  * a scheduler that exlicitly asks the user for the next thread to pick
33  */

34 public class InteractiveScheduler extends Scheduler {
35   private static BufferedReader JavaDoc br = new BufferedReader JavaDoc(
36                                            new InputStreamReader JavaDoc(System.in));
37   private static boolean quit = false;
38   private int thread;
39   private int random;
40   private boolean lastRandom;
41
42   public InteractiveScheduler (Config config) {
43     initialize();
44   }
45
46   public int getRandom () {
47     return random;
48   }
49
50   public int getThread () {
51     return thread;
52   }
53
54   public void initialize () {
55     thread = 0;
56     random = -1;
57     lastRandom = true;
58   }
59
60   public ThreadInfo locateThread (SystemState ss) {
61     if (quit) {
62       return null;
63     }
64
65     int nthreads = ss.getThreadCount();
66     int nrunnable = 0;
67     BitSet JavaDoc runnable = new BitSet JavaDoc();
68
69     for (int i = 0; i < nthreads; i++) {
70       ThreadInfo th = ss.getThreadInfo(i);
71
72       if (th.isRunnable()) {
73         runnable.set(i);
74         nrunnable++;
75       }
76     }
77
78     if (nrunnable == 0) {
79       System.out.println("Deadlock: backing up");
80
81       return null;
82     }
83
84     System.out.print("Runnable threads [" + nrunnable + "]: ");
85
86     for (int i = 0; i < nthreads; i++) {
87       if (runnable.get(i)) {
88         if (i == thread) {
89           System.out.print("[" + i + "] ");
90         } else {
91           System.out.print(i + " ");
92         }
93       }
94     }
95
96     System.out.println();
97
98     while (true) {
99       try {
100         System.out.print("> ");
101
102         String JavaDoc s = br.readLine();
103
104         if (s.equals("quit") || s.equals("q")) {
105           quit = true;
106
107           return null;
108         } else if (s.equals("backtrack") || s.equals("back") ||
109                        s.equals("b")) {
110           return null;
111         } else if (s.equals("show") || s.equals("s")) {
112           while (true) {
113             System.out.print("show> ");
114             s = br.readLine();
115
116             if (s.equals("")) {
117               break;
118             }
119
120             try {
121               int l = Integer.parseInt(s);
122
123               if (runnable.get(l)) {
124                 ThreadInfo th = (ThreadInfo) ss.getThreadInfo(l);
125                 System.out.println(th.getMethod().getCompleteName() + ":" +
126                                    th.getPC().getPosition() + " " +
127                                    th.getPC());
128
129                 break;
130               }
131             } catch (NumberFormatException JavaDoc e) {
132             }
133           }
134         } else if (s.equals("random") || s.equals("r")) {
135           while (true) {
136             System.out.println("Current random: " + random);
137             System.out.print("random> ");
138             s = br.readLine();
139
140             if (s.equals("")) {
141               break;
142             }
143
144             try {
145               random = Integer.parseInt(s);
146
147               break;
148             } catch (NumberFormatException JavaDoc e) {
149             }
150           }
151         } else if (s.equals("help") || s.equals("h") || s.equals("?")) {
152           System.out.println("command:");
153           System.out.println(" quit");
154           System.out.println(" random");
155           System.out.println(" backtrack");
156           System.out.println(" help");
157         } else {
158           if (runnable.get(thread) && s.equals("")) {
159             break;
160           }
161
162           try {
163             int i = Integer.parseInt(s);
164
165             if (runnable.get(i)) {
166               thread = i;
167
168               break;
169             }
170           } catch (NumberFormatException JavaDoc e) {
171           }
172         }
173       } catch (IOException JavaDoc e) {
174         throw new JPFException(e.getMessage());
175       }
176     }
177
178     System.out.println("Running thread " + thread);
179
180     return ss.getThreadInfo(thread);
181   }
182
183   public void next () {
184     if (lastRandom) {
185       random = -1;
186       thread++;
187     } else {
188       random++;
189     }
190   }
191
192   public int random (int max) {
193     if (random == -1) {
194       random = 0;
195     }
196
197     lastRandom = (random == max - 1);
198
199     return random;
200   }
201 }
Popular Tags