Add hppa-openbsd target
[official-gcc.git] / gcc / ada / 5zosprim.adb
blob5c046f79efae1cfc7da250242369f84ea34baa8d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNU ADA 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 -- --
10 -- Copyright (C) 1998-2001 Free Software Foundation, Inc. --
11 -- --
12 -- GNARL is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNARL; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
29 -- --
30 -- GNARL was developed by the GNARL team at Florida State University. It is --
31 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
32 -- State University (http://www.gnat.com). --
33 -- --
34 ------------------------------------------------------------------------------
36 -- This version is for VxWorks targets
38 with System.OS_Interface;
39 -- Since the thread library is part of the VxWorks kernel, using OS_Interface
40 -- is not a problem here, as long as we only use System.OS_Interface as a
41 -- set of C imported routines: using Ada routines from this package would
42 -- create a dependency on libgnarl in libgnat, which is not desirable.
44 with Interfaces.C;
45 -- used for type int
47 package body System.OS_Primitives is
49 use System.OS_Interface;
51 --------------------------
52 -- Internal functions --
53 --------------------------
55 function To_Clock_Ticks (D : Duration) return int;
56 -- Convert a duration value (in seconds) into clock ticks.
57 -- Note that this routine is duplicated from System.OS_Interface since
58 -- as explained above, we do not want to depend on libgnarl
60 function To_Clock_Ticks (D : Duration) return int is
61 Ticks : Long_Long_Integer;
62 Rate_Duration : Duration;
63 Ticks_Duration : Duration;
64 begin
65 -- Ensure that the duration can be converted to ticks
66 -- at the current clock tick rate without overflowing.
68 Rate_Duration := Duration (sysClkRateGet);
70 if D > (Duration'Last / Rate_Duration) then
71 Ticks := Long_Long_Integer (int'Last);
72 else
73 -- We always want to round up to the nearest clock tick.
75 Ticks_Duration := D * Rate_Duration;
76 Ticks := Long_Long_Integer (Ticks_Duration);
78 if Ticks_Duration > Duration (Ticks) then
79 Ticks := Ticks + 1;
80 end if;
82 if Ticks > Long_Long_Integer (int'Last) then
83 Ticks := Long_Long_Integer (int'Last);
84 end if;
85 end if;
87 return int (Ticks);
88 end To_Clock_Ticks;
90 -----------
91 -- Clock --
92 -----------
94 function Clock return Duration is
95 TS : aliased timespec;
96 Result : int;
98 use type Interfaces.C.int;
99 begin
100 Result := clock_gettime (CLOCK_REALTIME, 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 Result : int;
120 Rel_Time : Duration;
121 Abs_Time : Duration;
122 Check_Time : Duration := Clock;
124 begin
125 if Mode = Relative then
126 Rel_Time := Time;
127 Abs_Time := Time + Check_Time;
128 else
129 Rel_Time := Time - Check_Time;
130 Abs_Time := Time;
131 end if;
133 if Rel_Time > 0.0 then
134 loop
135 Result := taskDelay (To_Clock_Ticks (Rel_Time));
136 Check_Time := Clock;
138 exit when Abs_Time <= Check_Time;
140 Rel_Time := Abs_Time - Check_Time;
141 end loop;
142 end if;
143 end Timed_Delay;
145 end System.OS_Primitives;