2002-04-02 David S. Miller <davem@redhat.com>
[official-gcc.git] / gcc / ada / 7sosprim.adb
blobfe8aeea3f824578f16e2633d7a500c7b520871ae
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 -- --
10 -- Copyright (C) 1998-2001 Free Software Foundation, Inc. --
11 -- --
12 -- GNARL is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNARL; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
29 -- --
30 -- GNARL was developed by the GNARL team at Florida State University. It is --
31 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
32 -- State University (http://www.gnat.com). --
33 -- --
34 ------------------------------------------------------------------------------
36 -- This version is for POSIX-like operating systems
38 package body System.OS_Primitives is
40 -- ??? These definitions are duplicated from System.OS_Interface
41 -- because we don't want to depend on any package. Consider removing
42 -- these declarations in System.OS_Interface and move these ones in
43 -- the spec.
45 type struct_timezone is record
46 tz_minuteswest : Integer;
47 tz_dsttime : Integer;
48 end record;
49 pragma Convention (C, struct_timezone);
50 type struct_timezone_ptr is access all struct_timezone;
52 type time_t is new Integer;
54 type struct_timeval is record
55 tv_sec : time_t;
56 tv_usec : Integer;
57 end record;
58 pragma Convention (C, struct_timeval);
60 function gettimeofday
61 (tv : access struct_timeval;
62 tz : struct_timezone_ptr) return Integer;
63 pragma Import (C, gettimeofday, "gettimeofday");
65 type timespec is record
66 tv_sec : time_t;
67 tv_nsec : Long_Integer;
68 end record;
69 pragma Convention (C, timespec);
71 function nanosleep (rqtp, rmtp : access timespec) return Integer;
72 pragma Import (C, nanosleep, "nanosleep");
74 -----------
75 -- Clock --
76 -----------
78 function Clock return Duration is
79 TV : aliased struct_timeval;
80 Result : Integer;
82 begin
83 Result := gettimeofday (TV'Access, null);
84 return Duration (TV.tv_sec) + Duration (TV.tv_usec) / 10#1#E6;
85 end Clock;
87 ---------------------
88 -- Monotonic_Clock --
89 ---------------------
91 function Monotonic_Clock return Duration renames Clock;
93 -----------------
94 -- To_Timespec --
95 -----------------
97 function To_Timespec (D : Duration) return timespec;
99 function To_Timespec (D : Duration) return timespec is
100 S : time_t;
101 F : Duration;
103 begin
104 S := time_t (Long_Long_Integer (D));
105 F := D - Duration (S);
107 -- If F has negative value due to a round-up, adjust for positive F
108 -- value.
110 if F < 0.0 then
111 S := S - 1;
112 F := F + 1.0;
113 end if;
115 return timespec' (tv_sec => S,
116 tv_nsec => Long_Integer (Long_Long_Integer (F * 10#1#E9)));
117 end To_Timespec;
119 -----------------
120 -- Timed_Delay --
121 -----------------
123 procedure Timed_Delay
124 (Time : Duration;
125 Mode : Integer)
127 Request : aliased timespec;
128 Remaind : aliased timespec;
129 Result : Integer;
130 Rel_Time : Duration;
131 Abs_Time : Duration;
132 Check_Time : Duration := Clock;
133 begin
134 if Mode = Relative then
135 Rel_Time := Time;
136 Abs_Time := Time + Check_Time;
137 else
138 Rel_Time := Time - Check_Time;
139 Abs_Time := Time;
140 end if;
142 if Rel_Time > 0.0 then
143 loop
144 Request := To_Timespec (Rel_Time);
145 Result := nanosleep (Request'Access, Remaind'Access);
146 Check_Time := Clock;
148 exit when Abs_Time <= Check_Time;
150 Rel_Time := Abs_Time - Check_Time;
151 end loop;
152 end if;
153 end Timed_Delay;
155 end System.OS_Primitives;