Update concepts branch to revision 131834
[official-gcc.git] / gcc / ada / s-osprim-vxworks.adb
blob62fde6a7af172ca2f5af8357a36c4a4931e153bd
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-2008, 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 2, or (at your option) any later ver- --
14 -- sion. GNARL 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This version is for VxWorks targets
36 with System.OS_Interface;
37 -- Since the thread library is part of the VxWorks kernel, using OS_Interface
38 -- is not a problem here, as long as we only use System.OS_Interface as a
39 -- set of C imported routines: using Ada routines from this package would
40 -- create a dependency on libgnarl in libgnat, which is not desirable.
42 with Interfaces.C;
44 package body System.OS_Primitives is
46 use System.OS_Interface;
47 use type Interfaces.C.int;
49 ------------------------
50 -- Internal functions --
51 ------------------------
53 function To_Clock_Ticks (D : Duration) return int;
54 -- Convert a duration value (in seconds) into clock ticks.
55 -- Note that this routine is duplicated from System.OS_Interface since
56 -- as explained above, we do not want to depend on libgnarl
58 function To_Clock_Ticks (D : Duration) return int is
59 Ticks : Long_Long_Integer;
60 Rate_Duration : Duration;
61 Ticks_Duration : Duration;
63 begin
64 if D < 0.0 then
65 return -1;
66 end if;
68 -- Ensure that the duration can be converted to ticks
69 -- at the current clock tick rate without overflowing.
71 Rate_Duration := Duration (sysClkRateGet);
73 if D > (Duration'Last / Rate_Duration) then
74 Ticks := Long_Long_Integer (int'Last);
75 else
76 Ticks_Duration := D * Rate_Duration;
77 Ticks := Long_Long_Integer (Ticks_Duration);
79 if Ticks_Duration > Duration (Ticks) then
80 Ticks := Ticks + 1;
81 end if;
83 if Ticks > Long_Long_Integer (int'Last) then
84 Ticks := Long_Long_Integer (int'Last);
85 end if;
86 end if;
88 return int (Ticks);
89 end To_Clock_Ticks;
91 -----------
92 -- Clock --
93 -----------
95 function Clock return Duration is
96 TS : aliased timespec;
97 Result : int;
98 begin
99 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
100 pragma Assert (Result = 0);
101 return Duration (TS.ts_sec) + Duration (TS.ts_nsec) / 10#1#E9;
102 end Clock;
104 ---------------------
105 -- Monotonic_Clock --
106 ---------------------
108 function Monotonic_Clock return Duration renames Clock;
110 -----------------
111 -- Timed_Delay --
112 -----------------
114 procedure Timed_Delay
115 (Time : Duration;
116 Mode : Integer)
118 Rel_Time : Duration;
119 Abs_Time : Duration;
120 Base_Time : constant Duration := Clock;
121 Check_Time : Duration := Base_Time;
122 Ticks : int;
124 Result : int;
125 pragma Unreferenced (Result);
127 begin
128 if Mode = Relative then
129 Rel_Time := Time;
130 Abs_Time := Time + Check_Time;
131 else
132 Rel_Time := Time - Check_Time;
133 Abs_Time := Time;
134 end if;
136 if Rel_Time > 0.0 then
137 loop
138 Ticks := To_Clock_Ticks (Rel_Time);
140 if Mode = Relative and then Ticks < int'Last then
141 -- The first tick will delay anytime between 0 and
142 -- 1 / sysClkRateGet seconds, so we need to add one to
143 -- be on the safe side.
145 Ticks := Ticks + 1;
146 end if;
148 Result := taskDelay (Ticks);
149 Check_Time := Clock;
151 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
153 Rel_Time := Abs_Time - Check_Time;
154 end loop;
155 end if;
156 end Timed_Delay;
158 ----------------
159 -- Initialize --
160 ----------------
162 procedure Initialize is
163 begin
164 null;
165 end Initialize;
167 end System.OS_Primitives;