fixing pr42337
[official-gcc.git] / gcc / ada / s-osprim-mingw.adb
blobc818811ed1a75ae7e36561eaebf7fa59b751f54c
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);
202 if QueryPerformanceCounter (Ctrl_Ticks'Access) = Win32.FALSE then
203 pragma Assert
204 (Standard.False,
205 "Could not query high performance counter in Clock");
206 null;
207 end if;
208 exit when Loc_Time /= Ctrl_Time;
209 Loc_Ticks := Ctrl_Ticks;
210 end loop;
212 -- Check elapsed Performance Counter between samples
213 -- to choose the best one.
215 Elapsed := Ctrl_Ticks - Loc_Ticks;
217 if Elapsed < Current_Max then
218 Base_Time := Loc_Time;
219 Base_Ticks := Loc_Ticks;
220 Current_Max := Elapsed;
221 -- Exit the loop when we have reached the expected precision
222 exit when Elapsed <= Max_Elapsed;
223 end if;
224 end loop;
226 Base_Clock := Duration
227 (Long_Long_Float ((Base_Time - epoch_1970) * system_time_ns) /
228 Long_Long_Float (Sec_Unit));
229 end Get_Base_Time;
231 ---------------------
232 -- Monotonic_Clock --
233 ---------------------
235 function Monotonic_Clock return Duration is
236 Current_Ticks : aliased LARGE_INTEGER;
237 Elap_Secs_Tick : Duration;
238 begin
239 if QueryPerformanceCounter (Current_Ticks'Access) = Win32.FALSE then
240 return 0.0;
241 else
242 Elap_Secs_Tick :=
243 Duration (Long_Long_Float (Current_Ticks - BMTA.all) /
244 Long_Long_Float (TFA.all));
245 return BMCA.all + Elap_Secs_Tick;
246 end if;
247 end Monotonic_Clock;
249 -----------------
250 -- Timed_Delay --
251 -----------------
253 procedure Timed_Delay (Time : Duration; Mode : Integer) is
255 function Mode_Clock return Duration;
256 pragma Inline (Mode_Clock);
257 -- Return the current clock value using either the monotonic clock or
258 -- standard clock depending on the Mode value.
260 ----------------
261 -- Mode_Clock --
262 ----------------
264 function Mode_Clock return Duration is
265 begin
266 case Mode is
267 when Absolute_RT =>
268 return Monotonic_Clock;
269 when others =>
270 return Clock;
271 end case;
272 end Mode_Clock;
274 -- Local Variables
276 Base_Time : constant Duration := Mode_Clock;
277 -- Base_Time is used to detect clock set backward, in this case we
278 -- cannot ensure the delay accuracy.
280 Rel_Time : Duration;
281 Abs_Time : Duration;
282 Check_Time : Duration := Base_Time;
284 -- Start of processing for Timed Delay
286 begin
287 if Mode = Relative then
288 Rel_Time := Time;
289 Abs_Time := Time + Check_Time;
290 else
291 Rel_Time := Time - Check_Time;
292 Abs_Time := Time;
293 end if;
295 if Rel_Time > 0.0 then
296 loop
297 Sleep (DWORD (Rel_Time * 1000.0));
298 Check_Time := Mode_Clock;
300 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
302 Rel_Time := Abs_Time - Check_Time;
303 end loop;
304 end if;
305 end Timed_Delay;
307 ----------------
308 -- Initialize --
309 ----------------
311 Initialized : Boolean := False;
313 procedure Initialize is
314 begin
315 if Initialized then
316 return;
317 end if;
319 Initialized := True;
321 -- Get starting time as base
323 if QueryPerformanceFrequency (Tick_Frequency'Access) = Win32.FALSE then
324 raise Program_Error with
325 "cannot get high performance counter frequency";
326 end if;
328 Get_Base_Time;
330 -- Keep base clock and ticks for the monotonic clock. These values
331 -- should never be changed to ensure proper behavior of the monotonic
332 -- clock.
334 Base_Monotonic_Clock := Base_Clock;
335 Base_Monotonic_Ticks := Base_Ticks;
336 end Initialize;
338 end System.OS_Primitives;