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. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 ------------------------------------------------------------------------------
35 -- This is the NT version of this package
38 with System.OS_Interface;
40 package body System.OS_Primitives is
42 use System.OS_Interface;
44 ---------------------------------------
45 -- Data for the high resolution clock --
46 ---------------------------------------
48 Tick_Frequency : aliased LARGE_INTEGER;
49 -- Holds frequency of high-performance counter used by Clock
50 -- Windows NT uses a 1_193_182 Hz counter on PCs.
52 Base_Ticks : aliased LARGE_INTEGER;
53 -- Holds the Tick count for the base time.
55 Base_Clock : Duration;
56 -- Holds the current clock for the standard clock's base time
58 Base_Monotonic_Clock : Duration;
59 -- Holds the current clock for monotonic clock's base time
61 Base_Time : aliased Long_Long_Integer;
62 -- Holds the base time used to check for system time change, used with
63 -- the standard clock.
65 procedure Get_Base_Time;
66 -- Retrieve the base time. This base time will be used by clock to
67 -- compute the current time by adding to it a fraction of the
68 -- performance counter. This is for the implementation of a
69 -- high-resolution clock.
75 -- This implementation of clock provides high resolution timer values
76 -- using QueryPerformanceCounter. This call return a 64 bits values (based
77 -- on the 8253 16 bits counter). This counter is updated every 1/1_193_182
78 -- times per seconds. The call to QueryPerformanceCounter takes 6
79 -- microsecs to complete.
81 function Clock return Duration is
82 Max_Shift : constant Duration := 2.0;
83 Hundreds_Nano_In_Sec : constant := 1E7;
84 Current_Ticks : aliased LARGE_INTEGER;
85 Elap_Secs_Tick : Duration;
86 Elap_Secs_Sys : Duration;
87 Now : aliased Long_Long_Integer;
90 if not QueryPerformanceCounter (Current_Ticks'Access) then
94 GetSystemTimeAsFileTime (Now'Access);
97 Duration (abs (Now - Base_Time) / Hundreds_Nano_In_Sec);
100 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
101 Long_Long_Float (Tick_Frequency));
103 -- If we have a shift of more than Max_Shift seconds we resynchonize the
104 -- Clock. This is probably due to a manual Clock adjustment, an DST
105 -- adjustment or an NNTP synchronisation. And we want to adjust the
106 -- time for this system (non-monotonic) clock.
108 if abs (Elap_Secs_Sys - Elap_Secs_Tick) > Max_Shift then
112 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
113 Long_Long_Float (Tick_Frequency));
116 return Base_Clock + Elap_Secs_Tick;
123 procedure Get_Base_Time is
124 use System.OS_Interface;
126 -- The resolution for GetSystemTime is 1 millisecond.
128 -- The time to get both base times should take less than 1 millisecond.
129 -- Therefore, the elapsed time reported by GetSystemTime between both
130 -- actions should be null.
132 Max_Elapsed : constant := 0;
134 Test_Now : aliased Long_Long_Integer;
136 epoch_1970 : constant := 16#19D_B1DE_D53E_8000#; -- win32 UTC epoch
137 system_time_ns : constant := 100; -- 100 ns per tick
138 Sec_Unit : constant := 10#1#E9;
141 -- Here we must be sure that both of these calls are done in a short
142 -- amount of time. Both are base time and should in theory be taken
143 -- at the very same time.
146 GetSystemTimeAsFileTime (Base_Time'Access);
148 if not QueryPerformanceCounter (Base_Ticks'Access) then
151 "Could not query high performance counter in Clock");
155 GetSystemTimeAsFileTime (Test_Now'Access);
157 exit when Test_Now - Base_Time = Max_Elapsed;
160 Base_Clock := Duration
161 (Long_Long_Float ((Base_Time - epoch_1970) * system_time_ns) /
162 Long_Long_Float (Sec_Unit));
165 ---------------------
166 -- Monotonic_Clock --
167 ---------------------
169 function Monotonic_Clock return Duration is
170 Current_Ticks : aliased LARGE_INTEGER;
171 Elap_Secs_Tick : Duration;
173 if not QueryPerformanceCounter (Current_Ticks'Access) then
178 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
179 Long_Long_Float (Tick_Frequency));
181 return Base_Monotonic_Clock + Elap_Secs_Tick;
188 procedure Timed_Delay (Time : Duration; Mode : Integer) is
191 Check_Time : Duration := Monotonic_Clock;
194 if Mode = Relative then
196 Abs_Time := Time + Check_Time;
198 Rel_Time := Time - Check_Time;
202 if Rel_Time > 0.0 then
204 Sleep (DWORD (Rel_Time * 1000.0));
205 Check_Time := Monotonic_Clock;
207 exit when Abs_Time <= Check_Time;
209 Rel_Time := Abs_Time - Check_Time;
214 -- Package elaboration, get starting time as base
217 if not QueryPerformanceFrequency (Tick_Frequency'Access) then
218 Ada.Exceptions.Raise_Exception
219 (Program_Error'Identity,
220 "cannot get high performance counter frequency");
225 Base_Monotonic_Clock := Base_Clock;
226 end System.OS_Primitives;