OSDN Git Service

2009-02-27 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-osprim-unix.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-2007, 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 uses gettimeofday and select
35 --  This file is suitable for OpenNT, Dec Unix and SCO UnixWare.
36
37 package body System.OS_Primitives is
38
39    --  ??? These definitions are duplicated from System.OS_Interface
40    --  because we don't want to depend on any package. Consider removing
41    --  these declarations in System.OS_Interface and move these ones in
42    --  the spec.
43
44    type struct_timeval is record
45       tv_sec  : Integer;
46       tv_usec : Integer;
47    end record;
48    pragma Convention (C, struct_timeval);
49
50    procedure gettimeofday
51      (tv : not null access struct_timeval;
52       tz : Address := Null_Address);
53    pragma Import (C, gettimeofday, "gettimeofday");
54
55    procedure C_select
56      (n         : Integer := 0;
57       readfds,
58       writefds,
59       exceptfds : Address := Null_Address;
60       timeout   : not null access struct_timeval);
61    pragma Import (C, C_select, "select");
62
63    -----------
64    -- Clock --
65    -----------
66
67    function Clock return Duration is
68       TV : aliased struct_timeval;
69
70    begin
71       gettimeofday (TV'Access);
72       return Duration (TV.tv_sec) + Duration (TV.tv_usec) / 10#1#E6;
73    end Clock;
74
75    ---------------------
76    -- Monotonic_Clock --
77    ---------------------
78
79    function Monotonic_Clock return Duration renames Clock;
80
81    -----------------
82    -- Timed_Delay --
83    -----------------
84
85    procedure Timed_Delay
86      (Time : Duration;
87       Mode : Integer)
88    is
89       Rel_Time   : Duration;
90       Abs_Time   : Duration;
91       Base_Time  : constant Duration := Clock;
92       Check_Time : Duration := Base_Time;
93       timeval    : aliased struct_timeval;
94
95    begin
96       if Mode = Relative then
97          Rel_Time := Time;
98          Abs_Time := Time + Check_Time;
99       else
100          Rel_Time := Time - Check_Time;
101          Abs_Time := Time;
102       end if;
103
104       if Rel_Time > 0.0 then
105          loop
106             timeval.tv_sec := Integer (Rel_Time);
107
108             if Duration (timeval.tv_sec) > Rel_Time then
109                timeval.tv_sec := timeval.tv_sec - 1;
110             end if;
111
112             timeval.tv_usec :=
113               Integer ((Rel_Time - Duration (timeval.tv_sec)) * 10#1#E6);
114
115             C_select (timeout => timeval'Unchecked_Access);
116             Check_Time := Clock;
117
118             exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
119
120             Rel_Time := Abs_Time - Check_Time;
121          end loop;
122       end if;
123    end Timed_Delay;
124
125    ----------------
126    -- Initialize --
127    ----------------
128
129    procedure Initialize is
130    begin
131       null;
132    end Initialize;
133
134 end System.OS_Primitives;