2009-10-01 Tobias Burnus <burnus@net-b.de>
[official-gcc/alias-decl.git] / gcc / ada / s-osprim-mingw.adb
blob4a9161665358b0e8af92441540c3e51da685d771
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT 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-2009, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 -- This is the NT version of this package
34 with System.Win32.Ext;
36 package body System.OS_Primitives is
38 use System.Win32;
39 use System.Win32.Ext;
41 ----------------------------------------
42 -- Data for the high resolution clock --
43 ----------------------------------------
45 -- Declare some pointers to access multi-word data above. This is needed
46 -- to workaround a limitation in the GNU/Linker auto-import feature used
47 -- to build the GNAT runtime DLLs. In fact the Clock and Monotonic_Clock
48 -- routines are inlined and they are using some multi-word variables.
49 -- GNU/Linker will fail to auto-import those variables when building
50 -- libgnarl.dll. The indirection level introduced here has no measurable
51 -- penalties.
53 -- Note that access variables below must not be declared as constant
54 -- otherwise the compiler optimization will remove this indirect access.
56 type DA is access all Duration;
57 -- Use to have indirect access to multi-word variables
59 type LIA is access all LARGE_INTEGER;
60 -- Use to have indirect access to multi-word variables
62 type LLIA is access all Long_Long_Integer;
63 -- Use to have indirect access to multi-word variables
65 Tick_Frequency : aliased LARGE_INTEGER;
66 TFA : constant LIA := Tick_Frequency'Access;
67 -- Holds frequency of high-performance counter used by Clock
68 -- Windows NT uses a 1_193_182 Hz counter on PCs.
70 Base_Ticks : aliased LARGE_INTEGER;
71 BTA : constant LIA := Base_Ticks'Access;
72 -- Holds the Tick count for the base time
74 Base_Monotonic_Ticks : aliased LARGE_INTEGER;
75 BMTA : constant LIA := Base_Monotonic_Ticks'Access;
76 -- Holds the Tick count for the base monotonic time
78 Base_Clock : aliased Duration;
79 BCA : constant DA := Base_Clock'Access;
80 -- Holds the current clock for the standard clock's base time
82 Base_Monotonic_Clock : aliased Duration;
83 BMCA : constant DA := Base_Monotonic_Clock'Access;
84 -- Holds the current clock for monotonic clock's base time
86 Base_Time : aliased Long_Long_Integer;
87 BTiA : constant LLIA := Base_Time'Access;
88 -- Holds the base time used to check for system time change, used with
89 -- the standard clock.
91 procedure Get_Base_Time;
92 -- Retrieve the base time and base ticks. These values will be used by
93 -- clock to compute the current time by adding to it a fraction of the
94 -- performance counter. This is for the implementation of a
95 -- high-resolution clock. Note that this routine does not change the base
96 -- monotonic values used by the monotonic clock.
98 -----------
99 -- Clock --
100 -----------
102 -- This implementation of clock provides high resolution timer values
103 -- using QueryPerformanceCounter. This call return a 64 bits values (based
104 -- on the 8253 16 bits counter). This counter is updated every 1/1_193_182
105 -- times per seconds. The call to QueryPerformanceCounter takes 6
106 -- microsecs to complete.
108 function Clock return Duration is
109 Max_Shift : constant Duration := 2.0;
110 Hundreds_Nano_In_Sec : constant Long_Long_Float := 1.0E7;
111 Current_Ticks : aliased LARGE_INTEGER;
112 Elap_Secs_Tick : Duration;
113 Elap_Secs_Sys : Duration;
114 Now : aliased Long_Long_Integer;
116 begin
117 if QueryPerformanceCounter (Current_Ticks'Access) = Win32.FALSE then
118 return 0.0;
119 end if;
121 GetSystemTimeAsFileTime (Now'Access);
123 Elap_Secs_Sys :=
124 Duration (Long_Long_Float (abs (Now - BTiA.all)) /
125 Hundreds_Nano_In_Sec);
127 Elap_Secs_Tick :=
128 Duration (Long_Long_Float (Current_Ticks - BTA.all) /
129 Long_Long_Float (TFA.all));
131 -- If we have a shift of more than Max_Shift seconds we resynchronize
132 -- the Clock. This is probably due to a manual Clock adjustment, an
133 -- DST adjustment or an NTP synchronisation. And we want to adjust the
134 -- time for this system (non-monotonic) clock.
136 if abs (Elap_Secs_Sys - Elap_Secs_Tick) > Max_Shift then
137 Get_Base_Time;
139 Elap_Secs_Tick :=
140 Duration (Long_Long_Float (Current_Ticks - BTA.all) /
141 Long_Long_Float (TFA.all));
142 end if;
144 return BCA.all + Elap_Secs_Tick;
145 end Clock;
147 -------------------
148 -- Get_Base_Time --
149 -------------------
151 procedure Get_Base_Time is
153 -- The resolution for GetSystemTime is 1 millisecond
155 -- The time to get both base times should take less than 1 millisecond.
156 -- Therefore, the elapsed time reported by GetSystemTime between both
157 -- actions should be null.
159 Max_Elapsed : constant := 0;
160 epoch_1970 : constant := 16#19D_B1DE_D53E_8000#; -- win32 UTC epoch
161 system_time_ns : constant := 100; -- 100 ns per tick
162 Sec_Unit : constant := 10#1#E9;
163 Test_Now : aliased Long_Long_Integer;
164 Loc_Ticks : aliased LARGE_INTEGER;
165 Loc_Time : aliased Long_Long_Integer;
166 Elapsed : Long_Long_Integer;
167 Current_Max : Long_Long_Integer := Long_Long_Integer'Last;
169 begin
170 -- Here we must be sure that both of these calls are done in a short
171 -- amount of time. Both are base time and should in theory be taken
172 -- at the very same time.
174 -- The goal of the following loop is to synchronize the system time
175 -- with the Win32 performance counter by getting a base offset for both.
176 -- Using these offsets it is then possible to compute actual time using
177 -- a performance counter which has a better precision than the Win32
178 -- time API.
180 -- Try at most 10th times to reach the best synchronisation (below 1
181 -- millisecond) otherwise the runtime will use the best value reached
182 -- during the runs.
184 for K in 1 .. 10 loop
185 GetSystemTimeAsFileTime (Loc_Time'Access);
187 if QueryPerformanceCounter (Loc_Ticks'Access) = Win32.FALSE then
188 pragma Assert
189 (Standard.False,
190 "Could not query high performance counter in Clock");
191 null;
192 end if;
194 GetSystemTimeAsFileTime (Test_Now'Access);
196 Elapsed := Test_Now - Loc_Time;
198 if Elapsed < Current_Max then
199 Base_Time := Loc_Time;
200 Base_Ticks := Loc_Ticks;
201 Current_Max := Elapsed;
202 end if;
204 exit when Elapsed = Max_Elapsed;
205 end loop;
207 Base_Clock := Duration
208 (Long_Long_Float ((Base_Time - epoch_1970) * system_time_ns) /
209 Long_Long_Float (Sec_Unit));
210 end Get_Base_Time;
212 ---------------------
213 -- Monotonic_Clock --
214 ---------------------
216 function Monotonic_Clock return Duration is
217 Current_Ticks : aliased LARGE_INTEGER;
218 Elap_Secs_Tick : Duration;
219 begin
220 if QueryPerformanceCounter (Current_Ticks'Access) = Win32.FALSE then
221 return 0.0;
222 else
223 Elap_Secs_Tick :=
224 Duration (Long_Long_Float (Current_Ticks - BMTA.all) /
225 Long_Long_Float (TFA.all));
226 return BMCA.all + Elap_Secs_Tick;
227 end if;
228 end Monotonic_Clock;
230 -----------------
231 -- Timed_Delay --
232 -----------------
234 procedure Timed_Delay (Time : Duration; Mode : Integer) is
236 function Mode_Clock return Duration;
237 pragma Inline (Mode_Clock);
238 -- Return the current clock value using either the monotonic clock or
239 -- standard clock depending on the Mode value.
241 ----------------
242 -- Mode_Clock --
243 ----------------
245 function Mode_Clock return Duration is
246 begin
247 case Mode is
248 when Absolute_RT =>
249 return Monotonic_Clock;
250 when others =>
251 return Clock;
252 end case;
253 end Mode_Clock;
255 -- Local Variables
257 Base_Time : constant Duration := Mode_Clock;
258 -- Base_Time is used to detect clock set backward, in this case we
259 -- cannot ensure the delay accuracy.
261 Rel_Time : Duration;
262 Abs_Time : Duration;
263 Check_Time : Duration := Base_Time;
265 -- Start of processing for Timed Delay
267 begin
268 if Mode = Relative then
269 Rel_Time := Time;
270 Abs_Time := Time + Check_Time;
271 else
272 Rel_Time := Time - Check_Time;
273 Abs_Time := Time;
274 end if;
276 if Rel_Time > 0.0 then
277 loop
278 Sleep (DWORD (Rel_Time * 1000.0));
279 Check_Time := Mode_Clock;
281 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
283 Rel_Time := Abs_Time - Check_Time;
284 end loop;
285 end if;
286 end Timed_Delay;
288 ----------------
289 -- Initialize --
290 ----------------
292 Initialized : Boolean := False;
294 procedure Initialize is
295 begin
296 if Initialized then
297 return;
298 end if;
300 Initialized := True;
302 -- Get starting time as base
304 if QueryPerformanceFrequency (Tick_Frequency'Access) = Win32.FALSE then
305 raise Program_Error with
306 "cannot get high performance counter frequency";
307 end if;
309 Get_Base_Time;
311 -- Keep base clock and ticks for the monotonic clock. These values
312 -- should never be changed to ensure proper behavior of the monotonic
313 -- clock.
315 Base_Monotonic_Clock := Base_Clock;
316 Base_Monotonic_Ticks := Base_Ticks;
317 end Initialize;
319 end System.OS_Primitives;