Add an UNSPEC_PROLOGUE_USE to prevent the link register from being considered dead.
[official-gcc.git] / gcc / ada / 5wosprim.adb
blobb4279a02751ca7b5b19bfafbf245fea4bcbb97dd
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. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 -- --
33 ------------------------------------------------------------------------------
35 -- This is the NT version of this package
37 with Ada.Exceptions;
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.
71 -----------
72 -- Clock --
73 -----------
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;
89 begin
90 if not QueryPerformanceCounter (Current_Ticks'Access) then
91 return 0.0;
92 end if;
94 GetSystemTimeAsFileTime (Now'Access);
96 Elap_Secs_Sys :=
97 Duration (abs (Now - Base_Time) / Hundreds_Nano_In_Sec);
99 Elap_Secs_Tick :=
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
109 Get_Base_Time;
111 Elap_Secs_Tick :=
112 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
113 Long_Long_Float (Tick_Frequency));
114 end if;
116 return Base_Clock + Elap_Secs_Tick;
117 end Clock;
119 -------------------
120 -- Get_Base_Time --
121 -------------------
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;
140 begin
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.
145 loop
146 GetSystemTimeAsFileTime (Base_Time'Access);
148 if not QueryPerformanceCounter (Base_Ticks'Access) then
149 pragma Assert
150 (Standard.False,
151 "Could not query high performance counter in Clock");
152 null;
153 end if;
155 GetSystemTimeAsFileTime (Test_Now'Access);
157 exit when Test_Now - Base_Time = Max_Elapsed;
158 end loop;
160 Base_Clock := Duration
161 (Long_Long_Float ((Base_Time - epoch_1970) * system_time_ns) /
162 Long_Long_Float (Sec_Unit));
163 end Get_Base_Time;
165 ---------------------
166 -- Monotonic_Clock --
167 ---------------------
169 function Monotonic_Clock return Duration is
170 Current_Ticks : aliased LARGE_INTEGER;
171 Elap_Secs_Tick : Duration;
172 begin
173 if not QueryPerformanceCounter (Current_Ticks'Access) then
174 return 0.0;
175 end if;
177 Elap_Secs_Tick :=
178 Duration (Long_Long_Float (Current_Ticks - Base_Ticks) /
179 Long_Long_Float (Tick_Frequency));
181 return Base_Monotonic_Clock + Elap_Secs_Tick;
182 end Monotonic_Clock;
184 -----------------
185 -- Timed_Delay --
186 -----------------
188 procedure Timed_Delay (Time : Duration; Mode : Integer) is
189 Rel_Time : Duration;
190 Abs_Time : Duration;
191 Check_Time : Duration := Monotonic_Clock;
193 begin
194 if Mode = Relative then
195 Rel_Time := Time;
196 Abs_Time := Time + Check_Time;
197 else
198 Rel_Time := Time - Check_Time;
199 Abs_Time := Time;
200 end if;
202 if Rel_Time > 0.0 then
203 loop
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;
210 end loop;
211 end if;
212 end Timed_Delay;
214 -- Package elaboration, get starting time as base
216 begin
217 if not QueryPerformanceFrequency (Tick_Frequency'Access) then
218 Ada.Exceptions.Raise_Exception
219 (Program_Error'Identity,
220 "cannot get high performance counter frequency");
221 end if;
223 Get_Base_Time;
225 Base_Monotonic_Clock := Base_Clock;
226 end System.OS_Primitives;