1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . O S _ P R I M I T I V E S --
10 -- Copyright (C) 1998-2001 Free Software Foundation, Inc. --
12 -- GNARL is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNARL; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNARL was developed by the GNARL team at Florida State University. It is --
31 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
32 -- State University (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 -- This is the OS/2 version of this package
38 with Interfaces
.C
; use Interfaces
.C
;
39 with Interfaces
.OS2Lib
; use Interfaces
.OS2Lib
;
40 with Interfaces
.OS2Lib
.Synchronization
; use Interfaces
.OS2Lib
.Synchronization
;
42 package body System
.OS_Primitives
is
48 Epoch_Offset
: Duration; -- See Set_Epoch_Offset
49 Max_Tick_Count
: QWORD
:= 0.0;
50 -- This is needed to compensate for small glitches in the
51 -- hardware clock or the way it is read by the OS
53 -----------------------
54 -- Local Subprograms --
55 -----------------------
57 procedure Set_Epoch_Offset
;
58 -- Initializes the Epoch_1970_Offset to the offset of the System_Clock
59 -- relative to the Unix epoch (Jan 1, 1970), such that
60 -- Clock = System_Clock + Epoch_1970_Offset
62 function System_Clock
return Duration;
63 pragma Inline
(System_Clock
);
64 -- Function returning value of system clock with system-dependent timebase.
65 -- For OS/2 the system clock returns the elapsed time since system boot.
66 -- The clock resolution is approximately 838 ns.
72 function System_Clock
return Duration is
74 -- Implement conversion from tick count to Duration
75 -- using fixed point arithmetic. The frequency of
76 -- the Intel 8254 timer chip is 18.2 * 2**16 Hz.
78 Tick_Duration
: constant := 1.0 / (18.2 * 2**16);
79 Tick_Count
: aliased QWORD
;
82 Must_Not_Fail
(DosTmrQueryTime
(Tick_Count
'Access));
83 -- Read nr of clock ticks since boot time
85 Max_Tick_Count
:= QWORD
'Max (Tick_Count
, Max_Tick_Count
);
87 return Max_Tick_Count
* Tick_Duration
;
94 function Clock
return Duration is
96 return System_Clock
+ Epoch_Offset
;
100 -- Monotonic_Clock --
101 ---------------------
103 function Monotonic_Clock
return Duration renames Clock
;
105 ----------------------
106 -- Set_Epoch_Offset --
107 ----------------------
109 procedure Set_Epoch_Offset
is
111 -- Interface to Unix C style gettimeofday
113 type timeval
is record
118 procedure gettimeofday
119 (time
: access timeval
;
120 zone
: System
.Address
:= System
.Address
'Null_Parameter);
121 pragma Import
(C
, gettimeofday
);
123 Time_Of_Day
: aliased timeval
;
124 Micro_To_Nano
: constant := 1.0E3
;
125 Sec_To_Nano
: constant := 1.0E9
;
126 Nanos_Since_Epoch
: QWORD
;
129 gettimeofday
(Time_Of_Day
'Access);
130 Nanos_Since_Epoch
:= QWORD
(Time_Of_Day
.tv_sec
) * Sec_To_Nano
131 + QWORD
(Time_Of_Day
.tv_usec
) * Micro_To_Nano
;
134 Duration'(Nanos_Since_Epoch / Sec_To_Nano) - System_Clock;
136 end Set_Epoch_Offset;
142 procedure Timed_Delay
148 Check_Time : Duration := Clock;
151 if Mode = Relative then
153 Abs_Time := Time + Check_Time;
155 Rel_Time := Time - Check_Time;
159 if Rel_Time > 0.0 then
161 Must_Not_Fail (DosSleep (ULONG (Rel_Time * 1000.0)));
165 exit when Abs_Time <= Check_Time;
167 Rel_Time := Abs_Time - Check_Time;
174 end System.OS_Primitives;