next up previous contents index
Next: GNAT Implementation Up: Time and Clocks Previous: Time and Clocks   Contents   Index

Subsections

Ada Time and Clocks

Ada provides access to the clock by providing two packages. The main section of the Ada Reference Manual defines a compulsory library package called Ada.Calendar that provides an abstraction for ``wall clock'' time that recognizes leap years, leap seconds and other adjustments. In Real-Time Systems Annex, a second representation is given that defines a monotonic (that is, non-decreasing) regular clock (package Ada.Real_Time). Both these representations should map down to the same hardware clock but cater for different application needs [BW98, section 2.5].


Ada.Calendar

The interface of this package is as follows [AAR95, section 9.6]:

package Ada.Calendar is

   type Time is private;

   subtype Year_Number  is Integer range 1901 .. 2099;
   subtype Month_Number is Integer range 1 .. 12;
   subtype Day_Number   is Integer range 1 .. 31;

   subtype Day_Duration is Duration range 0.0 .. 86_400.0;

   function Clock return Time;

   function Year    (Date : Time) return Year_Number;
   function Month   (Date : Time) return Month_Number;
   function Day     (Date : Time) return Day_Number;
   function Seconds (Date : Time) return Day_Duration;

   procedure Split
     (Date    : Time;
      Year    : out Year_Number;
      Month   : out Month_Number;
      Day     : out Day_Number;
      Seconds : out Day_Duration);

   function Time_Of
     (Year    : Year_Number;
      Month   : Month_Number;
      Day     : Day_Number;
      Seconds : Day_Duration := 0.0)
      return    Time;

   function "+" (Left : Time;     Right : Duration) return Time;
   function "+" (Left : Duration; Right : Time)     return Time;
   function "-" (Left : Time;     Right : Duration) return Time;
   function "-" (Left : Time;     Right : Time)     return Duration;

   function "<"  (Left, Right : Time) return Boolean;
   function "<=" (Left, Right : Time) return Boolean;
   function ">"  (Left, Right : Time) return Boolean;
   function ">=" (Left, Right : Time) return Boolean;

   Time_Error : exception;

private
   . . .
end Ada.Calendar;

The current time is returned by the function Clock. Conversion between Time and program accessible types is provided by subprograms Split and Time_Of. in addition some arithmetic and boolean operations are specified [BW98, section 2.5].


Ada.Real_Time

The interface of this package is as follows [AAR95, section D.8]:

package Ada.Real_Time is

   type Time is private;
   Time_First : constant Time;
   Time_Last  : constant Time;
   Time_Unit  : constant := implementation-defined-real-number;

   type Time_Span is private;
   Time_Span_First : constant Time_Span;
   Time_Span_Last  :  constant Time_Span;
   Time_Span_Zero  :  constant Time_Span;
   Time_Span_Unit  :  constant Time_Span;

   Tick : constant Time_Span;
   function Clock return Time;

   function "+"  (Left : Time;      Right : Time_Span) return Time;
   function "+"  (Left : Time_Span; Right : Time)      return Time;
   function "-"  (Left : Time;      Right : Time_Span) return Time;
   function "-"  (Left : Time;      Right : Time)      return Time_Span;

   function "<"  (Left, Right : Time) return Boolean;
   function "<=" (Left, Right : Time) return Boolean;
   function ">"  (Left, Right : Time) return Boolean;
   function ">=" (Left, Right : Time) return Boolean;

   function "+"  (Left, Right : Time_Span)             return Time_Span;
   function "-"  (Left, Right : Time_Span)             return Time_Span;
   function "-"  (Right : Time_Span)                   return Time_Span;
   function "*"  (Left : Time_Span; Right : Integer)   return Time_Span;
   function "*"  (Left : Integer;   Right : Time_Span) return Time_Span;
   function "/"  (Left, Right : Time_Span)             return Integer;
   function "/"  (Left : Time_Span; Right : Integer)   return Time_Span;

   function "abs" (Right : Time_Span) return Time_Span;

   function "<"  (Left, Right : Time_Span) return Boolean;
   function "<=" (Left, Right : Time_Span) return Boolean;
   function ">"  (Left, Right : Time_Span) return Boolean;
   function ">=" (Left, Right : Time_Span) return Boolean;

   function To_Duration  (TS : Time_Span) return Duration;
   function To_Time_Span (D : Duration)   return Time_Span;

   function Nanoseconds  (NS : Integer) return Time_Span;
   function Microseconds (US : Integer) return Time_Span;
   function Milliseconds (MS : Integer) return Time_Span;

   type Seconds_Count is new Integer range -Integer'Last .. Integer'Last;

   procedure Split (T : Time; SC : out Seconds_Count; TS : out Time_Span);
   function Time_Of (SC : Seconds_Count; TS : Time_Span) return Time;

private
   ...
end Ada.Real_Time;


Delay Statement

Ada tasks are able to delay their execution for a period of time. This enables the task to be queued on some future event rather than busy-wait o calls to the clock function. The expression following the delay must yield the value of the predefined Ada type Duration. It is important to appreciate that this sentence is an approximate time construct which indicates that the task will be delayed by at least the amount specified [BW98, section 2.5.1].

   delay_relative_statement ::= delay delay_expression

If a delay to an absolute time is required, then the delay until statement should be used.

   delay_until_statement ::= delay until delay_expression

As with delays, delay until is accurate only in its lower bound. The task involved will not be released before the current time has reached that specified in the statement but may be released later [BW98, section 2.5.1].


Timed Entry Call

A timed entry call issues an entry call which is canceled if the call is not accepted within the specified period (relative or absolute) [BW98, section 6.9.1]. The syntax is [AAR95, section 9.7.2]:

   timed_entry_call ::=
      select
        entry_call_alternative
      or
        delay_alternative
      end select;

   entry_call_alternative ::=
      entry_call_statement [sequence_of_statements]


Timed Selective Wait

Often it is the case that a server task cannot unreservedly commit itself to waiting for communication using one or more of its entries. The timed selective wait allows a server task to time-out if an entry call is not received within a certain period of time. The time-out is expressed using the delay statement and can therefore be a relative or an absolute delay. If the relative time expressed is zero or negative, or the absolute time has passed, then the delay alternative is equivalent to having an ``else part'' [BW98, section 2.5.1] (see section [*]). The syntax of the selective accept is [AAR95, section 9.7.1]:

   selective_accept ::=
     select
        [guard]
        select_alternative
   { or
        [guard]
        select_alternative }
   [ else
       sequence_of_statements ]
     end select;

   guard ::= when condition =>

   select_alternative ::=
      accept_alternative
      | delay_alternative
      | terminate_alternative

   accept_alternative ::=
      accept_statement [sequence_of_statements]

   delay_alternative ::=
      delay_statement [sequence_of_statements]

   terminate_alternative ::= terminate;


next up previous contents index
Next: GNAT Implementation Up: Time and Clocks Previous: Time and Clocks   Contents   Index
(c) Javier Miranda. Canary Islands (Spain), 2002. Version 1.0