PR sanitizer/65081
[official-gcc.git] / gcc / ada / s-osprim-vxworks.adb
blob1eccae5612aaf069de103e3e7106771934691e48
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-2011, 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 VxWorks targets
34 with System.OS_Interface;
35 -- Since the thread library is part of the VxWorks kernel, using OS_Interface
36 -- is not a problem here, as long as we only use System.OS_Interface as a
37 -- set of C imported routines: using Ada routines from this package would
38 -- create a dependency on libgnarl in libgnat, which is not desirable.
40 with System.OS_Constants;
41 with Interfaces.C;
43 package body System.OS_Primitives is
45 use System.OS_Interface;
46 use type Interfaces.C.int;
48 package OSC renames System.OS_Constants;
50 ------------------------
51 -- Internal functions --
52 ------------------------
54 function To_Clock_Ticks (D : Duration) return int;
55 -- Convert a duration value (in seconds) into clock ticks.
56 -- Note that this routine is duplicated from System.OS_Interface since
57 -- as explained above, we do not want to depend on libgnarl
59 function To_Clock_Ticks (D : Duration) return int is
60 Ticks : Long_Long_Integer;
61 Rate_Duration : Duration;
62 Ticks_Duration : Duration;
64 begin
65 if D < 0.0 then
66 return -1;
67 end if;
69 -- Ensure that the duration can be converted to ticks
70 -- at the current clock tick rate without overflowing.
72 Rate_Duration := Duration (sysClkRateGet);
74 if D > (Duration'Last / Rate_Duration) then
75 Ticks := Long_Long_Integer (int'Last);
76 else
77 Ticks_Duration := D * Rate_Duration;
78 Ticks := Long_Long_Integer (Ticks_Duration);
80 if Ticks_Duration > Duration (Ticks) then
81 Ticks := Ticks + 1;
82 end if;
84 if Ticks > Long_Long_Integer (int'Last) then
85 Ticks := Long_Long_Integer (int'Last);
86 end if;
87 end if;
89 return int (Ticks);
90 end To_Clock_Ticks;
92 -----------
93 -- Clock --
94 -----------
96 function Clock return Duration is
97 TS : aliased timespec;
98 Result : int;
99 begin
100 Result := clock_gettime (OSC.CLOCK_RT_Ada, TS'Unchecked_Access);
101 pragma Assert (Result = 0);
102 return Duration (TS.ts_sec) + Duration (TS.ts_nsec) / 10#1#E9;
103 end Clock;
105 ---------------------
106 -- Monotonic_Clock --
107 ---------------------
109 function Monotonic_Clock return Duration renames Clock;
111 -----------------
112 -- Timed_Delay --
113 -----------------
115 procedure Timed_Delay
116 (Time : Duration;
117 Mode : Integer)
119 Rel_Time : Duration;
120 Abs_Time : Duration;
121 Base_Time : constant Duration := Clock;
122 Check_Time : Duration := Base_Time;
123 Ticks : int;
125 Result : int;
126 pragma Unreferenced (Result);
128 begin
129 if Mode = Relative then
130 Rel_Time := Time;
131 Abs_Time := Time + Check_Time;
132 else
133 Rel_Time := Time - Check_Time;
134 Abs_Time := Time;
135 end if;
137 if Rel_Time > 0.0 then
138 loop
139 Ticks := To_Clock_Ticks (Rel_Time);
141 if Mode = Relative and then Ticks < int'Last then
142 -- The first tick will delay anytime between 0 and
143 -- 1 / sysClkRateGet seconds, so we need to add one to
144 -- be on the safe side.
146 Ticks := Ticks + 1;
147 end if;
149 Result := taskDelay (Ticks);
150 Check_Time := Clock;
152 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
154 Rel_Time := Abs_Time - Check_Time;
155 end loop;
156 end if;
157 end Timed_Delay;
159 ----------------
160 -- Initialize --
161 ----------------
163 procedure Initialize is
164 begin
165 null;
166 end Initialize;
168 end System.OS_Primitives;