KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DEOS > DEOSKernel


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 /**
22  * DOCUMENT ME!
23  */

24 class DEOSKernel {
25   //typedef enum
26
//{
27
public static int threadSuccess = 0;
28   public static int threadInvalidHandle = 1;
29   public static int threadInvalidInterrupt = 2;
30   public static int threadNotDormant = 3;
31   public static int threadNotSchedulable = 4;
32   public static int threadInsufficientPrivilege = 5;
33   public static int threadNotDynamic = 6;
34   public static int threadNotStatic = 7;
35   public static int threadMaximumThreadsExceeded = 8;
36   public static int threadInsufficientRAMForStack = 9;
37   public static int threadNoSuchThread = 10;
38   public static int threadInvalidTemplate = 11;
39   public static int threadNotActive = 12;
40   public static int threadInScheduleBefore = 13;
41   public static int threadInsufficientBudget = 14;
42   public static int threadDuplicateISR = 15;
43   public static int threadInvalidFromDynamicProcess = 16;
44   public static int threadPrimaryCannotBeISR = 17;
45
46   static void coldStartKernel () {
47     //System.out.println("DEOSKernel.coldStartKernel");
48
// Must be done before Scheduler.
49
StartOfPeriodEvent.initialize();
50
51
52     // Must be done after System.
53
// Scheduler initialize doesn't return unless we get a shutdown()
54
Scheduler.initialize();
55
56     // System.out.println("DEOSKernel: Finished Initialization");
57
}
58
59   static int createThreadK (String JavaDoc name, int threadTemplateId, int threadBudget,
60                             int periodIndex) {
61     //System.out.println("API: createThreadK Period " + periodIndex +
62
// " Budget " + threadBudget );
63
int returnStatus;
64     DEOSProcess currentProcess = Scheduler.currentProcess();
65
66     // Allocate a thread, then initialize it
67
Thread JavaDoc threadCreated = new Thread JavaDoc(name);
68
69     if (threadCreated == null) {
70       System.out.println("Thread could not be created");
71       returnStatus = threadMaximumThreadsExceeded;
72     } else {
73       // Allocate stack and initialize the thread...
74
if (!threadCreated.ConceptualObjectConstructor(periodIndex)) {
75         threadCreated = null;
76         returnStatus = threadInsufficientRAMForStack;
77       } else {
78         int interruptState = CPU.enterCritical();
79         returnStatus = localStartThread(threadCreated, threadBudget,
80                                         periodIndex);
81         CPU.exitCritical(interruptState);
82
83         if (threadSuccess == returnStatus) {
84         } else {
85           threadCreated.ConceptualObjectDestructor();
86           threadCreated = null;
87         }
88       }
89     }
90
91     return returnStatus;
92   }
93
94   static int deleteThreadK (Thread JavaDoc theThread) {
95     //System.out.println(theThread + " Made it into deleteThread ");
96
if (theThread != Scheduler.currentThread()) {
97       System.out.println("Thread " + theThread + " no longer running delete");
98
99       return 0;
100     }
101
102     int result;
103     int interruptState = CPU.enterCritical();
104
105     CPU.exitCritical(interruptState);
106     theThread.initiateStopAndDelete();
107     result = threadSuccess;
108     interruptState = CPU.enterCritical();
109     CPU.exitCritical(interruptState);
110
111     return result;
112   }
113
114   static int localStartThread (Thread JavaDoc theThread, int requestedThreadBudget,
115                                int periodIndex) {
116     // changed the followign code because can't pass int (budget) by reference.
117
// cpuTimeInMicroseconds budget; // budget set by following call.
118
int budget; // budget set by following call.
119

120
121     //System.out.println("DEOSKernel.localStartThread");
122
budget = Scheduler.currentProcess()
123                       .allocateCPUBudgetForThread(theThread,
124                                                   requestedThreadBudget,
125                                                   periodIndex);
126
127     if (budget > -1) {
128       theThread.startThread(budget);
129
130       return threadSuccess;
131     } else {
132       return threadNotSchedulable;
133     }
134   }
135
136   static int waitUntilNextPeriodK (Thread JavaDoc th) {
137     // System.out.println(th + " Made it into WaitUntil..." + Scheduler.currentThread());
138
if (th != Scheduler.currentThread()) {
139       System.out.println("Thread " + th + " no longer running");
140
141       return 0;
142     }
143
144
145     //DEOS.handler.resetTimerInterrupt();
146
Scheduler.currentThread().waitForNextPeriod();
147
148     return 0; // void really
149
}
150
151   //} threadStatus;
152
}
Popular Tags