OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / 5wosprim.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                GNU ADA 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 --                                                                          --
10 --          Copyright (C) 1998-2001 Free Software Foundation, Inc.          --
11 --                                                                          --
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.                                                      --
22 --                                                                          --
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.                                      --
29 --                                                                          --
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).                                  --
33 --                                                                          --
34 ------------------------------------------------------------------------------
35
36 --  This is the NT version of this package
37
38 with Ada.Exceptions;
39 with System.OS_Interface;
40
41 package body System.OS_Primitives is
42
43    use System.OS_Interface;
44
45    ---------------------------------------
46    -- Data for the high resolution clock --
47    ---------------------------------------
48
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.
52
53    Base_Ticks           : aliased LARGE_INTEGER;
54    --  Holds the Tick count for the base time.
55
56    Base_Clock           : Duration;
57    --  Holds the current clock for the standard clock's base time
58
59    Base_Monotonic_Clock : Duration;
60    --  Holds the current clock for monotonic clock's base time
61
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.
65
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.
71
72    -----------
73    -- Clock --
74    -----------
75
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.
81
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;
89
90    begin
91       if not QueryPerformanceCounter (Current_Ticks'Access) then
92          return 0.0;
93       end if;
94
95       GetSystemTimeAsFileTime (Now'Access);
96
97       Elap_Secs_Sys :=
98         Duration (abs (Now - Base_Time) / Hundreds_Nano_In_Sec);
99
100       Elap_Secs_Tick :=
101         Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
102                   Long_Long_Float (Tick_Frequency));
103
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.
108
109       if abs (Elap_Secs_Sys - Elap_Secs_Tick) > Max_Shift then
110          Get_Base_Time;
111
112          Elap_Secs_Tick :=
113            Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
114                      Long_Long_Float (Tick_Frequency));
115       end if;
116
117       return Base_Clock + Elap_Secs_Tick;
118    end Clock;
119
120    -------------------
121    -- Get_Base_Time --
122    -------------------
123
124    procedure Get_Base_Time is
125       use System.OS_Interface;
126
127       --  The resolution for GetSystemTime is 1 millisecond.
128
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.
132
133       Max_Elapsed    : constant := 0;
134
135       Test_Now       : aliased Long_Long_Integer;
136
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;
140
141    begin
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.
145
146       loop
147          GetSystemTimeAsFileTime (Base_Time'Access);
148
149          if not QueryPerformanceCounter (Base_Ticks'Access) then
150             pragma Assert
151               (Standard.False,
152                "Could not query high performance counter in Clock");
153             null;
154          end if;
155
156          GetSystemTimeAsFileTime (Test_Now'Access);
157
158          exit when Test_Now - Base_Time = Max_Elapsed;
159       end loop;
160
161       Base_Clock := Duration
162         (Long_Long_Float ((Base_Time - epoch_1970) * system_time_ns) /
163          Long_Long_Float (Sec_Unit));
164    end Get_Base_Time;
165
166    ---------------------
167    -- Monotonic_Clock --
168    ---------------------
169
170    function Monotonic_Clock return Duration is
171       Current_Ticks  : aliased LARGE_INTEGER;
172       Elap_Secs_Tick : Duration;
173    begin
174       if not QueryPerformanceCounter (Current_Ticks'Access) then
175          return 0.0;
176       end if;
177
178       Elap_Secs_Tick :=
179         Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
180                   Long_Long_Float (Tick_Frequency));
181
182       return Base_Monotonic_Clock + Elap_Secs_Tick;
183    end Monotonic_Clock;
184
185    -----------------
186    -- Timed_Delay --
187    -----------------
188
189    procedure Timed_Delay (Time : Duration; Mode : Integer) is
190       Rel_Time   : Duration;
191       Abs_Time   : Duration;
192       Check_Time : Duration := Monotonic_Clock;
193
194    begin
195       if Mode = Relative then
196          Rel_Time := Time;
197          Abs_Time := Time + Check_Time;
198       else
199          Rel_Time := Time - Check_Time;
200          Abs_Time := Time;
201       end if;
202
203       if Rel_Time > 0.0 then
204          loop
205             Sleep (DWORD (Rel_Time * 1000.0));
206             Check_Time := Monotonic_Clock;
207
208             exit when Abs_Time <= Check_Time;
209
210             Rel_Time := Abs_Time - Check_Time;
211          end loop;
212       end if;
213    end Timed_Delay;
214
215 --  Package elaboration, get starting time as base
216
217 begin
218    if not QueryPerformanceFrequency (Tick_Frequency'Access) then
219       Ada.Exceptions.Raise_Exception
220         (Program_Error'Identity,
221          "cannot get high performance counter frequency");
222    end if;
223
224    Get_Base_Time;
225
226    Base_Monotonic_Clock := Base_Clock;
227 end System.OS_Primitives;