We want to be able to interface with time. To do this we need to gain access to time (using clocks), and then be able to co-ordinate execution with the environment, specifying rates of execution and specifying deadlines. We also need to have the ability to ‘timeout’ – to use time to identify failures in our system; a non-occurrence of some event. We also want to be able to satisfy timing requirements which will be covered later (next term; validation).
Access to a Clock
There are 2 main ways of having access to a clock.
- By having direct access to the environment’s time frame (e.g. GPS). This is becoming more and more popular as GPS gets cheaper and cheaper.
- By using an internal hardware clock that gives an adequate approximation to the passage of time in the environment. You can always adjust it for drift over the course of a long period of time.
Ada
You have a Calendar
package, and a Duration
type (which is a fixed point type). That type has a 'Small
parameter which must not be greater than 20 milliseconds; it’s the smallest possible granularity. Ideally it’s a lot lower than this.
In Ada95 there’s an additional Real_Time
package which doesn’t deal with ‘leap ticks’ (e.g. DST, leap seconds, leap years) and is intended to give a finer granularity.
In Real_Time
you have both Time
and Time_Span
. Here’s an example of using Real_Time
instead of Time
:
declare
use Ada.Real_Time;
Start, Finish : Time;
Interval : Time_Span := To_Time_Span(1.7);
begin
Start := Clock;
-- sequence of statements
Finish := Clock;
if Finish - Start > Interval then
raise Time_Error; -- a user-defined exception
end if;
end;
Though note there’s a big issue with this; if the sequence of statements never finish you’ll never even get to raise an error. You can use ATC to solve this (which we have covered).
Java
Similar to Ada. In Java itself you just get Java.lang.System.currentTimeMillis
which returns the number of milliseconds since 1/1/1970 GMT. RTSJ is a bit more useful.
There’s a class called HighResolutionTime
which is more useful, deals with milliseconds and nanoseconds. However We don’t use this, it’s just an abstraction; Instead you use AbsoluteTime
or RelativeTime
which extend HighResolutionTime
. Here’s an example (the same as the Ada one but in Java really):
{
AbsoluteTime oldTime, newTime;
RelativeTime interval;
Clock clock = Clock.getRealtimeClock();
oldTime = clock.getTime();
// other computations
newTime = clock.getTime();
interval = newTime.subtract(oldTime);
}
POSIX and C
This isn’t so important as we primarily deal with Ada and Java in this module. But ANSI C has a standard library for interfacing to “calendar” time which has a time type and lots of routines etc. Real-time POSIX requires at least one clock of minimum resolution 50 Hz (20ms).
Summary
-
We want to be able to access clocks.
-
We want to be able to delay (a thread).
-
We want to be able to recognise timeouts.
-
Ada has two clocks; calendar time and real-time.
-
RTSJ has the wall clock and the real-time clock.