KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DEOS > Timer


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 Timer {
28   static final int uSecsPeriod = Registry.uSecsInFastestPeriod;
29   static int Start_time = 0; // time 'requested' by thread and 'written to timer'
30
static int Remaining_time = 0;
31   static int Used_time = 0;
32
33   // JAVA DEOS these indicate that this _could_ happen, not that it will
34
static boolean tick = false;
35   static boolean timer = false;
36
37   public Timer () {
38   }
39
40   public static void clearInterrupts () {
41     timer = false;
42     tick = false;
43   }
44
45   public int timeRemaining () {
46     //Verify.beginAtomic();
47
int used_in_period = 0; // how much time did thread use
48
int time_to_eop = uSecsPeriod - Used_time; // time left in period
49

50     // if tick and timer are still set, then you know they happended -
51
// they are (should be) cleared by the threads otherwise
52
if (tick) { // used all the time to eop OR no time
53
used_in_period = time_to_eop;
54
55       //System.out.println(" system tick interrupt");
56
} else if (timer) { // used all the time OR no time
57
used_in_period = Start_time;
58
59       //System.out.println(" timer interrupt");
60
} else if (time_to_eop <= Start_time) {
61       DEOS.inc();
62
63       if (!Verify.randomBool()) { // used all the time to eop OR no time
64
used_in_period = time_to_eop;
65
66         //DEOS.println("going to end of period");
67
} else {
68         used_in_period = 0;
69
70         //DEOS.println("going no where");
71
}
72     } else { // time_to_eop > Start_time, i.e. use Start_time for calculations
73
DEOS.inc();
74
75       if (Verify.randomBool()) {
76         used_in_period = Start_time;
77
78         //DEOS.println("using full budget");
79
} else {
80         used_in_period = 0;
81
82         //DEOS.println("using no budget");
83
}
84     }
85
86     Used_time += used_in_period;
87
88     if (tick) {
89       Used_time = 0; // this is to help the invariant...
90
}
91
92     clearInterrupts();
93
94     Remaining_time = Start_time - used_in_period;
95
96     //DEOS.println("thread: " + used_in_period + " used, " +
97
// Remaining_time + " remaining. total period usage: " + Used_time);
98
// Verify.endAtomic();
99
//assert (Remaining_time >= 0);
100
// }
101
//System.out.println("Timer.timeRemaining " + Remaining_time);
102
return Remaining_time;
103   }
104
105   public /*synchronized*/
106    void write (int delayInMicroseconds) {
107     Start_time = delayInMicroseconds;
108
109     //DEOS.println("setting timer with " + Start_time);
110
if ((Start_time + Used_time) >= uSecsPeriod) {
111       tick = true; // tick may happen
112
} else if ((Start_time + Used_time) < uSecsPeriod) {
113       timer = true;
114     } else {
115       System.out.println("Timer ERROR - this case should not happen");
116
117       //assert (true);
118
}
119   }
120 }
Popular Tags