OSDN Git Service

* expr.c (expand_expr): Use unsave lang hook.
[pf3gnuchains/gcc-fork.git] / gcc / ada / 5zosprim.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 --                             $Revision: 1.7 $
10 --                                                                          --
11 --          Copyright (C) 1998-2001 Free Software Foundation, Inc.          --
12 --                                                                          --
13 -- GNARL is free software; you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- As a special exception,  if other files  instantiate  generics from this --
25 -- unit, or you link  this unit with other files  to produce an executable, --
26 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
27 -- covered  by the  GNU  General  Public  License.  This exception does not --
28 -- however invalidate  any other reasons why  the executable file  might be --
29 -- covered by the  GNU Public License.                                      --
30 --                                                                          --
31 -- GNARL was developed by the GNARL team at Florida State University. It is --
32 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
33 -- State University (http://www.gnat.com).                                  --
34 --                                                                          --
35 ------------------------------------------------------------------------------
36
37 --  This version is for VxWorks targets
38
39 with System.OS_Interface;
40 --  Since the thread library is part of the VxWorks kernel, using OS_Interface
41 --  is not a problem here, as long as we only use System.OS_Interface as a
42 --  set of C imported routines: using Ada routines from this package would
43 --  create a dependency on libgnarl in libgnat, which is not desirable.
44
45 with Interfaces.C;
46 --  used for type int
47
48 package body System.OS_Primitives is
49
50    use System.OS_Interface;
51
52    --------------------------
53    --  Internal functions  --
54    --------------------------
55
56    function To_Clock_Ticks (D : Duration) return int;
57    --  Convert a duration value (in seconds) into clock ticks.
58    --  Note that this routine is duplicated from System.OS_Interface since
59    --  as explained above, we do not want to depend on libgnarl
60
61    function To_Clock_Ticks (D : Duration) return int is
62       Ticks          : Long_Long_Integer;
63       Rate_Duration  : Duration;
64       Ticks_Duration : Duration;
65    begin
66       --  Ensure that the duration can be converted to ticks
67       --  at the current clock tick rate without overflowing.
68
69       Rate_Duration := Duration (sysClkRateGet);
70
71       if D > (Duration'Last / Rate_Duration) then
72          Ticks := Long_Long_Integer (int'Last);
73       else
74          --  We always want to round up to the nearest clock tick.
75
76          Ticks_Duration := D * Rate_Duration;
77          Ticks := Long_Long_Integer (Ticks_Duration);
78
79          if Ticks_Duration > Duration (Ticks) then
80             Ticks := Ticks + 1;
81          end if;
82
83          if Ticks > Long_Long_Integer (int'Last) then
84             Ticks := Long_Long_Integer (int'Last);
85          end if;
86       end if;
87
88       return int (Ticks);
89    end To_Clock_Ticks;
90
91    -----------
92    -- Clock --
93    -----------
94
95    function Clock return Duration is
96       TS     : aliased timespec;
97       Result : int;
98
99       use type Interfaces.C.int;
100    begin
101       Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
102       pragma Assert (Result = 0);
103       return Duration (TS.ts_sec) + Duration (TS.ts_nsec) / 10#1#E9;
104    end Clock;
105
106    ---------------------
107    -- Monotonic_Clock --
108    ---------------------
109
110    function Monotonic_Clock return Duration renames Clock;
111
112    -----------------
113    -- Timed_Delay --
114    -----------------
115
116    procedure Timed_Delay
117      (Time : Duration;
118       Mode : Integer)
119    is
120       Result     : int;
121       Rel_Time   : Duration;
122       Abs_Time   : Duration;
123       Check_Time : Duration := Clock;
124
125    begin
126       if Mode = Relative then
127          Rel_Time := Time;
128          Abs_Time := Time + Check_Time;
129       else
130          Rel_Time := Time - Check_Time;
131          Abs_Time := Time;
132       end if;
133
134       if Rel_Time > 0.0 then
135          loop
136             Result := taskDelay (To_Clock_Ticks (Rel_Time));
137             Check_Time := Clock;
138
139             exit when Abs_Time <= Check_Time;
140
141             Rel_Time := Abs_Time - Check_Time;
142          end loop;
143       end if;
144    end Timed_Delay;
145
146 end System.OS_Primitives;