OSDN Git Service

* 1aexcept.adb, 1aexcept.ads, 1ic.ads, 1ssecsta.adb,
[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 --          Copyright (C) 1998-2001 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 2,  or (at your option) any later ver- --
14 -- sion. GNARL 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.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNARL was developed by the GNARL team at Florida State University.       --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  This version is for POSIX-like operating systems
35
36 package body System.OS_Primitives is
37
38    --  ??? These definitions are duplicated from System.OS_Interface
39    --  because we don't want to depend on any package. Consider removing
40    --  these declarations in System.OS_Interface and move these ones in
41    --  the spec.
42
43    type struct_timezone is record
44       tz_minuteswest  : Integer;
45       tz_dsttime   : Integer;
46    end record;
47    pragma Convention (C, struct_timezone);
48    type struct_timezone_ptr is access all struct_timezone;
49
50    type time_t is new Integer;
51
52    type struct_timeval is record
53       tv_sec       : time_t;
54       tv_usec      : Integer;
55    end record;
56    pragma Convention (C, struct_timeval);
57
58    function gettimeofday
59      (tv : access struct_timeval;
60       tz : struct_timezone_ptr) return Integer;
61    pragma Import (C, gettimeofday, "gettimeofday");
62
63    type timespec is record
64       tv_sec  : time_t;
65       tv_nsec : Long_Integer;
66    end record;
67    pragma Convention (C, timespec);
68
69    function nanosleep (rqtp, rmtp : access timespec) return Integer;
70    pragma Import (C, nanosleep, "nanosleep");
71
72    -----------
73    -- Clock --
74    -----------
75
76    function Clock return Duration is
77       TV     : aliased struct_timeval;
78       Result : Integer;
79
80    begin
81       Result := gettimeofday (TV'Access, null);
82       return Duration (TV.tv_sec) + Duration (TV.tv_usec) / 10#1#E6;
83    end Clock;
84
85    ---------------------
86    -- Monotonic_Clock --
87    ---------------------
88
89    function Monotonic_Clock return Duration renames Clock;
90
91    -----------------
92    -- To_Timespec --
93    -----------------
94
95    function To_Timespec (D : Duration) return timespec;
96
97    function To_Timespec (D : Duration) return timespec is
98       S : time_t;
99       F : Duration;
100
101    begin
102       S := time_t (Long_Long_Integer (D));
103       F := D - Duration (S);
104
105       --  If F has negative value due to a round-up, adjust for positive F
106       --  value.
107
108       if F < 0.0 then
109          S := S - 1;
110          F := F + 1.0;
111       end if;
112
113       return timespec' (tv_sec => S,
114         tv_nsec => Long_Integer (Long_Long_Integer (F * 10#1#E9)));
115    end To_Timespec;
116
117    -----------------
118    -- Timed_Delay --
119    -----------------
120
121    procedure Timed_Delay
122      (Time : Duration;
123       Mode : Integer)
124    is
125       Request : aliased timespec;
126       Remaind : aliased timespec;
127       Result  : Integer;
128       Rel_Time : Duration;
129       Abs_Time : Duration;
130       Check_Time : Duration := Clock;
131    begin
132       if Mode = Relative then
133          Rel_Time := Time;
134          Abs_Time := Time + Check_Time;
135       else
136          Rel_Time := Time - Check_Time;
137          Abs_Time := Time;
138       end if;
139
140       if Rel_Time > 0.0 then
141          loop
142             Request := To_Timespec (Rel_Time);
143             Result := nanosleep (Request'Access, Remaind'Access);
144             Check_Time := Clock;
145
146             exit when Abs_Time <= Check_Time;
147
148             Rel_Time := Abs_Time - Check_Time;
149          end loop;
150       end if;
151    end Timed_Delay;
152
153 end System.OS_Primitives;