1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . O S _ P R I M I T I V E S --
9 -- Copyright (C) 1998-2009, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This is the NT version of this package
34 with System.Win32.Ext;
36 package body System.OS_Primitives is
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
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.
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;
117 if QueryPerformanceCounter (Current_Ticks'Access) = Win32.FALSE then
121 GetSystemTimeAsFileTime (Now'Access);
124 Duration (Long_Long_Float (abs (Now - BTiA.all)) /
125 Hundreds_Nano_In_Sec);
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
140 Duration (Long_Long_Float (Current_Ticks - BTA.all) /
141 Long_Long_Float (TFA.all));
144 return BCA.all + Elap_Secs_Tick;
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;
161 Test_Now : aliased Long_Long_Integer;
163 epoch_1970 : constant := 16#19D_B1DE_D53E_8000#; -- win32 UTC epoch
164 system_time_ns : constant := 100; -- 100 ns per tick
165 Sec_Unit : constant := 10#1#E9;
168 -- Here we must be sure that both of these calls are done in a short
169 -- amount of time. Both are base time and should in theory be taken
170 -- at the very same time.
173 GetSystemTimeAsFileTime (Base_Time'Access);
175 if QueryPerformanceCounter (Base_Ticks'Access) = Win32.FALSE then
178 "Could not query high performance counter in Clock");
182 GetSystemTimeAsFileTime (Test_Now'Access);
184 exit when Test_Now - Base_Time = Max_Elapsed;
187 Base_Clock := Duration
188 (Long_Long_Float ((Base_Time - epoch_1970) * system_time_ns) /
189 Long_Long_Float (Sec_Unit));
192 ---------------------
193 -- Monotonic_Clock --
194 ---------------------
196 function Monotonic_Clock return Duration is
197 Current_Ticks : aliased LARGE_INTEGER;
198 Elap_Secs_Tick : Duration;
201 if QueryPerformanceCounter (Current_Ticks'Access) = Win32.FALSE then
206 Duration (Long_Long_Float (Current_Ticks - BMTA.all) /
207 Long_Long_Float (TFA.all));
209 return BMCA.all + Elap_Secs_Tick;
216 procedure Timed_Delay (Time : Duration; Mode : Integer) is
218 function Mode_Clock return Duration;
219 pragma Inline (Mode_Clock);
220 -- Return the current clock value using either the monotonic clock or
221 -- standard clock depending on the Mode value.
227 function Mode_Clock return Duration is
231 return Monotonic_Clock;
239 Base_Time : constant Duration := Mode_Clock;
240 -- Base_Time is used to detect clock set backward, in this case we
241 -- cannot ensure the delay accuracy.
245 Check_Time : Duration := Base_Time;
247 -- Start of processing for Timed Delay
250 if Mode = Relative then
252 Abs_Time := Time + Check_Time;
254 Rel_Time := Time - Check_Time;
258 if Rel_Time > 0.0 then
260 Sleep (DWORD (Rel_Time * 1000.0));
261 Check_Time := Mode_Clock;
263 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
265 Rel_Time := Abs_Time - Check_Time;
274 Initialized : Boolean := False;
276 procedure Initialize is
284 -- Get starting time as base
286 if QueryPerformanceFrequency (Tick_Frequency'Access) = Win32.FALSE then
287 raise Program_Error with
288 "cannot get high performance counter frequency";
293 -- Keep base clock and ticks for the monotonic clock. These values
294 -- should never be changed to ensure proper behavior of the monotonic
297 Base_Monotonic_Clock := Base_Clock;
298 Base_Monotonic_Ticks := Base_Ticks;
301 end System.OS_Primitives;