1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . O S _ P R I M I T I V E S --
10 -- Copyright (C) 1998-2001 Free Software Foundation, Inc. --
12 -- GNARL is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNARL; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNARL was developed by the GNARL team at Florida State University. It is --
31 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
32 -- State University (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 -- This is the NT version of this package
39 with System.OS_Interface;
41 package body System.OS_Primitives is
43 use System.OS_Interface;
45 ---------------------------------------
46 -- Data for the high resolution clock --
47 ---------------------------------------
49 Tick_Frequency : aliased LARGE_INTEGER;
50 -- Holds frequency of high-performance counter used by Clock
51 -- Windows NT uses a 1_193_182 Hz counter on PCs.
53 Base_Ticks : aliased LARGE_INTEGER;
54 -- Holds the Tick count for the base time.
56 Base_Clock : Duration;
57 -- Holds the current clock for the standard clock's base time
59 Base_Monotonic_Clock : Duration;
60 -- Holds the current clock for monotonic clock's base time
62 Base_Time : aliased Long_Long_Integer;
63 -- Holds the base time used to check for system time change, used with
64 -- the standard clock.
66 procedure Get_Base_Time;
67 -- Retrieve the base time. This base time will be used by clock to
68 -- compute the current time by adding to it a fraction of the
69 -- performance counter. This is for the implementation of a
70 -- high-resolution clock.
76 -- This implementation of clock provides high resolution timer values
77 -- using QueryPerformanceCounter. This call return a 64 bits values (based
78 -- on the 8253 16 bits counter). This counter is updated every 1/1_193_182
79 -- times per seconds. The call to QueryPerformanceCounter takes 6
80 -- microsecs to complete.
82 function Clock return Duration is
83 Max_Shift : constant Duration := 2.0;
84 Hundreds_Nano_In_Sec : constant := 1E7;
85 Current_Ticks : aliased LARGE_INTEGER;
86 Elap_Secs_Tick : Duration;
87 Elap_Secs_Sys : Duration;
88 Now : aliased Long_Long_Integer;
91 if not QueryPerformanceCounter (Current_Ticks'Access) then
95 GetSystemTimeAsFileTime (Now'Access);
98 Duration (abs (Now - Base_Time) / Hundreds_Nano_In_Sec);
101 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
102 Long_Long_Float (Tick_Frequency));
104 -- If we have a shift of more than Max_Shift seconds we resynchonize the
105 -- Clock. This is probably due to a manual Clock adjustment, an DST
106 -- adjustment or an NNTP synchronisation. And we want to adjust the
107 -- time for this system (non-monotonic) clock.
109 if abs (Elap_Secs_Sys - Elap_Secs_Tick) > Max_Shift then
113 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
114 Long_Long_Float (Tick_Frequency));
117 return Base_Clock + Elap_Secs_Tick;
124 procedure Get_Base_Time is
125 use System.OS_Interface;
127 -- The resolution for GetSystemTime is 1 millisecond.
129 -- The time to get both base times should take less than 1 millisecond.
130 -- Therefore, the elapsed time reported by GetSystemTime between both
131 -- actions should be null.
133 Max_Elapsed : constant := 0;
135 Test_Now : aliased Long_Long_Integer;
137 epoch_1970 : constant := 16#19D_B1DE_D53E_8000#; -- win32 UTC epoch
138 system_time_ns : constant := 100; -- 100 ns per tick
139 Sec_Unit : constant := 10#1#E9;
142 -- Here we must be sure that both of these calls are done in a short
143 -- amount of time. Both are base time and should in theory be taken
144 -- at the very same time.
147 GetSystemTimeAsFileTime (Base_Time'Access);
149 if not QueryPerformanceCounter (Base_Ticks'Access) then
152 "Could not query high performance counter in Clock");
156 GetSystemTimeAsFileTime (Test_Now'Access);
158 exit when Test_Now - Base_Time = Max_Elapsed;
161 Base_Clock := Duration
162 (Long_Long_Float ((Base_Time - epoch_1970) * system_time_ns) /
163 Long_Long_Float (Sec_Unit));
166 ---------------------
167 -- Monotonic_Clock --
168 ---------------------
170 function Monotonic_Clock return Duration is
171 Current_Ticks : aliased LARGE_INTEGER;
172 Elap_Secs_Tick : Duration;
174 if not QueryPerformanceCounter (Current_Ticks'Access) then
179 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
180 Long_Long_Float (Tick_Frequency));
182 return Base_Monotonic_Clock + Elap_Secs_Tick;
189 procedure Timed_Delay (Time : Duration; Mode : Integer) is
192 Check_Time : Duration := Monotonic_Clock;
195 if Mode = Relative then
197 Abs_Time := Time + Check_Time;
199 Rel_Time := Time - Check_Time;
203 if Rel_Time > 0.0 then
205 Sleep (DWORD (Rel_Time * 1000.0));
206 Check_Time := Monotonic_Clock;
208 exit when Abs_Time <= Check_Time;
210 Rel_Time := Abs_Time - Check_Time;
215 -- Package elaboration, get starting time as base
218 if not QueryPerformanceFrequency (Tick_Frequency'Access) then
219 Ada.Exceptions.Raise_Exception
220 (Program_Error'Identity,
221 "cannot get high performance counter frequency");
226 Base_Monotonic_Clock := Base_Clock;
227 end System.OS_Primitives;