OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / 5oosprim.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.       --
31 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 --  This is the OS/2 version of this package
36
37 with Interfaces.C;                      use Interfaces.C;
38 with Interfaces.OS2Lib;                 use Interfaces.OS2Lib;
39 with Interfaces.OS2Lib.Synchronization; use Interfaces.OS2Lib.Synchronization;
40
41 package body System.OS_Primitives is
42
43    ----------------
44    -- Local Data --
45    ----------------
46
47    Epoch_Offset    : Duration;       --  See Set_Epoch_Offset
48    Max_Tick_Count  : QWORD := 0.0;
49    --  This is needed to compensate for small glitches in the
50    --  hardware clock or the way it is read by the OS
51
52    -----------------------
53    -- Local Subprograms --
54    -----------------------
55
56    procedure Set_Epoch_Offset;
57    --  Initializes the Epoch_1970_Offset to the offset of the System_Clock
58    --  relative to the Unix epoch (Jan 1, 1970), such that
59    --     Clock = System_Clock + Epoch_1970_Offset
60
61    function System_Clock return Duration;
62    pragma Inline (System_Clock);
63    --  Function returning value of system clock with system-dependent timebase.
64    --  For OS/2 the system clock returns the elapsed time since system boot.
65    --  The clock resolution is approximately 838 ns.
66
67    ------------------
68    -- System_Clock --
69    ------------------
70
71    function System_Clock return Duration is
72
73       --  Implement conversion from tick count to Duration
74       --  using fixed point arithmetic. The frequency of
75       --  the Intel 8254 timer chip is 18.2 * 2**16 Hz.
76
77       Tick_Duration : constant := 1.0 / (18.2 * 2**16);
78       Tick_Count    : aliased QWORD;
79
80    begin
81       Must_Not_Fail (DosTmrQueryTime (Tick_Count'Access));
82       --  Read nr of clock ticks since boot time
83
84       Max_Tick_Count := QWORD'Max (Tick_Count, Max_Tick_Count);
85
86       return Max_Tick_Count * Tick_Duration;
87    end System_Clock;
88
89    -----------
90    -- Clock --
91    -----------
92
93    function Clock return Duration is
94    begin
95       return System_Clock + Epoch_Offset;
96    end Clock;
97
98    ---------------------
99    -- Monotonic_Clock --
100    ---------------------
101
102    function Monotonic_Clock return Duration renames Clock;
103
104    ----------------------
105    -- Set_Epoch_Offset --
106    ----------------------
107
108    procedure Set_Epoch_Offset is
109
110       --  Interface to Unix C style gettimeofday
111
112       type timeval is record
113          tv_sec  : long;
114          tv_usec : long;
115       end record;
116
117       procedure gettimeofday
118         (time : access timeval;
119          zone : System.Address := System.Address'Null_Parameter);
120       pragma Import (C, gettimeofday);
121
122       Time_Of_Day       : aliased timeval;
123       Micro_To_Nano     : constant := 1.0E3;
124       Sec_To_Nano       : constant := 1.0E9;
125       Nanos_Since_Epoch : QWORD;
126
127    begin
128       gettimeofday (Time_Of_Day'Access);
129       Nanos_Since_Epoch := QWORD (Time_Of_Day.tv_sec) * Sec_To_Nano
130         + QWORD (Time_Of_Day.tv_usec) * Micro_To_Nano;
131
132       Epoch_Offset :=
133          Duration'(Nanos_Since_Epoch / Sec_To_Nano) - System_Clock;
134
135    end Set_Epoch_Offset;
136
137    -----------------
138    -- Timed_Delay --
139    -----------------
140
141    procedure Timed_Delay
142      (Time : Duration;
143       Mode : Integer)
144    is
145       Rel_Time   : Duration;
146       Abs_Time   : Duration;
147       Check_Time : Duration := Clock;
148
149    begin
150       if Mode = Relative then
151          Rel_Time := Time;
152          Abs_Time := Time + Check_Time;
153       else
154          Rel_Time := Time - Check_Time;
155          Abs_Time := Time;
156       end if;
157
158       if Rel_Time > 0.0 then
159          loop
160             Must_Not_Fail (DosSleep (ULONG (Rel_Time * 1000.0)));
161
162             Check_Time := Clock;
163
164             exit when Abs_Time <= Check_Time;
165
166             Rel_Time := Abs_Time - Check_Time;
167          end loop;
168       end if;
169    end Timed_Delay;
170
171 begin
172    Set_Epoch_Offset;
173 end System.OS_Primitives;