OSDN Git Service

Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-osprim-mingw.adb
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-2009, 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 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.                                     --
17 --                                                                          --
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.               --
21 --                                                                          --
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/>.                                          --
26 --                                                                          --
27 -- GNARL was developed by the GNARL team at Florida State University.       --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
29 --                                                                          --
30 ------------------------------------------------------------------------------
31
32 --  This is the NT version of this package
33
34 with System.Win32.Ext;
35
36 package body System.OS_Primitives is
37
38    use System.Win32;
39    use System.Win32.Ext;
40
41    ----------------------------------------
42    -- Data for the high resolution clock --
43    ----------------------------------------
44
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
51    --  penalties.
52
53    --  Note that access variables below must not be declared as constant
54    --  otherwise the compiler optimization will remove this indirect access.
55
56    type DA is access all Duration;
57    --  Use to have indirect access to multi-word variables
58
59    type LIA is access all LARGE_INTEGER;
60    --  Use to have indirect access to multi-word variables
61
62    type LLIA is access all Long_Long_Integer;
63    --  Use to have indirect access to multi-word variables
64
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.
69
70    Base_Ticks : aliased LARGE_INTEGER;
71    BTA : constant LIA := Base_Ticks'Access;
72    --  Holds the Tick count for the base time
73
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
77
78    Base_Clock : aliased Duration;
79    BCA : constant DA := Base_Clock'Access;
80    --  Holds the current clock for the standard clock's base time
81
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
85
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.
90
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.
97
98    -----------
99    -- Clock --
100    -----------
101
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.
107
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;
115
116    begin
117       if QueryPerformanceCounter (Current_Ticks'Access) = Win32.FALSE then
118          return 0.0;
119       end if;
120
121       GetSystemTimeAsFileTime (Now'Access);
122
123       Elap_Secs_Sys :=
124         Duration (Long_Long_Float (abs (Now - BTiA.all)) /
125                     Hundreds_Nano_In_Sec);
126
127       Elap_Secs_Tick :=
128         Duration (Long_Long_Float (Current_Ticks - BTA.all) /
129                   Long_Long_Float (TFA.all));
130
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.
135
136       if abs (Elap_Secs_Sys - Elap_Secs_Tick) > Max_Shift then
137          Get_Base_Time;
138
139          Elap_Secs_Tick :=
140            Duration (Long_Long_Float (Current_Ticks - BTA.all) /
141                      Long_Long_Float (TFA.all));
142       end if;
143
144       return BCA.all + Elap_Secs_Tick;
145    end Clock;
146
147    -------------------
148    -- Get_Base_Time --
149    -------------------
150
151    procedure Get_Base_Time is
152
153       --  The resolution for GetSystemTime is 1 millisecond
154
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.
158
159       Max_Elapsed : constant := 0;
160
161       Test_Now : aliased Long_Long_Integer;
162
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;
166
167    begin
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.
171
172       loop
173          GetSystemTimeAsFileTime (Base_Time'Access);
174
175          if QueryPerformanceCounter (Base_Ticks'Access) = Win32.FALSE then
176             pragma Assert
177               (Standard.False,
178                "Could not query high performance counter in Clock");
179             null;
180          end if;
181
182          GetSystemTimeAsFileTime (Test_Now'Access);
183
184          exit when Test_Now - Base_Time = Max_Elapsed;
185       end loop;
186
187       Base_Clock := Duration
188         (Long_Long_Float ((Base_Time - epoch_1970) * system_time_ns) /
189          Long_Long_Float (Sec_Unit));
190    end Get_Base_Time;
191
192    ---------------------
193    -- Monotonic_Clock --
194    ---------------------
195
196    function Monotonic_Clock return Duration is
197       Current_Ticks  : aliased LARGE_INTEGER;
198       Elap_Secs_Tick : Duration;
199
200    begin
201       if QueryPerformanceCounter (Current_Ticks'Access) = Win32.FALSE then
202          return 0.0;
203       end if;
204
205       Elap_Secs_Tick :=
206         Duration (Long_Long_Float (Current_Ticks - BMTA.all) /
207                   Long_Long_Float (TFA.all));
208
209       return BMCA.all + Elap_Secs_Tick;
210    end Monotonic_Clock;
211
212    -----------------
213    -- Timed_Delay --
214    -----------------
215
216    procedure Timed_Delay (Time : Duration; Mode : Integer) is
217
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.
222
223       ----------------
224       -- Mode_Clock --
225       ----------------
226
227       function Mode_Clock return Duration is
228       begin
229          case Mode is
230             when Absolute_RT =>
231                return Monotonic_Clock;
232             when others =>
233                return Clock;
234          end case;
235       end Mode_Clock;
236
237       --  Local Variables
238
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.
242
243       Rel_Time   : Duration;
244       Abs_Time   : Duration;
245       Check_Time : Duration := Base_Time;
246
247    --  Start of processing for Timed Delay
248
249    begin
250       if Mode = Relative then
251          Rel_Time := Time;
252          Abs_Time := Time + Check_Time;
253       else
254          Rel_Time := Time - Check_Time;
255          Abs_Time := Time;
256       end if;
257
258       if Rel_Time > 0.0 then
259          loop
260             Sleep (DWORD (Rel_Time * 1000.0));
261             Check_Time := Mode_Clock;
262
263             exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
264
265             Rel_Time := Abs_Time - Check_Time;
266          end loop;
267       end if;
268    end Timed_Delay;
269
270    ----------------
271    -- Initialize --
272    ----------------
273
274    Initialized : Boolean := False;
275
276    procedure Initialize is
277    begin
278       if Initialized then
279          return;
280       end if;
281
282       Initialized := True;
283
284       --  Get starting time as base
285
286       if QueryPerformanceFrequency (Tick_Frequency'Access) = Win32.FALSE then
287          raise Program_Error with
288            "cannot get high performance counter frequency";
289       end if;
290
291       Get_Base_Time;
292
293       --  Keep base clock and ticks for the monotonic clock. These values
294       --  should never be changed to ensure proper behavior of the monotonic
295       --  clock.
296
297       Base_Monotonic_Clock := Base_Clock;
298       Base_Monotonic_Ticks := Base_Ticks;
299    end Initialize;
300
301 end System.OS_Primitives;