1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . O S _ P R I M I T I V E S --
9 -- Copyright (C) 1998-2014, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This version is for POSIX-like operating systems
34 package body System
.OS_Primitives
is
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
41 type time_t
is new Long_Integer;
43 type timespec
is record
45 tv_nsec
: Long_Integer;
47 pragma Convention
(C
, timespec
);
49 function nanosleep
(rqtp
, rmtp
: not null access timespec
) return Integer;
50 pragma Import
(C
, nanosleep
, "nanosleep");
56 function Clock
return Duration is
58 type timeval
is array (1 .. 3) of Long_Integer;
59 -- The timeval array is sized to contain Long_Long_Integer sec and
60 -- Long_Integer usec. If Long_Long_Integer'Size = Long_Integer'Size then
61 -- it will be overly large but that will not effect the implementation
62 -- since it is not accessed directly.
64 procedure timeval_to_duration
65 (T
: not null access timeval
;
66 sec
: not null access Long_Long_Integer;
67 usec
: not null access Long_Integer);
68 pragma Import
(C
, timeval_to_duration
, "__gnat_timeval_to_duration");
70 Micro
: constant := 10**6;
71 sec
: aliased Long_Long_Integer;
72 usec
: aliased Long_Integer;
75 pragma Unreferenced
(Result
);
79 Tz
: System
.Address
:= System
.Null_Address
) return Integer;
80 pragma Import
(C
, gettimeofday
, "gettimeofday");
83 -- The return codes for gettimeofday are as follows (from man pages):
84 -- EPERM settimeofday is called by someone other than the superuser
85 -- EINVAL Timezone (or something else) is invalid
86 -- EFAULT One of tv or tz pointed outside accessible address space
88 -- None of these codes signal a potential clock skew, hence the return
89 -- value is never checked.
91 Result
:= gettimeofday
(TV
'Access, System
.Null_Address
);
92 timeval_to_duration
(TV
'Access, sec
'Access, usec
'Access);
93 return Duration (sec
) + Duration (usec
) / Micro
;
100 function Monotonic_Clock
return Duration renames Clock
;
106 function To_Timespec
(D
: Duration) return timespec
;
108 function To_Timespec
(D
: Duration) return timespec
is
113 S
:= time_t
(Long_Long_Integer (D
));
114 F
:= D
- Duration (S
);
116 -- If F has negative value due to a round-up, adjust for positive F
125 timespec
'(tv_sec => S,
126 tv_nsec => Long_Integer (Long_Long_Integer (F * 10#1#E9)));
133 procedure Timed_Delay
137 Request : aliased timespec;
138 Remaind : aliased timespec;
141 Base_Time : constant Duration := Clock;
142 Check_Time : Duration := Base_Time;
145 pragma Unreferenced (Result);
148 if Mode = Relative then
150 Abs_Time := Time + Check_Time;
152 Rel_Time := Time - Check_Time;
156 if Rel_Time > 0.0 then
158 Request := To_Timespec (Rel_Time);
159 Result := nanosleep (Request'Access, Remaind'Access);
162 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
164 Rel_Time := Abs_Time - Check_Time;
173 procedure Initialize is
178 end System.OS_Primitives;