KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 /**
23  * MJI NativePeer class for java.lang.Thread library abstraction
24  */

25 public class JPF_java_lang_Thread {
26   
27   public static boolean isAlive (MJIEnv env, int objref) {
28     return getThreadInfo(env, objref).isAlive();
29   }
30
31   public static void setDaemon0 (MJIEnv env, int objref, boolean isDaemon) {
32     ThreadInfo ti = getThreadInfo(env, objref);
33     ti.setDaemon(isDaemon);
34   }
35
36   public static boolean isInterrupted (MJIEnv env, int objref,
37                                        boolean clearStatus) {
38     return getThreadInfo(env, objref).isInterrupted(clearStatus);
39   }
40
41   public static void setName0 (MJIEnv env, int objref, int nameRef) {
42     // it bails if you try to set a null name
43
if (nameRef == -1) {
44       env.throwException("java.lang.IllegalArgumentException");
45
46       return;
47     }
48
49     // we have to intercept this to cache the name as a Java object
50
// (to be stored in ThreadData)
51
// luckily enough, it's copied into the java.lang.Thread object
52
// as a char[], i.e. does not have to preserve identity
53
// Note the nastiness in here - the java.lang.Thread object is only used
54
// to get the initial values into ThreadData, and gets inconsistent
55
// if this method is called (just works because the 'name' field is only
56
// directly accessed from within the Thread ctors)
57
ThreadInfo ti = getThreadInfo(env, objref);
58     ti.setName(env.getStringObject(nameRef));
59   }
60
61   public static void setPriority0 (MJIEnv env, int objref, int prio) {
62     // again, we have to cache this in ThreadData for performance reasons
63
ThreadInfo ti = getThreadInfo(env, objref);
64     ti.setPriority(prio);
65   }
66
67   public static int countStackFrames (MJIEnv env, int objref) {
68     return getThreadInfo(env, objref).countStackFrames();
69   }
70
71   public static int currentThread (MJIEnv env, int clsObjRef) {
72     ThreadInfo ti = env.getThreadInfo();
73
74     return ti.getObjectReference();
75   }
76
77   public static boolean holdsLock (MJIEnv env, int clsObjRef, int objref) {
78     ThreadInfo ti = env.getThreadInfo();
79     ElementInfo ei = env.getElementInfo(objref);
80
81     return ei.isLockedBy(ti);
82   }
83
84   /**
85    * This method is the common initializer for all Thread ctors, and the only
86    * single location where we can init our ThreadInfo, but it is PRIVATE
87    */

88
89   //__Ljava_lang_ThreadGroup__2Ljava_lang_Runnable__2Ljava_lang_String__2JZ
90
public static void init0 (MJIEnv env, int objref, int rGroup, int rRunnable,
91                             int rName, long stackSize) {
92     ThreadInfo ti = getThreadInfo(env, objref);
93     ti.init(rGroup, rRunnable, rName, stackSize, true);
94   }
95
96   public static void interrupt (MJIEnv env, int objref) {
97     env.getElementInfo(objref).interrupt();
98   }
99
100   public static void registerNatives (MJIEnv env, int clsObjRef) {
101     // nothing
102
}
103
104   public static void resume0 (MJIEnv env, int objref) {
105     // very deprecated
106
}
107
108   public static void sleep__J (MJIEnv env, int clsObjRef, long millis) {
109     // no time
110
}
111
112   public static void sleep__JI (MJIEnv env, int clsObjRef, long millis,
113                                 int nanos) {
114     // no time
115
}
116
117   // <2do> pcm - I don't think so, find out!
118
// it's synced, and if the real native impl doesn't do the
119
// MONITOREXIT trick, it wouldn't go unlocked
120

121   /**
122      public static boolean $isExecutable_start (MJIEnv env, int objref) {
123        ThreadInfo newThread = getThreadInfo(env,objref);
124        int target = env.getIntField(objref,"target");
125        
126        if (target == -1) {
127          target = objref;
128        }
129      
130          // better late than never
131          newThread.setTarget(target);
132      
133          // bug fix, only grab lock if "isSynchronized" 3/1/2002
134          // <2do> pcm - I don't think that's right, start nowadays IS synced
135          ElementInfo ei = env.getElementInfo(target);
136          ClassInfo ci = ei.getClassInfo();
137          MethodInfo run = ci.getMethod("run()", true);
138      
139          if (run.isSynchronized()) {
140            return ei.canLock(newThread);
141          }
142      
143          return true;
144        }
145    **/

146   public static void start (MJIEnv env, int objref) {
147     ThreadInfo newThread = getThreadInfo(env, objref);
148     
149     // check if this thread was already started. If it's still running, this
150
// is a IllegalThreadStateException. If it already terminated, it just gets
151
// silently ignored in Java 1.4, but the 1.5 spec explicitly marks this
152
// as illegal, so we adopt this by throwing an IllegalThreadState, too
153
if (newThread.getStatus() != ThreadInfo.NEW) {
154       env.throwException("java.lang.IllegalThreadStateException");
155       return;
156     }
157     
158     // Outch - that's bad. we have to dig this out from the innards
159
// of the java.lang.Thread class
160
int target = newThread.getTarget();
161
162     if (target == -1) {
163       // note that we don't set the 'target' field, since java.lang.Thread doesn't
164
target = objref;
165     }
166
167     // better late than never
168
newThread.setTarget(target);
169
170     ElementInfo ei = env.getElementInfo(target);
171     ClassInfo ci = ei.getClassInfo();
172     MethodInfo run = ci.getMethod("run()", true);
173
174     newThread.pushFrame(new StackFrame(run, false, target));
175     newThread.setStatus(run.isSynchronized
176                         ? ThreadInfo.SYNC_RUNNING : ThreadInfo.RUNNING);
177
178     // <2do> now that we have another runnable, we should re-compute
179
// reachability so that subsequent potential breaks work correctly
180
if (newThread.usePor()){ // means we use on-the-fly POR
181
//env.getSystemState().activateGC();
182
env.getDynamicArea().analyzeHeap(false); // sledgehammer mark
183
}
184   }
185
186   public static void stop0 (MJIEnv env, int objref, int threadDeathExcptRef) {
187     // that's very very very deprecated. I know of almost no kernel
188
// thread system with user-stoppable threads
189
}
190
191   public static void suspend0 (MJIEnv env, int objref) {
192     // again, deprecated
193
}
194
195   public static void yield (MJIEnv env, int clsObjRef) {
196     // do nothing, we are scheduling relevant, so we should get all
197
// possible scheduling combinations
198
}
199
200   // it's synchronized
201
/*
202   public static void join__ (MJIEnv env, int objref) {
203     ThreadInfo ti = getThreadInfo(env,objref);
204     
205     if (ti.isAlive()) {
206       env.wait(objref);
207     }
208   }
209    */

210   
211   private static ThreadInfo getThreadInfo (MJIEnv env, int objref) {
212     KernelState ks = env.getKernelState();
213
214     return ks.tl.locate(objref);
215   }
216 }
217
Popular Tags