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