1 package DEOS; 20 21 import gov.nasa.jpf.jvm.Verify; 22 23 24 27 class Thread { 28 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 itsName; 58 DEOSThread body; 59 60 public Thread (String name) { 61 itsName = name; 63 64 timeoutNode = new threadListNode(this); 65 startOfPeriodWaitNode = new threadListNode(this); 66 ; 67 preemptionNode = new threadListNode(this); 68 69 70 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 itsBudget.setTotalBudgetInUsec(b); 92 } 93 94 public void setCurrentPriority (int p) { 95 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 waitForNextTriggeringEvent(); 128 itsLastCompletion = itsPeriodicEvent.currentPeriod(); 129 130 } 132 133 public void cpuAllowanceExceeded () { 134 if (this == Scheduler.idleThread()) { 135 startChargingCPUTime(); 137 138 } 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 Thread 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 Scheduler.scheduleOtherThread(); 170 } 171 172 public int periodIndex () { 173 return itsPeriodIndex; 174 } 175 176 public void startChargingCPUTime () { 177 int cp = itsPeriodicEvent.currentPeriod(); 180 int budget; 181 182 if (isIdle()) { 184 budget = itsCurrentBudget.totalBudgetInUsec(); 185 } else { 186 if (cp == itsLastExecution) { 187 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 Assertion.check(); 202 203 itsCurrentBudget.startTimer(); 204 } 205 206 public void startThread (int theCPUBudget) { 207 itsCurrentPriority = Scheduler.priorityForPeriodIndex(itsPeriodIndex); 209 itsBudget.setTotalBudgetInUsec(theCPUBudget); 210 startThreadInternal(); 211 itsLastCompletion = itsPeriodicEvent.currentPeriod() - 1; 212 waitForNextTriggeringEvent(); 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 int remainingTime = bonus + 229 DEOS.theTimer.getRemainingTime( 230 DEOS.systemClock.getCurrentTime()); 231 itsCurrentBudget.setRemainingBudgetInUsec(remainingTime); 232 233 } 236 237 public void stopThread () { 238 itsLastCompletion = itsPeriodicEvent.currentPeriod(); 240 itsCreationStatus = threadStatusDormant; 241 Assertion.removeThread(this); 242 243 } 245 246 public String toString () { 247 return itsName; 248 } 249 250 public void waitForNextPeriod () { 251 int interruptState = CPU.enterCritical(); 253 completeForPeriod(); 254 255 256 Scheduler.scheduleOtherThread(); 258 CPU.exitCritical(interruptState); 259 } 260 261 public void waitForNextTriggeringEvent () { 262 itsPeriodicEvent.makeThreadWait(this); 264 } 265 } | Popular Tags |