Merged revision 156805 into branch.
[official-gcc.git] / gcc / ada / s-osprim-mingw.adb
blobfc286e67b3037d1069af417b0b78d63661711e16
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 epoch_1970 : constant := 16#19D_B1DE_D53E_8000#; -- win32 UTC epoch
160 system_time_ns : constant := 100; -- 100 ns per tick
161 Sec_Unit : constant := 10#1#E9;
162 Max_Elapsed : constant LARGE_INTEGER :=
163 LARGE_INTEGER (Tick_Frequency / 100_000);
164 -- Look for a precision of 0.01 ms
166 Loc_Ticks, Ctrl_Ticks : aliased LARGE_INTEGER;
167 Loc_Time, Ctrl_Time : aliased Long_Long_Integer;
168 Elapsed : LARGE_INTEGER;
169 Current_Max : LARGE_INTEGER := LARGE_INTEGER'Last;
171 begin
172 -- Here we must be sure that both of these calls are done in a short
173 -- amount of time. Both are base time and should in theory be taken
174 -- at the very same time.
176 -- The goal of the following loop is to synchronize the system time
177 -- with the Win32 performance counter by getting a base offset for both.
178 -- Using these offsets it is then possible to compute actual time using
179 -- a performance counter which has a better precision than the Win32
180 -- time API.
182 -- Try at most 10th times to reach the best synchronisation (below 1
183 -- millisecond) otherwise the runtime will use the best value reached
184 -- during the runs.
186 for K in 1 .. 10 loop
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 (Ctrl_Time'Access);
196 -- Scan for clock tick, will take upto 16ms/1ms depending on PC.
197 -- This cannot be an infinite loop or the system hardware is badly
198 -- dammaged.
200 loop
201 GetSystemTimeAsFileTime (Loc_Time'Access);
203 if QueryPerformanceCounter (Ctrl_Ticks'Access) = Win32.FALSE then
204 pragma Assert
205 (Standard.False,
206 "Could not query high performance counter in Clock");
207 null;
208 end if;
210 exit when Loc_Time /= Ctrl_Time;
211 Loc_Ticks := Ctrl_Ticks;
212 end loop;
214 -- Check elapsed Performance Counter between samples
215 -- to choose the best one.
217 Elapsed := Ctrl_Ticks - Loc_Ticks;
219 if Elapsed < Current_Max then
220 Base_Time := Loc_Time;
221 Base_Ticks := Loc_Ticks;
222 Current_Max := Elapsed;
224 -- Exit the loop when we have reached the expected precision
226 exit when Elapsed <= Max_Elapsed;
227 end if;
228 end loop;
230 Base_Clock := Duration
231 (Long_Long_Float ((Base_Time - epoch_1970) * system_time_ns) /
232 Long_Long_Float (Sec_Unit));
233 end Get_Base_Time;
235 ---------------------
236 -- Monotonic_Clock --
237 ---------------------
239 function Monotonic_Clock return Duration is
240 Current_Ticks : aliased LARGE_INTEGER;
241 Elap_Secs_Tick : Duration;
242 begin
243 if QueryPerformanceCounter (Current_Ticks'Access) = Win32.FALSE then
244 return 0.0;
245 else
246 Elap_Secs_Tick :=
247 Duration (Long_Long_Float (Current_Ticks - BMTA.all) /
248 Long_Long_Float (TFA.all));
249 return BMCA.all + Elap_Secs_Tick;
250 end if;
251 end Monotonic_Clock;
253 -----------------
254 -- Timed_Delay --
255 -----------------
257 procedure Timed_Delay (Time : Duration; Mode : Integer) is
259 function Mode_Clock return Duration;
260 pragma Inline (Mode_Clock);
261 -- Return the current clock value using either the monotonic clock or
262 -- standard clock depending on the Mode value.
264 ----------------
265 -- Mode_Clock --
266 ----------------
268 function Mode_Clock return Duration is
269 begin
270 case Mode is
271 when Absolute_RT =>
272 return Monotonic_Clock;
273 when others =>
274 return Clock;
275 end case;
276 end Mode_Clock;
278 -- Local Variables
280 Base_Time : constant Duration := Mode_Clock;
281 -- Base_Time is used to detect clock set backward, in this case we
282 -- cannot ensure the delay accuracy.
284 Rel_Time : Duration;
285 Abs_Time : Duration;
286 Check_Time : Duration := Base_Time;
288 -- Start of processing for Timed Delay
290 begin
291 if Mode = Relative then
292 Rel_Time := Time;
293 Abs_Time := Time + Check_Time;
294 else
295 Rel_Time := Time - Check_Time;
296 Abs_Time := Time;
297 end if;
299 if Rel_Time > 0.0 then
300 loop
301 Sleep (DWORD (Rel_Time * 1000.0));
302 Check_Time := Mode_Clock;
304 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
306 Rel_Time := Abs_Time - Check_Time;
307 end loop;
308 end if;
309 end Timed_Delay;
311 ----------------
312 -- Initialize --
313 ----------------
315 Initialized : Boolean := False;
317 procedure Initialize is
318 begin
319 if Initialized then
320 return;
321 end if;
323 Initialized := True;
325 -- Get starting time as base
327 if QueryPerformanceFrequency (Tick_Frequency'Access) = Win32.FALSE then
328 raise Program_Error with
329 "cannot get high performance counter frequency";
330 end if;
332 Get_Base_Time;
334 -- Keep base clock and ticks for the monotonic clock. These values
335 -- should never be changed to ensure proper behavior of the monotonic
336 -- clock.
338 Base_Monotonic_Clock := Base_Clock;
339 Base_Monotonic_Ticks := Base_Ticks;
340 end Initialize;
342 end System.OS_Primitives;