KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DEOS > Scheduler


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 Scheduler {
28   static Thread JavaDoc itsRunningThread;
29   static Thread JavaDoc itsIdleThread;
30   static PriorityListOfThreads itsRunnableList;
31
32   //static int systemTickCount = 0xFFFFFFFF ;
33
static DEOSProcess theProcess;
34   static SynchObject synch;
35
36   public static DEOSProcess currentProcess () {
37     return theProcess;
38   }
39
40   public static Thread JavaDoc currentThread () {
41     return itsRunningThread;
42   }
43
44   public static Thread JavaDoc idleThread () {
45     return itsIdleThread;
46   }
47
48   public static void initialize () {
49     // JAVA DEOS
50
synch = new SynchObject();
51
52
53     //System.out.println("Scheduler.initialize");
54
itsRunnableList = new PriorityListOfThreads();
55     initializeIdleProcess();
56
57     int interruptState = CPU.enterCritical();
58     itsRunningThread = itsIdleThread;
59     itsRunningThread.startChargingCPUTime();
60     CPU.exitCritical(interruptState);
61     theProcess = new DEOSProcess();
62     DEOSKernel.localStartThread(theProcess.mainThread(),
63                                 Registry.uSecsInFastestPeriod, 0);
64   }
65
66   public static int priorityForPeriodIndex (int thePeriodIndex) {
67     return Registry.numPeriods - thePeriodIndex;
68   }
69
70   public static PriorityListOfThreads runnableList () {
71     return itsRunnableList;
72   }
73
74   public static void scheduleAnyThread () {
75     //System.out.println("Scheduler.scheduleAnyThread: current = " +
76
// itsRunningThread);
77
if (!itsRunnableList.isEmpty()) {
78       if (itsRunningThread.currentPriority() < itsRunnableList.head().parent()
79                                                               .currentPriority()) {
80         //System.out.println("Preemption");
81
itsRunnableList.addAtBeginning(itsRunningThread.preemptionNode);
82         Scheduler.scheduleOtherThread();
83       } else {
84         //System.out.println("Running has Higher Priority, i.e. no Preemption");
85
itsRunningThread.stopChargingCPUTime(0);
86         itsRunningThread.startChargingCPUTime();
87       }
88     } else {
89       System.out.println("How can this be!!!");
90     }
91   }
92
93   public static void scheduleOtherThread () {
94     Thread JavaDoc newThread;
95     Thread JavaDoc fromThread = itsRunningThread;
96
97     // Verify.beginAtomic();
98
//System.out.println("Scheduler.scheduleOtherThread");
99
threadListNode runnableThreadListNode = itsRunnableList.head();
100     Thread JavaDoc runnableThread = runnableThreadListNode.parent();
101     newThread = runnableThread;
102     runnableThreadListNode.removeFromList();
103
104
105     // Verify.endAtomic();
106
fromThread.stopChargingCPUTime(0);
107
108
109     //Verify.endAtomic();
110
newThread.startChargingCPUTime(); // cannot add this to atomic, since
111

112
113     // it can block
114
// JAVA DEOS - All the threads wait on the synch object. To wake up
115
// the newThread we interrupt() it. Since it was waiting on the
116
// synch object and the fromThread has the lock on the synch object,
117
// the new thread won't run until we call wait(). This is intended
118
// to only allow on thread to be running at a time like in the real
119
// DEOS. - penix
120
itsRunningThread = newThread;
121
122     //System.out.println(fromThread + " waking up " + newThread);
123
// check to make sure the current thread is not also the new
124
// thread or no one will get interrupted...
125
//if (fromThread != newThread) {
126
// newThread.javaThread().interrupt();
127
// try{synch.wait();}
128
// catch(InterruptedException ex) {
129
// //System.out.println("Waking up again");
130
// };
131
//}
132
}
133
134   //private
135
static void handleSystemTickInterrupt () {
136     //System.out.println("Scheduler.handleSystemTickInterrupt");
137
StartOfPeriodEvent.eventForPeriodIndex(0).pulseEvent(0);
138     Scheduler.scheduleAnyThread();
139
140     // System.out.println("Scheduler.handleSystemTickInterrupt RETURN");
141
}
142
143   //private
144
static void handleTimerInterrupt () {
145     //System.out.println("Scheduler.handleTimerInterrupt");
146
itsRunningThread.cpuAllowanceExceeded();
147   }
148
149   private static void idleThreadMain () {
150     while (true) {
151     }
152   }
153
154   private static void initializeIdleProcess () {
155     //System.out.println("Scheduler.initializeIdleProcess");
156
itsIdleThread = new Thread JavaDoc("idle");
157
158
159     // %%%%%5 added this code to make it work see sched.spin file
160
itsIdleThread.ConceptualObjectConstructor(0);
161     itsIdleThread.setCurrentPriority(0);
162     itsIdleThread.waitForNextTriggeringEvent();
163
164     itsIdleThread.startOfPeriodWaitNode.removeFromList();
165
166
167     //SPIN Registry::periodDurationInMicroSecs(itsIdleThread->periodIndex())
168
itsIdleThread.setCPUBudget(Registry.periodDurationInMicroSecs(
169                                      itsIdleThread.periodIndex()));
170     itsIdleThread.budget().replenish();
171   }
172 }
Popular Tags