OSDN Git Service

* rtl.h (mem_attrs): Rename decl to expr; adjust all users.
[pf3gnuchains/gcc-fork.git] / gcc / ada / 5posprim.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.8 $
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 uses gettimeofday and select
38 --  Currently OpenNT, Dec Unix, Solaris and SCO UnixWare use this file.
39
40 package body System.OS_Primitives is
41
42    --  ??? These definitions are duplicated from System.OS_Interface
43    --  because we don't want to depend on any package. Consider removing
44    --  these declarations in System.OS_Interface and move these ones in
45    --  the spec.
46
47    type struct_timezone is record
48       tz_minuteswest  : Integer;
49       tz_dsttime   : Integer;
50    end record;
51    pragma Convention (C, struct_timezone);
52    type struct_timezone_ptr is access all struct_timezone;
53
54    type struct_timeval is record
55       tv_sec       : Integer;
56       tv_usec      : Integer;
57    end record;
58    pragma Convention (C, struct_timeval);
59
60    function gettimeofday
61      (tv : access struct_timeval;
62       tz : struct_timezone_ptr) return Integer;
63    pragma Import (C, gettimeofday, "gettimeofday");
64
65    type fd_set is null record;
66    type fd_set_ptr is access all fd_set;
67
68    function C_select
69      (n         : Integer    := 0;
70       readfds,
71       writefds,
72       exceptfds : fd_set_ptr := null;
73       timeout   : access struct_timeval) return Integer;
74    pragma Import (C, C_select, "select");
75
76    -----------
77    -- Clock --
78    -----------
79
80    function Clock return Duration is
81       TV     : aliased struct_timeval;
82       Result : Integer;
83
84    begin
85       Result := gettimeofday (TV'Access, null);
86       return Duration (TV.tv_sec) + Duration (TV.tv_usec) / 10#1#E6;
87    end Clock;
88
89    ---------------------
90    -- Monotonic_Clock --
91    ---------------------
92
93    function Monotonic_Clock return Duration renames Clock;
94
95    -----------------
96    -- Timed_Delay --
97    -----------------
98
99    procedure Timed_Delay
100      (Time : Duration;
101       Mode : Integer)
102    is
103       Result     : Integer;
104       Rel_Time   : Duration;
105       Abs_Time   : Duration;
106       Check_Time : Duration := Clock;
107       timeval    : aliased struct_timeval;
108
109    begin
110       if Mode = Relative then
111          Rel_Time := Time;
112          Abs_Time := Time + Check_Time;
113       else
114          Rel_Time := Time - Check_Time;
115          Abs_Time := Time;
116       end if;
117
118       if Rel_Time > 0.0 then
119          loop
120             timeval.tv_sec := Integer (Rel_Time);
121
122             if Duration (timeval.tv_sec) > Rel_Time then
123                timeval.tv_sec := timeval.tv_sec - 1;
124             end if;
125
126             timeval.tv_usec :=
127               Integer ((Rel_Time - Duration (timeval.tv_sec)) * 10#1#E6);
128
129             Result := C_select (timeout => timeval'Unchecked_Access);
130             Check_Time := Clock;
131
132             exit when Abs_Time <= Check_Time;
133
134             Rel_Time := Abs_Time - Check_Time;
135          end loop;
136       end if;
137    end Timed_Delay;
138
139 end System.OS_Primitives;