OSDN Git Service

2009-04-10 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-osprim-darwin.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-2008, 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 3,  or (at your option) any later ver- --
14 -- sion.  GNAT 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.                                     --
17 --                                                                          --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception,   --
20 -- version 3.1, as published by the Free Software Foundation.               --
21 --                                                                          --
22 -- You should have received a copy of the GNU General Public License and    --
23 -- a copy of the GCC Runtime Library Exception along with this program;     --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25 -- <http://www.gnu.org/licenses/>.                                          --
26 --                                                                          --
27 -- GNARL was developed by the GNARL team at Florida State University.       --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
29 --                                                                          --
30 ------------------------------------------------------------------------------
31
32 --  This version is for darwin
33
34 package body System.OS_Primitives is
35
36    --  ??? These definitions are duplicated from System.OS_Interface
37    --  because we don't want to depend on any package. Consider removing
38    --  these declarations in System.OS_Interface and move these ones in
39    --  the spec.
40
41    type struct_timezone is record
42       tz_minuteswest  : Integer;
43       tz_dsttime   : Integer;
44    end record;
45    pragma Convention (C, struct_timezone);
46    type struct_timezone_ptr is access all struct_timezone;
47
48    type time_t is new Long_Integer;
49
50    type struct_timeval is record
51       tv_sec       : time_t;
52       tv_usec      : Integer;
53    end record;
54    pragma Convention (C, struct_timeval);
55
56    function gettimeofday
57      (tv : not null access struct_timeval;
58       tz : struct_timezone_ptr) return Integer;
59    pragma Import (C, gettimeofday, "gettimeofday");
60
61    type timespec is record
62       tv_sec  : time_t;
63       tv_nsec : Long_Integer;
64    end record;
65    pragma Convention (C, timespec);
66
67    function nanosleep (rqtp, rmtp : not null access timespec) return Integer;
68    pragma Import (C, nanosleep, "nanosleep");
69
70    -----------
71    -- Clock --
72    -----------
73
74    function Clock return Duration is
75       TV     : aliased struct_timeval;
76
77       Result : Integer;
78       pragma Unreferenced (Result);
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
114         timespec'(tv_sec  => S,
115                   tv_nsec => Long_Integer (Long_Long_Integer (F * 10#1#E9)));
116    end To_Timespec;
117
118    -----------------
119    -- Timed_Delay --
120    -----------------
121
122    procedure Timed_Delay
123      (Time : Duration;
124       Mode : Integer)
125    is
126       Request    : aliased timespec;
127       Remaind    : aliased timespec;
128       Rel_Time   : Duration;
129       Abs_Time   : Duration;
130       Base_Time  : constant Duration := Clock;
131       Check_Time : Duration := Base_Time;
132
133       Result : Integer;
134       pragma Unreferenced (Result);
135
136    begin
137       if Mode = Relative then
138          Rel_Time := Time;
139          Abs_Time := Time + Check_Time;
140       else
141          Rel_Time := Time - Check_Time;
142          Abs_Time := Time;
143       end if;
144
145       if Rel_Time > 0.0 then
146          loop
147             Request := To_Timespec (Rel_Time);
148             Result := nanosleep (Request'Access, Remaind'Access);
149             Check_Time := Clock;
150
151             exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
152
153             Rel_Time := Abs_Time - Check_Time;
154          end loop;
155       end if;
156    end Timed_Delay;
157
158    ----------------
159    -- Initialize --
160    ----------------
161
162    procedure Initialize is
163    begin
164       null;
165    end Initialize;
166
167 end System.OS_Primitives;