| 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-2025, 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 POSIX-like operating systems |
| 33 |
|
| 34 |
with System.Parameters; |
| 35 |
package body System.OS_Primitives is |
| 36 |
|
| 37 |
-- ??? These definitions are duplicated from System.OS_Interface |
| 38 |
-- because we don't want to depend on any package. Consider removing |
| 39 |
-- these declarations in System.OS_Interface and move these ones in |
| 40 |
-- the spec. |
| 41 |
|
| 42 |
type time_t is range -2 ** (System.Parameters.time_t_bits - 1) |
| 43 |
.. 2 ** (System.Parameters.time_t_bits - 1) - 1; |
| 44 |
|
| 45 |
type timespec is record |
| 46 |
tv_sec : time_t; |
| 47 |
tv_nsec : Long_Integer; |
| 48 |
end record; |
| 49 |
pragma Convention (C, timespec); |
| 50 |
|
| 51 |
function nanosleep (rqtp, rmtp : not null access timespec) return Integer; |
| 52 |
pragma Import (C, nanosleep, "nanosleep"); |
| 53 |
|
| 54 |
----------- |
| 55 |
-- Clock -- |
| 56 |
----------- |
| 57 |
|
| 58 |
function Clock return Duration is |
| 59 |
|
| 60 |
type timeval is record |
| 61 |
tv_sec : time_t; |
| 62 |
tv_usec : Long_Integer; |
| 63 |
end record; |
| 64 |
pragma Convention (C, timeval); |
| 65 |
|
| 66 |
procedure timeval_to_duration |
| 67 |
(T : not null access timeval; |
| 68 |
sec : not null access Long_Long_Integer; |
| 69 |
usec : not null access Long_Integer); |
| 70 |
pragma Import (C, timeval_to_duration, "__gnat_timeval_to_duration"); |
| 71 |
|
| 72 |
Micro : constant := 10**6; |
| 73 |
sec : aliased Long_Long_Integer; |
| 74 |
usec : aliased Long_Integer; |
| 75 |
TV : aliased timeval; |
| 76 |
Result : Integer; |
| 77 |
pragma Unreferenced (Result); |
| 78 |
|
| 79 |
function gettimeofday |
| 80 |
(Tv : access timeval; |
| 81 |
Tz : System.Address := System.Null_Address) return Integer; |
| 82 |
pragma Import (C, gettimeofday, "gettimeofday"); |
| 83 |
|
| 84 |
begin |
| 85 |
-- The return codes for gettimeofday are as follows (from man pages): |
| 86 |
-- EPERM settimeofday is called by someone other than the superuser |
| 87 |
-- EINVAL Timezone (or something else) is invalid |
| 88 |
-- EFAULT One of tv or tz pointed outside accessible address space |
| 89 |
|
| 90 |
-- None of these codes signal a potential clock skew, hence the return |
| 91 |
-- value is never checked. |
| 92 |
|
| 93 |
Result := gettimeofday (TV'Access, System.Null_Address); |
| 94 |
timeval_to_duration (TV'Access, sec'Access, usec'Access); |
| 95 |
return Duration (sec) + Duration (usec) / Micro; |
| 96 |
end Clock; |
| 97 |
|
| 98 |
----------------- |
| 99 |
-- To_Timespec -- |
| 100 |
----------------- |
| 101 |
|
| 102 |
function To_Timespec (D : Duration) return timespec; |
| 103 |
|
| 104 |
function To_Timespec (D : Duration) return timespec is |
| 105 |
S : time_t; |
| 106 |
F : Duration; |
| 107 |
|
| 108 |
begin |
| 109 |
S := time_t (Long_Long_Integer (D)); |
| 110 |
F := D - Duration (S); |
| 111 |
|
| 112 |
-- If F has negative value due to a round-up, adjust for positive F |
| 113 |
-- value. |
| 114 |
|
| 115 |
if F < 0.0 then |
| 116 |
S := S - 1; |
| 117 |
F := F + 1.0; |
| 118 |
end if; |
| 119 |
|
| 120 |
return |
| 121 |
timespec'(tv_sec => S, |
| 122 |
tv_nsec => Long_Integer (Long_Long_Integer (F * 10#1#E9))); |
| 123 |
end To_Timespec; |
| 124 |
|
| 125 |
----------------- |
| 126 |
-- Timed_Delay -- |
| 127 |
----------------- |
| 128 |
|
| 129 |
procedure Timed_Delay |
| 130 |
(Time : Duration; |
| 131 |
Mode : Integer) |
| 132 |
is |
| 133 |
Request : aliased timespec; |
| 134 |
Remaind : aliased timespec; |
| 135 |
Rel_Time : Duration; |
| 136 |
Abs_Time : Duration; |
| 137 |
Base_Time : constant Duration := Clock; |
| 138 |
Check_Time : Duration := Base_Time; |
| 139 |
|
| 140 |
Result : Integer; |
| 141 |
pragma Unreferenced (Result); |
| 142 |
|
| 143 |
begin |
| 144 |
if Mode = Relative then |
| 145 |
Rel_Time := Time; |
| 146 |
Abs_Time := Time + Check_Time; |
| 147 |
else |
| 148 |
Rel_Time := Time - Check_Time; |
| 149 |
Abs_Time := Time; |
| 150 |
end if; |
| 151 |
|
| 152 |
if Rel_Time > 0.0 then |
| 153 |
loop |
| 154 |
Request := To_Timespec (Rel_Time); |
| 155 |
Result := nanosleep (Request'Access, Remaind'Access); |
| 156 |
Check_Time := Clock; |
| 157 |
|
| 158 |
exit when Abs_Time <= Check_Time or else Check_Time < Base_Time; |
| 159 |
|
| 160 |
Rel_Time := Abs_Time - Check_Time; |
| 161 |
end loop; |
| 162 |
end if; |
| 163 |
end Timed_Delay; |
| 164 |
|
| 165 |
---------------- |
| 166 |
-- Initialize -- |
| 167 |
---------------- |
| 168 |
|
| 169 |
procedure Initialize is |
| 170 |
begin |
| 171 |
null; |
| 172 |
end Initialize; |
| 173 |
|
| 174 |
end System.OS_Primitives; |