mips.h (set_volatile): Delete.
[official-gcc.git] / gcc / ada / s-osprim-mingw.adb
blobff1c9a31baa92e16d3a8886ffbdb7304fac11d01
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-2007, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 Interfaces.C;
38 package body System.OS_Primitives is
40 ---------------------------
41 -- Win32 API Definitions --
42 ---------------------------
44 -- These definitions are copied from System.OS_Interface because we do not
45 -- want to depend on gnarl here.
47 type DWORD is new Interfaces.C.unsigned_long;
49 type LARGE_INTEGER is delta 1.0 range -2.0**63 .. 2.0**63 - 1.0;
51 type BOOL is new Boolean;
52 for BOOL'Size use Interfaces.C.unsigned_long'Size;
54 procedure GetSystemTimeAsFileTime
55 (lpFileTime : not null access Long_Long_Integer);
56 pragma Import (Stdcall, GetSystemTimeAsFileTime, "GetSystemTimeAsFileTime");
58 function QueryPerformanceCounter
59 (lpPerformanceCount : not null access LARGE_INTEGER) return BOOL;
60 pragma Import
61 (Stdcall, QueryPerformanceCounter, "QueryPerformanceCounter");
63 function QueryPerformanceFrequency
64 (lpFrequency : not null access LARGE_INTEGER) return BOOL;
65 pragma Import
66 (Stdcall, QueryPerformanceFrequency, "QueryPerformanceFrequency");
68 procedure Sleep (dwMilliseconds : DWORD);
69 pragma Import (Stdcall, Sleep, External_Name => "Sleep");
71 ----------------------------------------
72 -- Data for the high resolution clock --
73 ----------------------------------------
75 -- Declare some pointers to access multi-word data above. This is needed
76 -- to workaround a limitation in the GNU/Linker auto-import feature used
77 -- to build the GNAT runtime DLLs. In fact the Clock and Monotonic_Clock
78 -- routines are inlined and they are using some multi-word variables.
79 -- GNU/Linker will fail to auto-import those variables when building
80 -- libgnarl.dll. The indirection level introduced here has no measurable
81 -- penalties.
83 -- Note that access variables below must not be declared as constant
84 -- otherwise the compiler optimization will remove this indirect access.
86 type DA is access all Duration;
87 -- Use to have indirect access to multi-word variables
89 type LIA is access all LARGE_INTEGER;
90 -- Use to have indirect access to multi-word variables
92 type LLIA is access all Long_Long_Integer;
93 -- Use to have indirect access to multi-word variables
95 Tick_Frequency : aliased LARGE_INTEGER;
96 TFA : constant LIA := Tick_Frequency'Access;
97 -- Holds frequency of high-performance counter used by Clock
98 -- Windows NT uses a 1_193_182 Hz counter on PCs.
100 Base_Ticks : aliased LARGE_INTEGER;
101 BTA : constant LIA := Base_Ticks'Access;
102 -- Holds the Tick count for the base time
104 Base_Monotonic_Ticks : aliased LARGE_INTEGER;
105 BMTA : constant LIA := Base_Monotonic_Ticks'Access;
106 -- Holds the Tick count for the base monotonic time
108 Base_Clock : aliased Duration;
109 BCA : constant DA := Base_Clock'Access;
110 -- Holds the current clock for the standard clock's base time
112 Base_Monotonic_Clock : aliased Duration;
113 BMCA : constant DA := Base_Monotonic_Clock'Access;
114 -- Holds the current clock for monotonic clock's base time
116 Base_Time : aliased Long_Long_Integer;
117 BTiA : constant LLIA := Base_Time'Access;
118 -- Holds the base time used to check for system time change, used with
119 -- the standard clock.
121 procedure Get_Base_Time;
122 -- Retrieve the base time and base ticks. These values will be used by
123 -- clock to compute the current time by adding to it a fraction of the
124 -- performance counter. This is for the implementation of a
125 -- high-resolution clock. Note that this routine does not change the base
126 -- monotonic values used by the monotonic clock.
128 -----------
129 -- Clock --
130 -----------
132 -- This implementation of clock provides high resolution timer values
133 -- using QueryPerformanceCounter. This call return a 64 bits values (based
134 -- on the 8253 16 bits counter). This counter is updated every 1/1_193_182
135 -- times per seconds. The call to QueryPerformanceCounter takes 6
136 -- microsecs to complete.
138 function Clock return Duration is
139 Max_Shift : constant Duration := 2.0;
140 Hundreds_Nano_In_Sec : constant Long_Long_Float := 1.0E7;
141 Current_Ticks : aliased LARGE_INTEGER;
142 Elap_Secs_Tick : Duration;
143 Elap_Secs_Sys : Duration;
144 Now : aliased Long_Long_Integer;
146 begin
147 if not QueryPerformanceCounter (Current_Ticks'Access) then
148 return 0.0;
149 end if;
151 GetSystemTimeAsFileTime (Now'Access);
153 Elap_Secs_Sys :=
154 Duration (Long_Long_Float (abs (Now - BTiA.all)) /
155 Hundreds_Nano_In_Sec);
157 Elap_Secs_Tick :=
158 Duration (Long_Long_Float (Current_Ticks - BTA.all) /
159 Long_Long_Float (TFA.all));
161 -- If we have a shift of more than Max_Shift seconds we resynchonize the
162 -- Clock. This is probably due to a manual Clock adjustment, an DST
163 -- adjustment or an NTP synchronisation. And we want to adjust the time
164 -- for this system (non-monotonic) clock.
166 if abs (Elap_Secs_Sys - Elap_Secs_Tick) > Max_Shift then
167 Get_Base_Time;
169 Elap_Secs_Tick :=
170 Duration (Long_Long_Float (Current_Ticks - BTA.all) /
171 Long_Long_Float (TFA.all));
172 end if;
174 return BCA.all + Elap_Secs_Tick;
175 end Clock;
177 -------------------
178 -- Get_Base_Time --
179 -------------------
181 procedure Get_Base_Time is
183 -- The resolution for GetSystemTime is 1 millisecond
185 -- The time to get both base times should take less than 1 millisecond.
186 -- Therefore, the elapsed time reported by GetSystemTime between both
187 -- actions should be null.
189 Max_Elapsed : constant := 0;
191 Test_Now : aliased Long_Long_Integer;
193 epoch_1970 : constant := 16#19D_B1DE_D53E_8000#; -- win32 UTC epoch
194 system_time_ns : constant := 100; -- 100 ns per tick
195 Sec_Unit : constant := 10#1#E9;
197 begin
198 -- Here we must be sure that both of these calls are done in a short
199 -- amount of time. Both are base time and should in theory be taken
200 -- at the very same time.
202 loop
203 GetSystemTimeAsFileTime (Base_Time'Access);
205 if not QueryPerformanceCounter (Base_Ticks'Access) then
206 pragma Assert
207 (Standard.False,
208 "Could not query high performance counter in Clock");
209 null;
210 end if;
212 GetSystemTimeAsFileTime (Test_Now'Access);
214 exit when Test_Now - Base_Time = Max_Elapsed;
215 end loop;
217 Base_Clock := Duration
218 (Long_Long_Float ((Base_Time - epoch_1970) * system_time_ns) /
219 Long_Long_Float (Sec_Unit));
220 end Get_Base_Time;
222 ---------------------
223 -- Monotonic_Clock --
224 ---------------------
226 function Monotonic_Clock return Duration is
227 Current_Ticks : aliased LARGE_INTEGER;
228 Elap_Secs_Tick : Duration;
230 begin
231 if not QueryPerformanceCounter (Current_Ticks'Access) then
232 return 0.0;
233 end if;
235 Elap_Secs_Tick :=
236 Duration (Long_Long_Float (Current_Ticks - BMTA.all) /
237 Long_Long_Float (TFA.all));
239 return BMCA.all + Elap_Secs_Tick;
240 end Monotonic_Clock;
242 -----------------
243 -- Timed_Delay --
244 -----------------
246 procedure Timed_Delay (Time : Duration; Mode : Integer) is
248 function Mode_Clock return Duration;
249 pragma Inline (Mode_Clock);
250 -- Return the current clock value using either the monotonic clock or
251 -- standard clock depending on the Mode value.
253 ----------------
254 -- Mode_Clock --
255 ----------------
257 function Mode_Clock return Duration is
258 begin
259 case Mode is
260 when Absolute_RT =>
261 return Monotonic_Clock;
262 when others =>
263 return Clock;
264 end case;
265 end Mode_Clock;
267 -- Local Variables
269 Base_Time : constant Duration := Mode_Clock;
270 -- Base_Time is used to detect clock set backward, in this case we
271 -- cannot ensure the delay accuracy.
273 Rel_Time : Duration;
274 Abs_Time : Duration;
275 Check_Time : Duration := Base_Time;
277 -- Start of processing for Timed Delay
279 begin
280 if Mode = Relative then
281 Rel_Time := Time;
282 Abs_Time := Time + Check_Time;
283 else
284 Rel_Time := Time - Check_Time;
285 Abs_Time := Time;
286 end if;
288 if Rel_Time > 0.0 then
289 loop
290 Sleep (DWORD (Rel_Time * 1000.0));
291 Check_Time := Mode_Clock;
293 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
295 Rel_Time := Abs_Time - Check_Time;
296 end loop;
297 end if;
298 end Timed_Delay;
300 ----------------
301 -- Initialize --
302 ----------------
304 Initialized : Boolean := False;
306 procedure Initialize is
307 begin
308 if Initialized then
309 return;
310 end if;
312 Initialized := True;
314 -- Get starting time as base
316 if not QueryPerformanceFrequency (Tick_Frequency'Access) then
317 raise Program_Error
318 with "cannot get high performance counter frequency";
319 end if;
321 Get_Base_Time;
323 -- Keep base clock and ticks for the monotonic clock. These values
324 -- should never be changed to ensure proper behavior of the monotonic
325 -- clock.
327 Base_Monotonic_Clock := Base_Clock;
328 Base_Monotonic_Ticks := Base_Ticks;
329 end Initialize;
331 end System.OS_Primitives;