| 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.1-2008-like operating systems |
| 33 |
|
| 34 |
with System.CRTL; |
| 35 |
with System.OS_Constants; |
| 36 |
with System.Parameters; |
| 37 |
package body System.OS_Primitives is |
| 38 |
|
| 39 |
subtype int is System.CRTL.int; |
| 40 |
|
| 41 |
-- ??? These definitions are duplicated from System.OS_Interface because |
| 42 |
-- we don't want to depend on any package. Consider removing these |
| 43 |
-- declarations in System.OS_Interface and move these ones to the spec. |
| 44 |
|
| 45 |
type time_t is range -2 ** (System.Parameters.time_t_bits - 1) |
| 46 |
.. 2 ** (System.Parameters.time_t_bits - 1) - 1; |
| 47 |
|
| 48 |
type timespec is record |
| 49 |
tv_sec : time_t; |
| 50 |
tv_nsec : Long_Integer; |
| 51 |
end record; |
| 52 |
pragma Convention (C, timespec); |
| 53 |
|
| 54 |
function nanosleep (rqtp, rmtp : not null access timespec) return Integer; |
| 55 |
pragma Import (C, nanosleep, "nanosleep"); |
| 56 |
|
| 57 |
----------- |
| 58 |
-- Clock -- |
| 59 |
----------- |
| 60 |
|
| 61 |
function Clock return Duration is |
| 62 |
TS : aliased timespec; |
| 63 |
Result : int; |
| 64 |
|
| 65 |
type clockid_t is new int; |
| 66 |
CLOCK_REALTIME : constant clockid_t := |
| 67 |
System.OS_Constants.CLOCK_REALTIME; |
| 68 |
|
| 69 |
function clock_gettime |
| 70 |
(clock_id : clockid_t; |
| 71 |
tp : access timespec) return int; |
| 72 |
pragma Import (C, clock_gettime, "clock_gettime"); |
| 73 |
|
| 74 |
begin |
| 75 |
Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access); |
| 76 |
pragma Assert (Result = 0); |
| 77 |
return Duration (TS.tv_sec) + Duration (TS.tv_nsec) / 10#1#E9; |
| 78 |
end Clock; |
| 79 |
|
| 80 |
----------------- |
| 81 |
-- To_Timespec -- |
| 82 |
----------------- |
| 83 |
|
| 84 |
function To_Timespec (D : Duration) return timespec; |
| 85 |
|
| 86 |
function To_Timespec (D : Duration) return timespec is |
| 87 |
S : time_t; |
| 88 |
F : Duration; |
| 89 |
|
| 90 |
begin |
| 91 |
S := time_t (Long_Long_Integer (D)); |
| 92 |
F := D - Duration (S); |
| 93 |
|
| 94 |
-- If F has negative value due to a round-up, adjust for positive F |
| 95 |
-- value. |
| 96 |
|
| 97 |
if F < 0.0 then |
| 98 |
S := S - 1; |
| 99 |
F := F + 1.0; |
| 100 |
end if; |
| 101 |
|
| 102 |
return |
| 103 |
timespec'(tv_sec => S, |
| 104 |
tv_nsec => Long_Integer (Long_Long_Integer (F * 10#1#E9))); |
| 105 |
end To_Timespec; |
| 106 |
|
| 107 |
----------------- |
| 108 |
-- Timed_Delay -- |
| 109 |
----------------- |
| 110 |
|
| 111 |
procedure Timed_Delay |
| 112 |
(Time : Duration; |
| 113 |
Mode : Integer) |
| 114 |
is separate; |
| 115 |
|
| 116 |
---------------- |
| 117 |
-- Initialize -- |
| 118 |
---------------- |
| 119 |
|
| 120 |
procedure Initialize is |
| 121 |
begin |
| 122 |
null; |
| 123 |
end Initialize; |
| 124 |
|
| 125 |
end System.OS_Primitives; |