OSDN Git Service

* sem_ch3.adb (Find_Type_Of_Subtype_Indic): If subtype indication
[pf3gnuchains/gcc-fork.git] / gcc / ada / 7sosprim.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.4 $                             --
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 POSIX-like operating systems
38
39 package body System.OS_Primitives is
40
41    --  ??? These definitions are duplicated from System.OS_Interface
42    --  because we don't want to depend on any package. Consider removing
43    --  these declarations in System.OS_Interface and move these ones in
44    --  the spec.
45
46    type struct_timezone is record
47       tz_minuteswest  : Integer;
48       tz_dsttime   : Integer;
49    end record;
50    pragma Convention (C, struct_timezone);
51    type struct_timezone_ptr is access all struct_timezone;
52
53    type time_t is new Integer;
54
55    type struct_timeval is record
56       tv_sec       : time_t;
57       tv_usec      : Integer;
58    end record;
59    pragma Convention (C, struct_timeval);
60
61    function gettimeofday
62      (tv : access struct_timeval;
63       tz : struct_timezone_ptr) return Integer;
64    pragma Import (C, gettimeofday, "gettimeofday");
65
66    type timespec is record
67       tv_sec  : time_t;
68       tv_nsec : Long_Integer;
69    end record;
70    pragma Convention (C, timespec);
71
72    function nanosleep (rqtp, rmtp : access timespec) return Integer;
73    pragma Import (C, nanosleep, "nanosleep");
74
75    -----------
76    -- Clock --
77    -----------
78
79    function Clock return Duration is
80       TV     : aliased struct_timeval;
81       Result : Integer;
82
83    begin
84       Result := gettimeofday (TV'Access, null);
85       return Duration (TV.tv_sec) + Duration (TV.tv_usec) / 10#1#E6;
86    end Clock;
87
88    ---------------------
89    -- Monotonic_Clock --
90    ---------------------
91
92    function Monotonic_Clock return Duration renames Clock;
93
94    -----------------
95    -- To_Timespec --
96    -----------------
97
98    function To_Timespec (D : Duration) return timespec;
99
100    function To_Timespec (D : Duration) return timespec is
101       S : time_t;
102       F : Duration;
103
104    begin
105       S := time_t (Long_Long_Integer (D));
106       F := D - Duration (S);
107
108       --  If F has negative value due to a round-up, adjust for positive F
109       --  value.
110
111       if F < 0.0 then
112          S := S - 1;
113          F := F + 1.0;
114       end if;
115
116       return timespec' (tv_sec => S,
117         tv_nsec => Long_Integer (Long_Long_Integer (F * 10#1#E9)));
118    end To_Timespec;
119
120    -----------------
121    -- Timed_Delay --
122    -----------------
123
124    procedure Timed_Delay
125      (Time : Duration;
126       Mode : Integer)
127    is
128       Request : aliased timespec;
129       Remaind : aliased timespec;
130       Result  : Integer;
131       Rel_Time : Duration;
132       Abs_Time : Duration;
133       Check_Time : Duration := Clock;
134    begin
135       if Mode = Relative then
136          Rel_Time := Time;
137          Abs_Time := Time + Check_Time;
138       else
139          Rel_Time := Time - Check_Time;
140          Abs_Time := Time;
141       end if;
142
143       if Rel_Time > 0.0 then
144          loop
145             Request := To_Timespec (Rel_Time);
146             Result := nanosleep (Request'Access, Remaind'Access);
147             Check_Time := Clock;
148
149             exit when Abs_Time <= Check_Time;
150
151             Rel_Time := Abs_Time - Check_Time;
152          end loop;
153       end if;
154    end Timed_Delay;
155
156 end System.OS_Primitives;