| 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 darwin |
| 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 struct_timezone is record |
| 43 |
tz_minuteswest : Integer; |
| 44 |
tz_dsttime : Integer; |
| 45 |
end record; |
| 46 |
pragma Convention (C, struct_timezone); |
| 47 |
type struct_timezone_ptr is access all struct_timezone; |
| 48 |
|
| 49 |
type time_t is range -2 ** (System.Parameters.time_t_bits - 1) |
| 50 |
.. 2 ** (System.Parameters.time_t_bits - 1) - 1; |
| 51 |
|
| 52 |
type struct_timeval is record |
| 53 |
tv_sec : time_t; |
| 54 |
tv_usec : Integer; |
| 55 |
end record; |
| 56 |
pragma Convention (C, struct_timeval); |
| 57 |
|
| 58 |
function gettimeofday |
| 59 |
(tv : not null access struct_timeval; |
| 60 |
tz : struct_timezone_ptr) return Integer; |
| 61 |
pragma Import (C, gettimeofday, "gettimeofday"); |
| 62 |
|
| 63 |
type timespec is record |
| 64 |
tv_sec : time_t; |
| 65 |
tv_nsec : Long_Integer; |
| 66 |
end record; |
| 67 |
pragma Convention (C, timespec); |
| 68 |
|
| 69 |
function nanosleep (rqtp, rmtp : not null access timespec) return Integer; |
| 70 |
pragma Import (C, nanosleep, "nanosleep"); |
| 71 |
|
| 72 |
----------- |
| 73 |
-- Clock -- |
| 74 |
----------- |
| 75 |
|
| 76 |
function Clock return Duration is |
| 77 |
TV : aliased struct_timeval; |
| 78 |
|
| 79 |
Result : Integer; |
| 80 |
pragma Unreferenced (Result); |
| 81 |
|
| 82 |
begin |
| 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 |
| 87 |
|
| 88 |
-- None of these codes signal a potential clock skew, hence the return |
| 89 |
-- value is never checked. |
| 90 |
|
| 91 |
Result := gettimeofday (TV'Access, null); |
| 92 |
return Duration (TV.tv_sec) + Duration (TV.tv_usec) / 10#1#E6; |
| 93 |
end Clock; |
| 94 |
|
| 95 |
----------------- |
| 96 |
-- To_Timespec -- |
| 97 |
----------------- |
| 98 |
|
| 99 |
function To_Timespec (D : Duration) return timespec; |
| 100 |
|
| 101 |
function To_Timespec (D : Duration) return timespec is |
| 102 |
S : time_t; |
| 103 |
F : Duration; |
| 104 |
|
| 105 |
begin |
| 106 |
S := time_t (Long_Long_Integer (D)); |
| 107 |
F := D - Duration (S); |
| 108 |
|
| 109 |
-- If F has negative value due to a round-up, adjust for positive F |
| 110 |
-- value. |
| 111 |
|
| 112 |
if F < 0.0 then |
| 113 |
S := S - 1; |
| 114 |
F := F + 1.0; |
| 115 |
end if; |
| 116 |
|
| 117 |
return |
| 118 |
timespec'(tv_sec => S, |
| 119 |
tv_nsec => Long_Integer (Long_Long_Integer (F * 10#1#E9))); |
| 120 |
end To_Timespec; |
| 121 |
|
| 122 |
----------------- |
| 123 |
-- Timed_Delay -- |
| 124 |
----------------- |
| 125 |
|
| 126 |
procedure Timed_Delay |
| 127 |
(Time : Duration; |
| 128 |
Mode : Integer) |
| 129 |
is |
| 130 |
Request : aliased timespec; |
| 131 |
Remaind : aliased timespec; |
| 132 |
Rel_Time : Duration; |
| 133 |
Abs_Time : Duration; |
| 134 |
Base_Time : constant Duration := Clock; |
| 135 |
Check_Time : Duration := Base_Time; |
| 136 |
|
| 137 |
Result : Integer; |
| 138 |
pragma Unreferenced (Result); |
| 139 |
|
| 140 |
begin |
| 141 |
if Mode = Relative then |
| 142 |
Rel_Time := Time; |
| 143 |
Abs_Time := Time + Check_Time; |
| 144 |
else |
| 145 |
Rel_Time := Time - Check_Time; |
| 146 |
Abs_Time := Time; |
| 147 |
end if; |
| 148 |
|
| 149 |
if Rel_Time > 0.0 then |
| 150 |
loop |
| 151 |
Request := To_Timespec (Rel_Time); |
| 152 |
Result := nanosleep (Request'Access, Remaind'Access); |
| 153 |
Check_Time := Clock; |
| 154 |
|
| 155 |
exit when Abs_Time <= Check_Time or else Check_Time < Base_Time; |
| 156 |
|
| 157 |
Rel_Time := Abs_Time - Check_Time; |
| 158 |
end loop; |
| 159 |
end if; |
| 160 |
end Timed_Delay; |
| 161 |
|
| 162 |
---------------- |
| 163 |
-- Initialize -- |
| 164 |
---------------- |
| 165 |
|
| 166 |
procedure Initialize is |
| 167 |
begin |
| 168 |
null; |
| 169 |
end Initialize; |
| 170 |
|
| 171 |
end System.OS_Primitives; |