* c-common.c (get_priority): Add check for
[official-gcc.git] / gcc / ada / s-taprob.adb
blobd4b08e4c1f170ae2fe4e23e31232b3219eadf34a
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . T A S K I N G . P R O T E C T E D _ O B J E C T S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1991-1994, Florida State University --
10 -- Copyright (C) 1995-2006, AdaCore --
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, 51 Franklin Street, Fifth Floor, --
21 -- Boston, MA 02110-1301, 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. --
31 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 -- --
33 ------------------------------------------------------------------------------
35 pragma Polling (Off);
36 -- Turn off polling, we do not want ATC polling to take place during
37 -- tasking operations. It causes infinite loops and other problems.
39 with System.Task_Primitives.Operations;
40 -- used for Write_Lock
41 -- Unlock
42 -- Self
44 with System.Parameters;
45 -- used for Runtime_Traces
47 with System.Traces;
48 -- used for Send_Trace_Info
50 with System.Soft_Links.Tasking;
51 -- Used for Init_Tasking_Soft_Links
53 package body System.Tasking.Protected_Objects is
55 use System.Task_Primitives.Operations;
56 use System.Traces;
58 -------------------------
59 -- Finalize_Protection --
60 -------------------------
62 procedure Finalize_Protection (Object : in out Protection) is
63 begin
64 Finalize_Lock (Object.L'Unrestricted_Access);
65 end Finalize_Protection;
67 ---------------------------
68 -- Initialize_Protection --
69 ---------------------------
71 procedure Initialize_Protection
72 (Object : Protection_Access;
73 Ceiling_Priority : Integer)
75 Init_Priority : Integer := Ceiling_Priority;
77 begin
78 if Init_Priority = Unspecified_Priority then
79 Init_Priority := System.Priority'Last;
80 end if;
82 Initialize_Lock (Init_Priority, Object.L'Access);
83 Object.Ceiling := System.Any_Priority (Init_Priority);
84 Object.New_Ceiling := System.Any_Priority (Init_Priority);
85 Object.Owner := Null_Task;
86 end Initialize_Protection;
88 -----------------
89 -- Get_Ceiling --
90 -----------------
92 function Get_Ceiling
93 (Object : Protection_Access) return System.Any_Priority is
94 begin
95 return Object.New_Ceiling;
96 end Get_Ceiling;
98 ----------
99 -- Lock --
100 ----------
102 procedure Lock (Object : Protection_Access) is
103 Ceiling_Violation : Boolean;
105 begin
106 -- The lock is made without defering abort
108 -- Therefore the abort has to be deferred before calling this routine.
109 -- This means that the compiler has to generate a Defer_Abort call
110 -- before the call to Lock.
112 -- The caller is responsible for undeferring abort, and compiler
113 -- generated calls must be protected with cleanup handlers to ensure
114 -- that abort is undeferred in all cases.
116 -- If pragma Detect_Blocking is active then, as described in the ARM
117 -- 9.5.1, par. 15, we must check whether this is an external call on a
118 -- protected subprogram with the same target object as that of the
119 -- protected action that is currently in progress (i.e., if the caller
120 -- is already the protected object's owner). If this is the case hence
121 -- Program_Error must be raised.
123 if Detect_Blocking and then Object.Owner = Self then
124 raise Program_Error;
125 end if;
127 Write_Lock (Object.L'Access, Ceiling_Violation);
129 if Parameters.Runtime_Traces then
130 Send_Trace_Info (PO_Lock);
131 end if;
133 if Ceiling_Violation then
134 raise Program_Error;
135 end if;
137 -- We are entering in a protected action, so that we increase the
138 -- protected object nesting level (if pragma Detect_Blocking is
139 -- active), and update the protected object's owner.
141 if Detect_Blocking then
142 declare
143 Self_Id : constant Task_Id := Self;
144 begin
145 -- Update the protected object's owner
147 Object.Owner := Self_Id;
149 -- Increase protected object nesting level
151 Self_Id.Common.Protected_Action_Nesting :=
152 Self_Id.Common.Protected_Action_Nesting + 1;
153 end;
154 end if;
155 end Lock;
157 --------------------
158 -- Lock_Read_Only --
159 --------------------
161 procedure Lock_Read_Only (Object : Protection_Access) is
162 Ceiling_Violation : Boolean;
164 begin
165 -- If pragma Detect_Blocking is active then, as described in the ARM
166 -- 9.5.1, par. 15, we must check whether this is an external call on
167 -- protected subprogram with the same target object as that of the
168 -- protected action that is currently in progress (i.e., if the caller
169 -- is already the protected object's owner). If this is the case hence
170 -- Program_Error must be raised.
172 -- Note that in this case (getting read access), several tasks may have
173 -- read ownership of the protected object, so that this method of
174 -- storing the (single) protected object's owner does not work reliably
175 -- for read locks. However, this is the approach taken for two major
176 -- reasosn: first, this function is not currently being used (it is
177 -- provided for possible future use), and second, it largely simplifies
178 -- the implementation.
180 if Detect_Blocking and then Object.Owner = Self then
181 raise Program_Error;
182 end if;
184 Read_Lock (Object.L'Access, Ceiling_Violation);
186 if Parameters.Runtime_Traces then
187 Send_Trace_Info (PO_Lock);
188 end if;
190 if Ceiling_Violation then
191 raise Program_Error;
192 end if;
194 -- We are entering in a protected action, so we increase the protected
195 -- object nesting level (if pragma Detect_Blocking is active).
197 if Detect_Blocking then
198 declare
199 Self_Id : constant Task_Id := Self;
200 begin
201 -- Update the protected object's owner
203 Object.Owner := Self_Id;
205 -- Increase protected object nesting level
207 Self_Id.Common.Protected_Action_Nesting :=
208 Self_Id.Common.Protected_Action_Nesting + 1;
209 end;
210 end if;
211 end Lock_Read_Only;
213 -----------------
214 -- Set_Ceiling --
215 -----------------
217 procedure Set_Ceiling
218 (Object : Protection_Access;
219 Prio : System.Any_Priority) is
220 begin
221 Object.New_Ceiling := Prio;
222 end Set_Ceiling;
224 ------------
225 -- Unlock --
226 ------------
228 procedure Unlock (Object : Protection_Access) is
229 begin
230 -- We are exiting from a protected action, so that we decrease the
231 -- protected object nesting level (if pragma Detect_Blocking is
232 -- active), and remove ownership of the protected object.
234 if Detect_Blocking then
235 declare
236 Self_Id : constant Task_Id := Self;
238 begin
239 -- Calls to this procedure can only take place when being within
240 -- a protected action and when the caller is the protected
241 -- object's owner.
243 pragma Assert (Self_Id.Common.Protected_Action_Nesting > 0
244 and then Object.Owner = Self_Id);
246 -- Remove ownership of the protected object
248 Object.Owner := Null_Task;
250 -- We are exiting from a protected action, so we decrease the
251 -- protected object nesting level.
253 Self_Id.Common.Protected_Action_Nesting :=
254 Self_Id.Common.Protected_Action_Nesting - 1;
255 end;
256 end if;
258 Unlock (Object.L'Access);
260 if Parameters.Runtime_Traces then
261 Send_Trace_Info (PO_Unlock);
262 end if;
263 end Unlock;
265 begin
266 -- Ensure that tasking is initialized, as well as tasking soft links
267 -- when using protected objects.
269 Tasking.Initialize;
270 System.Soft_Links.Tasking.Init_Tasking_Soft_Links;
271 end System.Tasking.Protected_Objects;