KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DEOS > 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 DEOS;
20
21 import gov.nasa.jpf.jvm.Verify;
22
23
24 /**
25  * DOCUMENT ME!
26  */

27 class Thread {
28   /*
29      static void stopAndDeleteThreadKernelExceptionHandler() {
30        Thread theThread = Scheduler.currentThread();
31        DEOSProcess ownerProcess = Scheduler.currentProcess();
32      
33        int interruptState = CPU.enterCritical();
34        theThread.stopThread();
35        theThread.itsCreationStatus = threadStatusNotCreated;
36        ownerProcess.deallocateCPUBudgetForThread( theThread );
37        theThread = null;
38        Scheduler.scheduleOtherThread();
39        CPU.exitCritical( interruptState );
40      }
41    */

42   static final int threadStatusNotCreated = 0;
43   static final int threadStatusDormant = 1;
44   static final int threadStatusActive = 2;
45   static final int threadStatusKernelExceptionPending = 3;
46   threadListNode timeoutNode;
47   threadListNode startOfPeriodWaitNode;
48   threadListNode preemptionNode;
49   Budget itsBudget;
50   Budget itsCurrentBudget;
51   int itsLastExecution;
52   int itsLastCompletion;
53   StartOfPeriodEvent itsPeriodicEvent;
54   int itsPeriodIndex;
55   int itsCurrentPriority;
56   int itsCreationStatus;
57   String JavaDoc itsName;
58   DEOSThread body;
59
60   public Thread (String JavaDoc name) {
61     //System.out.println("Thread Constructor");
62
itsName = name;
63
64     timeoutNode = new threadListNode(this);
65     startOfPeriodWaitNode = new threadListNode(this);
66     ;
67     preemptionNode = new threadListNode(this);
68
69
70     // %%%%%% added this see athread.spin
71
itsBudget = new Budget();
72     itsCreationStatus = threadStatusNotCreated;
73
74     Assertion.addThread(this);
75
76     if (name.equals("main")) {
77       body = new DEOSMainThread(this);
78     } else if (name.equals("idle")) {
79       body = new DEOSIdleThread(this);
80     } else {
81       body = new DEOSThread(this);
82     }
83   }
84
85   public DEOSThread getBody () {
86     return body;
87   }
88
89   public void setCPUBudget (int b) {
90     //System.out.println("Thread.setCPUBudget " + b);
91
itsBudget.setTotalBudgetInUsec(b);
92   }
93
94   public void setCurrentPriority (int p) {
95     //System.out.println("Thread.setCurrentPriority " + p);
96
itsCurrentPriority = p;
97   }
98
99   public boolean isIdle () {
100     return itsName.equals("idle");
101   }
102
103   public boolean isMain () {
104     return itsName.equals("main");
105   }
106
107   public boolean ConceptualObjectConstructor (int period) {
108     itsPeriodIndex = period;
109     itsCurrentPriority = Scheduler.priorityForPeriodIndex(itsPeriodIndex);
110     itsPeriodicEvent = StartOfPeriodEvent.eventForPeriodIndex(itsPeriodIndex);
111     itsCurrentBudget = itsBudget;
112     itsCreationStatus = threadStatusDormant;
113
114     return true;
115   }
116
117   public void ConceptualObjectDestructor () {
118     itsCreationStatus = threadStatusNotCreated;
119   }
120
121   public Budget budget () {
122     return itsBudget;
123   }
124
125   public void completeForPeriod () {
126     //Verify.beginAtomic();
127
waitForNextTriggeringEvent();
128     itsLastCompletion = itsPeriodicEvent.currentPeriod();
129
130     //Verify.endAtomic();
131
}
132
133   public void cpuAllowanceExceeded () {
134     if (this == Scheduler.idleThread()) {
135       //System.out.println("CPUAllowance Exceeded, but it is idle thread");
136
startChargingCPUTime();
137
138       //System.out.println("after");
139
} else {
140       waitForNextPeriod();
141     }
142   }
143
144   public int cpuBudget () {
145     return itsBudget.totalBudgetInUsec();
146   }
147
148   public int currentPriority () {
149     return itsCurrentPriority;
150   }
151
152   public void initiateStopAndDelete () {
153     // Verify.beginAtomic();
154
Thread JavaDoc current = Scheduler.currentThread();
155
156     if (current != this) {
157       System.out.println("Current running thread (" + current +
158                          ") != thread trying to delete itself!" + this);
159
160       return;
161     }
162
163     current.stopThread();
164     current.itsCreationStatus = threadStatusNotCreated;
165     DEOSProcess.deallocateCPUBudgetForThread(current);
166
167
168     // Verify.endAtomic();
169
Scheduler.scheduleOtherThread();
170   }
171
172   public int periodIndex () {
173     return itsPeriodIndex;
174   }
175
176   public void startChargingCPUTime () {
177     //Verify.beginAtomic();
178
//System.out.println("Thread.startChargingCPUTime");
179
int cp = itsPeriodicEvent.currentPeriod();
180     int budget;
181
182     // added by ckong - July 3, 2001
183
if (isIdle()) {
184       budget = itsCurrentBudget.totalBudgetInUsec();
185     } else {
186       if (cp == itsLastExecution) {
187         //sop.currentId == this.itsLastExecution
188
budget = itsCurrentBudget.remainingBudgetInUsec();
189       } else {
190         budget = itsCurrentBudget.totalBudgetInUsec();
191         itsLastExecution = cp;
192
193         int remainingTime = itsCurrentBudget.remainingBudgetInUsec();
194       }
195     }
196
197     itsCurrentBudget.setRemainingBudgetInUsec(budget);
198
199
200     // Verify.endAtomic();
201
Assertion.check();
202
203     itsCurrentBudget.startTimer();
204   }
205
206   public void startThread (int theCPUBudget) {
207     //System.out.println("Thread.StartThread");
208
itsCurrentPriority = Scheduler.priorityForPeriodIndex(itsPeriodIndex);
209     itsBudget.setTotalBudgetInUsec(theCPUBudget);
210     startThreadInternal();
211     itsLastCompletion = itsPeriodicEvent.currentPeriod() - 1;
212     waitForNextTriggeringEvent(); // assumes critical!
213
itsLastExecution = itsPeriodicEvent.currentPeriod();
214     itsLastCompletion = itsPeriodicEvent.currentPeriod();
215   }
216
217   public void startThreadInternal () {
218     itsCreationStatus = threadStatusActive;
219     itsBudget.setRemainingBudgetInUsec(itsBudget.totalBudgetInUsec());
220     itsCurrentBudget = itsBudget;
221   }
222
223   public void stopChargingCPUTime (int bonus) {
224     //Verify.beginAtomic();
225
//System.out.println("Thread.stopChargingCPUTime");
226
// Modified by ckong - June 25, 2001
227
//int remainingTime = bonus + DEOS.theTimer.timeRemaining();
228
int remainingTime = bonus +
229                         DEOS.theTimer.getRemainingTime(
230                               DEOS.systemClock.getCurrentTime());
231     itsCurrentBudget.setRemainingBudgetInUsec(remainingTime);
232
233     //body.stopThread();
234
//Verify.endAtomic();
235
}
236
237   public void stopThread () {
238     // Verify.beginAtomic();
239
itsLastCompletion = itsPeriodicEvent.currentPeriod();
240     itsCreationStatus = threadStatusDormant;
241     Assertion.removeThread(this);
242
243     // Verify.endAtomic();
244
}
245
246   public String JavaDoc toString () {
247     return itsName;
248   }
249
250   public void waitForNextPeriod () {
251     // Verify.beginAtomic();
252
int interruptState = CPU.enterCritical();
253     completeForPeriod();
254
255
256     // Verify.endAtomic();
257
Scheduler.scheduleOtherThread();
258     CPU.exitCritical(interruptState);
259   }
260
261   public void waitForNextTriggeringEvent () {
262     //System.out.println("Thread.waitForNextTriggeringEvent");
263
itsPeriodicEvent.makeThreadWait(this);
264   }
265 }
Popular Tags