OSDN Git Service

2005-11-21 Joel Sherrill <joel.sherrill@oarcorp.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-osprim-posix.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                 GNAT 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-2005 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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, 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 Long_Integer;
51
52    type struct_timeval is record
53       tv_sec       : time_t;
54       tv_usec      : Long_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
79       Result : Integer;
80       pragma Unreferenced (Result);
81
82    begin
83       Result := gettimeofday (TV'Access, null);
84       return Duration (TV.tv_sec) + Duration (TV.tv_usec) / 10#1#E6;
85    end Clock;
86
87    ---------------------
88    -- Monotonic_Clock --
89    ---------------------
90
91    function Monotonic_Clock return Duration renames Clock;
92
93    -----------------
94    -- To_Timespec --
95    -----------------
96
97    function To_Timespec (D : Duration) return timespec;
98
99    function To_Timespec (D : Duration) return timespec is
100       S : time_t;
101       F : Duration;
102
103    begin
104       S := time_t (Long_Long_Integer (D));
105       F := D - Duration (S);
106
107       --  If F has negative value due to a round-up, adjust for positive F
108       --  value.
109
110       if F < 0.0 then
111          S := S - 1;
112          F := F + 1.0;
113       end if;
114
115       return
116         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       Rel_Time : Duration;
131       Abs_Time : Duration;
132       Check_Time : Duration := Clock;
133
134       Result : Integer;
135       pragma Unreferenced (Result);
136
137    begin
138       if Mode = Relative then
139          Rel_Time := Time;
140          Abs_Time := Time + Check_Time;
141       else
142          Rel_Time := Time - Check_Time;
143          Abs_Time := Time;
144       end if;
145
146       if Rel_Time > 0.0 then
147          loop
148             Request := To_Timespec (Rel_Time);
149             Result := nanosleep (Request'Access, Remaind'Access);
150             Check_Time := Clock;
151
152             exit when Abs_Time <= Check_Time;
153
154             Rel_Time := Abs_Time - Check_Time;
155          end loop;
156       end if;
157    end Timed_Delay;
158
159    ----------------
160    -- Initialize --
161    ----------------
162
163    procedure Initialize is
164    begin
165       null;
166    end Initialize;
167
168 end System.OS_Primitives;