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. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 ------------------------------------------------------------------------------
35 -- This is the OS/2 version of this package
37 with Interfaces.C; use Interfaces.C;
38 with Interfaces.OS2Lib; use Interfaces.OS2Lib;
39 with Interfaces.OS2Lib.Synchronization; use Interfaces.OS2Lib.Synchronization;
41 package body System.OS_Primitives is
47 Epoch_Offset : Duration; -- See Set_Epoch_Offset
48 Max_Tick_Count : QWORD := 0.0;
49 -- This is needed to compensate for small glitches in the
50 -- hardware clock or the way it is read by the OS
52 -----------------------
53 -- Local Subprograms --
54 -----------------------
56 procedure Set_Epoch_Offset;
57 -- Initializes the Epoch_1970_Offset to the offset of the System_Clock
58 -- relative to the Unix epoch (Jan 1, 1970), such that
59 -- Clock = System_Clock + Epoch_1970_Offset
61 function System_Clock return Duration;
62 pragma Inline (System_Clock);
63 -- Function returning value of system clock with system-dependent timebase.
64 -- For OS/2 the system clock returns the elapsed time since system boot.
65 -- The clock resolution is approximately 838 ns.
71 function System_Clock return Duration is
73 -- Implement conversion from tick count to Duration
74 -- using fixed point arithmetic. The frequency of
75 -- the Intel 8254 timer chip is 18.2 * 2**16 Hz.
77 Tick_Duration : constant := 1.0 / (18.2 * 2**16);
78 Tick_Count : aliased QWORD;
81 Must_Not_Fail (DosTmrQueryTime (Tick_Count'Access));
82 -- Read nr of clock ticks since boot time
84 Max_Tick_Count := QWORD'Max (Tick_Count, Max_Tick_Count);
86 return Max_Tick_Count * Tick_Duration;
93 function Clock return Duration is
95 return System_Clock + Epoch_Offset;
100 ---------------------
102 function Monotonic_Clock return Duration renames Clock;
104 ----------------------
105 -- Set_Epoch_Offset --
106 ----------------------
108 procedure Set_Epoch_Offset is
110 -- Interface to Unix C style gettimeofday
112 type timeval is record
117 procedure gettimeofday
118 (time : access timeval;
119 zone : System.Address := System.Address'Null_Parameter);
120 pragma Import (C, gettimeofday);
122 Time_Of_Day : aliased timeval;
123 Micro_To_Nano : constant := 1.0E3;
124 Sec_To_Nano : constant := 1.0E9;
125 Nanos_Since_Epoch : QWORD;
128 gettimeofday (Time_Of_Day'Access);
129 Nanos_Since_Epoch := QWORD (Time_Of_Day.tv_sec) * Sec_To_Nano
130 + QWORD (Time_Of_Day.tv_usec) * Micro_To_Nano;
133 Duration'(Nanos_Since_Epoch / Sec_To_Nano) - System_Clock;
135 end Set_Epoch_Offset;
141 procedure Timed_Delay
147 Check_Time : Duration := Clock;
150 if Mode = Relative then
152 Abs_Time := Time + Check_Time;
154 Rel_Time := Time - Check_Time;
158 if Rel_Time > 0.0 then
160 Must_Not_Fail (DosSleep (ULONG (Rel_Time * 1000.0)));
164 exit when Abs_Time <= Check_Time;
166 Rel_Time := Abs_Time - Check_Time;
173 end System.OS_Primitives;