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 --
11 -- Copyright (C) 1998-2001 Free Software Foundation, Inc. --
13 -- GNARL is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNARL; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNARL was developed by the GNARL team at Florida State University. It is --
32 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
33 -- State University (http://www.gnat.com). --
35 ------------------------------------------------------------------------------
37 -- This is the OS/2 version of this package
39 with Interfaces
.C
; use Interfaces
.C
;
40 with Interfaces
.OS2Lib
; use Interfaces
.OS2Lib
;
41 with Interfaces
.OS2Lib
.Synchronization
; use Interfaces
.OS2Lib
.Synchronization
;
43 package body System
.OS_Primitives
is
49 Epoch_Offset
: Duration; -- See Set_Epoch_Offset
50 Max_Tick_Count
: QWORD
:= 0.0;
51 -- This is needed to compensate for small glitches in the
52 -- hardware clock or the way it is read by the OS
54 -----------------------
55 -- Local Subprograms --
56 -----------------------
58 procedure Set_Epoch_Offset
;
59 -- Initializes the Epoch_1970_Offset to the offset of the System_Clock
60 -- relative to the Unix epoch (Jan 1, 1970), such that
61 -- Clock = System_Clock + Epoch_1970_Offset
63 function System_Clock
return Duration;
64 pragma Inline
(System_Clock
);
65 -- Function returning value of system clock with system-dependent timebase.
66 -- For OS/2 the system clock returns the elapsed time since system boot.
67 -- The clock resolution is approximately 838 ns.
73 function System_Clock
return Duration is
75 -- Implement conversion from tick count to Duration
76 -- using fixed point arithmetic. The frequency of
77 -- the Intel 8254 timer chip is 18.2 * 2**16 Hz.
79 Tick_Duration
: constant := 1.0 / (18.2 * 2**16);
80 Tick_Count
: aliased QWORD
;
83 Must_Not_Fail
(DosTmrQueryTime
(Tick_Count
'Access));
84 -- Read nr of clock ticks since boot time
86 Max_Tick_Count
:= QWORD
'Max (Tick_Count
, Max_Tick_Count
);
88 return Max_Tick_Count
* Tick_Duration
;
95 function Clock
return Duration is
97 return System_Clock
+ Epoch_Offset
;
100 ---------------------
101 -- Monotonic_Clock --
102 ---------------------
104 function Monotonic_Clock
return Duration renames Clock
;
106 ----------------------
107 -- Set_Epoch_Offset --
108 ----------------------
110 procedure Set_Epoch_Offset
is
112 -- Interface to Unix C style gettimeofday
114 type timeval
is record
119 procedure gettimeofday
120 (time
: access timeval
;
121 zone
: System
.Address
:= System
.Address
'Null_Parameter);
122 pragma Import
(C
, gettimeofday
);
124 Time_Of_Day
: aliased timeval
;
125 Micro_To_Nano
: constant := 1.0E3
;
126 Sec_To_Nano
: constant := 1.0E9
;
127 Nanos_Since_Epoch
: QWORD
;
130 gettimeofday
(Time_Of_Day
'Access);
131 Nanos_Since_Epoch
:= QWORD
(Time_Of_Day
.tv_sec
) * Sec_To_Nano
132 + QWORD
(Time_Of_Day
.tv_usec
) * Micro_To_Nano
;
135 Duration'(Nanos_Since_Epoch / Sec_To_Nano) - System_Clock;
137 end Set_Epoch_Offset;
143 procedure Timed_Delay
149 Check_Time : Duration := Clock;
152 if Mode = Relative then
154 Abs_Time := Time + Check_Time;
156 Rel_Time := Time - Check_Time;
160 if Rel_Time > 0.0 then
162 Must_Not_Fail (DosSleep (ULONG (Rel_Time * 1000.0)));
166 exit when Abs_Time <= Check_Time;
168 Rel_Time := Abs_Time - Check_Time;
175 end System.OS_Primitives;