* rtl.h (struct rtx_def): Update comments.
[official-gcc.git] / gcc / ada / 5oosprim.adb
blob7bd90df3a13bc7efcf5ac1ee6fd1131039c40116
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . O S _ P R I M I T I V E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1998-2001 Free Software Foundation, Inc. --
11 -- --
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. --
22 -- --
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. --
29 -- --
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). --
33 -- --
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
44 ----------------
45 -- Local Data --
46 ----------------
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.
68 ------------------
69 -- System_Clock --
70 ------------------
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;
81 begin
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;
88 end System_Clock;
90 -----------
91 -- Clock --
92 -----------
94 function Clock return Duration is
95 begin
96 return System_Clock + Epoch_Offset;
97 end Clock;
99 ---------------------
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
114 tv_sec : long;
115 tv_usec : long;
116 end 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;
128 begin
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;
133 Epoch_Offset :=
134 Duration'(Nanos_Since_Epoch / Sec_To_Nano) - System_Clock;
136 end Set_Epoch_Offset;
138 -----------------
139 -- Timed_Delay --
140 -----------------
142 procedure Timed_Delay
143 (Time : Duration;
144 Mode : Integer)
146 Rel_Time : Duration;
147 Abs_Time : Duration;
148 Check_Time : Duration := Clock;
150 begin
151 if Mode = Relative then
152 Rel_Time := Time;
153 Abs_Time := Time + Check_Time;
154 else
155 Rel_Time := Time - Check_Time;
156 Abs_Time := Time;
157 end if;
159 if Rel_Time > 0.0 then
160 loop
161 Must_Not_Fail (DosSleep (ULONG (Rel_Time * 1000.0)));
163 Check_Time := Clock;
165 exit when Abs_Time <= Check_Time;
167 Rel_Time := Abs_Time - Check_Time;
168 end loop;
169 end if;
170 end Timed_Delay;
172 begin
173 Set_Epoch_Offset;
174 end System.OS_Primitives;