Fix thinko
[official-gcc.git] / gcc / ada / 5wosprim.adb
blob4ceee490e6ed3f6327dd3c9378554bf2f74ba9e4
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 -- Copyright (C) 1998-2001 Free Software Foundation, Inc. --
10 -- --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This is the NT version of this package
36 with Ada.Exceptions;
37 with System.OS_Interface;
39 package body System.OS_Primitives is
41 use System.OS_Interface;
43 ---------------------------------------
44 -- Data for the high resolution clock --
45 ---------------------------------------
47 Tick_Frequency : aliased LARGE_INTEGER;
48 -- Holds frequency of high-performance counter used by Clock
49 -- Windows NT uses a 1_193_182 Hz counter on PCs.
51 Base_Ticks : aliased LARGE_INTEGER;
52 -- Holds the Tick count for the base time.
54 Base_Clock : Duration;
55 -- Holds the current clock for the standard clock's base time
57 Base_Monotonic_Clock : Duration;
58 -- Holds the current clock for monotonic clock's base time
60 Base_Time : aliased Long_Long_Integer;
61 -- Holds the base time used to check for system time change, used with
62 -- the standard clock.
64 procedure Get_Base_Time;
65 -- Retrieve the base time. This base time will be used by clock to
66 -- compute the current time by adding to it a fraction of the
67 -- performance counter. This is for the implementation of a
68 -- high-resolution clock.
70 -----------
71 -- Clock --
72 -----------
74 -- This implementation of clock provides high resolution timer values
75 -- using QueryPerformanceCounter. This call return a 64 bits values (based
76 -- on the 8253 16 bits counter). This counter is updated every 1/1_193_182
77 -- times per seconds. The call to QueryPerformanceCounter takes 6
78 -- microsecs to complete.
80 function Clock return Duration is
81 Max_Shift : constant Duration := 2.0;
82 Hundreds_Nano_In_Sec : constant := 1E7;
83 Current_Ticks : aliased LARGE_INTEGER;
84 Elap_Secs_Tick : Duration;
85 Elap_Secs_Sys : Duration;
86 Now : aliased Long_Long_Integer;
88 begin
89 if not QueryPerformanceCounter (Current_Ticks'Access) then
90 return 0.0;
91 end if;
93 GetSystemTimeAsFileTime (Now'Access);
95 Elap_Secs_Sys :=
96 Duration (abs (Now - Base_Time) / Hundreds_Nano_In_Sec);
98 Elap_Secs_Tick :=
99 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
100 Long_Long_Float (Tick_Frequency));
102 -- If we have a shift of more than Max_Shift seconds we resynchonize the
103 -- Clock. This is probably due to a manual Clock adjustment, an DST
104 -- adjustment or an NNTP synchronisation. And we want to adjust the
105 -- time for this system (non-monotonic) clock.
107 if abs (Elap_Secs_Sys - Elap_Secs_Tick) > Max_Shift then
108 Get_Base_Time;
110 Elap_Secs_Tick :=
111 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
112 Long_Long_Float (Tick_Frequency));
113 end if;
115 return Base_Clock + Elap_Secs_Tick;
116 end Clock;
118 -------------------
119 -- Get_Base_Time --
120 -------------------
122 procedure Get_Base_Time is
123 use System.OS_Interface;
125 -- The resolution for GetSystemTime is 1 millisecond.
127 -- The time to get both base times should take less than 1 millisecond.
128 -- Therefore, the elapsed time reported by GetSystemTime between both
129 -- actions should be null.
131 Max_Elapsed : constant := 0;
133 Test_Now : aliased Long_Long_Integer;
135 epoch_1970 : constant := 16#19D_B1DE_D53E_8000#; -- win32 UTC epoch
136 system_time_ns : constant := 100; -- 100 ns per tick
137 Sec_Unit : constant := 10#1#E9;
139 begin
140 -- Here we must be sure that both of these calls are done in a short
141 -- amount of time. Both are base time and should in theory be taken
142 -- at the very same time.
144 loop
145 GetSystemTimeAsFileTime (Base_Time'Access);
147 if not QueryPerformanceCounter (Base_Ticks'Access) then
148 pragma Assert
149 (Standard.False,
150 "Could not query high performance counter in Clock");
151 null;
152 end if;
154 GetSystemTimeAsFileTime (Test_Now'Access);
156 exit when Test_Now - Base_Time = Max_Elapsed;
157 end loop;
159 Base_Clock := Duration
160 (Long_Long_Float ((Base_Time - epoch_1970) * system_time_ns) /
161 Long_Long_Float (Sec_Unit));
162 end Get_Base_Time;
164 ---------------------
165 -- Monotonic_Clock --
166 ---------------------
168 function Monotonic_Clock return Duration is
169 Current_Ticks : aliased LARGE_INTEGER;
170 Elap_Secs_Tick : Duration;
171 begin
172 if not QueryPerformanceCounter (Current_Ticks'Access) then
173 return 0.0;
174 end if;
176 Elap_Secs_Tick :=
177 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
178 Long_Long_Float (Tick_Frequency));
180 return Base_Monotonic_Clock + Elap_Secs_Tick;
181 end Monotonic_Clock;
183 -----------------
184 -- Timed_Delay --
185 -----------------
187 procedure Timed_Delay (Time : Duration; Mode : Integer) is
188 Rel_Time : Duration;
189 Abs_Time : Duration;
190 Check_Time : Duration := Monotonic_Clock;
192 begin
193 if Mode = Relative then
194 Rel_Time := Time;
195 Abs_Time := Time + Check_Time;
196 else
197 Rel_Time := Time - Check_Time;
198 Abs_Time := Time;
199 end if;
201 if Rel_Time > 0.0 then
202 loop
203 Sleep (DWORD (Rel_Time * 1000.0));
204 Check_Time := Monotonic_Clock;
206 exit when Abs_Time <= Check_Time;
208 Rel_Time := Abs_Time - Check_Time;
209 end loop;
210 end if;
211 end Timed_Delay;
213 -- Package elaboration, get starting time as base
215 begin
216 if not QueryPerformanceFrequency (Tick_Frequency'Access) then
217 Ada.Exceptions.Raise_Exception
218 (Program_Error'Identity,
219 "cannot get high performance counter frequency");
220 end if;
222 Get_Base_Time;
224 Base_Monotonic_Clock := Base_Clock;
225 end System.OS_Primitives;