Daily bump.
[official-gcc.git] / gcc / ada / 5wosprim.adb
blob4b11415327b65d6877c54b15f45dd548269122f2
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 -- $Revision: 1.1.16.1 $
10 -- --
11 -- Copyright (C) 1998-2001 Free Software Foundation, Inc. --
12 -- --
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. --
23 -- --
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. --
30 -- --
31 -- GNARL was developed by the GNARL team at Florida State University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 -- --
34 ------------------------------------------------------------------------------
36 -- This is the NT version of this package
38 with Ada.Exceptions;
39 with System.OS_Interface;
41 package body System.OS_Primitives is
43 use System.OS_Interface;
45 ---------------------------------------
46 -- Data for the high resolution clock --
47 ---------------------------------------
49 Tick_Frequency : aliased LARGE_INTEGER;
50 -- Holds frequency of high-performance counter used by Clock
51 -- Windows NT uses a 1_193_182 Hz counter on PCs.
53 Base_Ticks : aliased LARGE_INTEGER;
54 -- Holds the Tick count for the base time.
56 Base_Clock : Duration;
57 -- Holds the current clock for the standard clock's base time
59 Base_Monotonic_Clock : Duration;
60 -- Holds the current clock for monotonic clock's base time
62 Base_Time : aliased Long_Long_Integer;
63 -- Holds the base time used to check for system time change, used with
64 -- the standard clock.
66 procedure Get_Base_Time;
67 -- Retrieve the base time. This base time will be used by clock to
68 -- compute the current time by adding to it a fraction of the
69 -- performance counter. This is for the implementation of a
70 -- high-resolution clock.
72 -----------
73 -- Clock --
74 -----------
76 -- This implementation of clock provides high resolution timer values
77 -- using QueryPerformanceCounter. This call return a 64 bits values (based
78 -- on the 8253 16 bits counter). This counter is updated every 1/1_193_182
79 -- times per seconds. The call to QueryPerformanceCounter takes 6
80 -- microsecs to complete.
82 function Clock return Duration is
83 Max_Shift : constant Duration := 2.0;
84 Hundreds_Nano_In_Sec : constant := 1E7;
85 Current_Ticks : aliased LARGE_INTEGER;
86 Elap_Secs_Tick : Duration;
87 Elap_Secs_Sys : Duration;
88 Now : aliased Long_Long_Integer;
90 begin
91 if not QueryPerformanceCounter (Current_Ticks'Access) then
92 return 0.0;
93 end if;
95 GetSystemTimeAsFileTime (Now'Access);
97 Elap_Secs_Sys :=
98 Duration (abs (Now - Base_Time) / Hundreds_Nano_In_Sec);
100 Elap_Secs_Tick :=
101 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
102 Long_Long_Float (Tick_Frequency));
104 -- If we have a shift of more than Max_Shift seconds we resynchonize the
105 -- Clock. This is probably due to a manual Clock adjustment, an DST
106 -- adjustment or an NNTP synchronisation. And we want to adjust the
107 -- time for this system (non-monotonic) clock.
109 if abs (Elap_Secs_Sys - Elap_Secs_Tick) > Max_Shift then
110 Get_Base_Time;
112 Elap_Secs_Tick :=
113 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
114 Long_Long_Float (Tick_Frequency));
115 end if;
117 return Base_Clock + Elap_Secs_Tick;
118 end Clock;
120 -------------------
121 -- Get_Base_Time --
122 -------------------
124 procedure Get_Base_Time is
125 use System.OS_Interface;
127 -- The resolution for GetSystemTime is 1 millisecond.
129 -- The time to get both base times should take less than 1 millisecond.
130 -- Therefore, the elapsed time reported by GetSystemTime between both
131 -- actions should be null.
133 Max_Elapsed : constant := 0;
135 Test_Now : aliased Long_Long_Integer;
137 epoch_1970 : constant := 16#19D_B1DE_D53E_8000#; -- win32 UTC epoch
138 system_time_ns : constant := 100; -- 100 ns per tick
139 Sec_Unit : constant := 10#1#E9;
141 begin
142 -- Here we must be sure that both of these calls are done in a short
143 -- amount of time. Both are base time and should in theory be taken
144 -- at the very same time.
146 loop
147 GetSystemTimeAsFileTime (Base_Time'Access);
149 if not QueryPerformanceCounter (Base_Ticks'Access) then
150 pragma Assert
151 (Standard.False,
152 "Could not query high performance counter in Clock");
153 null;
154 end if;
156 GetSystemTimeAsFileTime (Test_Now'Access);
158 exit when Test_Now - Base_Time = Max_Elapsed;
159 end loop;
161 Base_Clock := Duration
162 (Long_Long_Float ((Base_Time - epoch_1970) * system_time_ns) /
163 Long_Long_Float (Sec_Unit));
164 end Get_Base_Time;
166 ---------------------
167 -- Monotonic_Clock --
168 ---------------------
170 function Monotonic_Clock return Duration is
171 Current_Ticks : aliased LARGE_INTEGER;
172 Elap_Secs_Tick : Duration;
173 begin
174 if not QueryPerformanceCounter (Current_Ticks'Access) then
175 return 0.0;
176 end if;
178 Elap_Secs_Tick :=
179 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
180 Long_Long_Float (Tick_Frequency));
182 return Base_Monotonic_Clock + Elap_Secs_Tick;
183 end Monotonic_Clock;
185 -----------------
186 -- Timed_Delay --
187 -----------------
189 procedure Timed_Delay (Time : Duration; Mode : Integer) is
190 Rel_Time : Duration;
191 Abs_Time : Duration;
192 Check_Time : Duration := Monotonic_Clock;
194 begin
195 if Mode = Relative then
196 Rel_Time := Time;
197 Abs_Time := Time + Check_Time;
198 else
199 Rel_Time := Time - Check_Time;
200 Abs_Time := Time;
201 end if;
203 if Rel_Time > 0.0 then
204 loop
205 Sleep (DWORD (Rel_Time * 1000.0));
206 Check_Time := Monotonic_Clock;
208 exit when Abs_Time <= Check_Time;
210 Rel_Time := Abs_Time - Check_Time;
211 end loop;
212 end if;
213 end Timed_Delay;
215 -- Package elaboration, get starting time as base
217 begin
218 if not QueryPerformanceFrequency (Tick_Frequency'Access) then
219 Ada.Exceptions.Raise_Exception
220 (Program_Error'Identity,
221 "cannot get high performance counter frequency");
222 end if;
224 Get_Base_Time;
226 Base_Monotonic_Clock := Base_Clock;
227 end System.OS_Primitives;