001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package DEOS;
020:
021: /**
022: * DOCUMENT ME!
023: */
024: class DEOSKernel {
025: //typedef enum
026: //{
027: public static int threadSuccess = 0;
028: public static int threadInvalidHandle = 1;
029: public static int threadInvalidInterrupt = 2;
030: public static int threadNotDormant = 3;
031: public static int threadNotSchedulable = 4;
032: public static int threadInsufficientPrivilege = 5;
033: public static int threadNotDynamic = 6;
034: public static int threadNotStatic = 7;
035: public static int threadMaximumThreadsExceeded = 8;
036: public static int threadInsufficientRAMForStack = 9;
037: public static int threadNoSuchThread = 10;
038: public static int threadInvalidTemplate = 11;
039: public static int threadNotActive = 12;
040: public static int threadInScheduleBefore = 13;
041: public static int threadInsufficientBudget = 14;
042: public static int threadDuplicateISR = 15;
043: public static int threadInvalidFromDynamicProcess = 16;
044: public static int threadPrimaryCannotBeISR = 17;
045:
046: static void coldStartKernel() {
047: //System.out.println("DEOSKernel.coldStartKernel");
048: // Must be done before Scheduler.
049: StartOfPeriodEvent.initialize();
050:
051: // Must be done after System.
052: // Scheduler initialize doesn't return unless we get a shutdown()
053: Scheduler.initialize();
054:
055: // System.out.println("DEOSKernel: Finished Initialization");
056: }
057:
058: static int createThreadK(String name, int threadTemplateId,
059: int threadBudget, int periodIndex) {
060: //System.out.println("API: createThreadK Period " + periodIndex +
061: // " Budget " + threadBudget );
062: int returnStatus;
063: DEOSProcess currentProcess = Scheduler.currentProcess();
064:
065: // Allocate a thread, then initialize it
066: Thread threadCreated = new Thread(name);
067:
068: if (threadCreated == null) {
069: System.out.println("Thread could not be created");
070: returnStatus = threadMaximumThreadsExceeded;
071: } else {
072: // Allocate stack and initialize the thread...
073: if (!threadCreated.ConceptualObjectConstructor(periodIndex)) {
074: threadCreated = null;
075: returnStatus = threadInsufficientRAMForStack;
076: } else {
077: int interruptState = CPU.enterCritical();
078: returnStatus = localStartThread(threadCreated,
079: threadBudget, periodIndex);
080: CPU.exitCritical(interruptState);
081:
082: if (threadSuccess == returnStatus) {
083: } else {
084: threadCreated.ConceptualObjectDestructor();
085: threadCreated = null;
086: }
087: }
088: }
089:
090: return returnStatus;
091: }
092:
093: static int deleteThreadK(Thread theThread) {
094: //System.out.println(theThread + " Made it into deleteThread ");
095: if (theThread != Scheduler.currentThread()) {
096: System.out.println("Thread " + theThread
097: + " no longer running delete");
098:
099: 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 theThread,
115: int requestedThreadBudget, 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: //System.out.println("DEOSKernel.localStartThread");
121: budget = Scheduler.currentProcess().allocateCPUBudgetForThread(
122: theThread, requestedThreadBudget, periodIndex);
123:
124: if (budget > -1) {
125: theThread.startThread(budget);
126:
127: return threadSuccess;
128: } else {
129: return threadNotSchedulable;
130: }
131: }
132:
133: static int waitUntilNextPeriodK(Thread th) {
134: // System.out.println(th + " Made it into WaitUntil..." + Scheduler.currentThread());
135: if (th != Scheduler.currentThread()) {
136: System.out.println("Thread " + th + " no longer running");
137:
138: return 0;
139: }
140:
141: //DEOS.handler.resetTimerInterrupt();
142: Scheduler.currentThread().waitForNextPeriod();
143:
144: return 0; // void really
145: }
146:
147: //} threadStatus;
148: }
|