2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / s-tpoben.adb
blob9131f8c07b9a7b3d7661536756bee1d646d9b157
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- SYSTEM.TASKING.PROTECTED_OBJECTS.ENTRIES --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1998-2014, 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 package contains all the simple primitives related to protected
33 -- objects with entries (i.e init, lock, unlock).
35 -- The handling of protected objects with no entries is done in
36 -- System.Tasking.Protected_Objects, the complex routines for protected
37 -- objects with entries in System.Tasking.Protected_Objects.Operations.
39 -- The split between Entries and Operations is needed to break circular
40 -- dependencies inside the run time.
42 -- Note: the compiler generates direct calls to this interface, via Rtsfind
44 with System.Task_Primitives.Operations;
45 with System.Restrictions;
46 with System.Parameters;
48 with System.Tasking.Initialization;
49 pragma Elaborate_All (System.Tasking.Initialization);
50 -- To insure that tasking is initialized if any protected objects are created
52 package body System.Tasking.Protected_Objects.Entries is
54 package STPO renames System.Task_Primitives.Operations;
56 use Parameters;
57 use Task_Primitives.Operations;
59 ----------------
60 -- Local Data --
61 ----------------
63 Locking_Policy : Character;
64 pragma Import (C, Locking_Policy, "__gl_locking_policy");
66 --------------
67 -- Finalize --
68 --------------
70 overriding procedure Finalize (Object : in out Protection_Entries) is
71 Entry_Call : Entry_Call_Link;
72 Caller : Task_Id;
73 Ceiling_Violation : Boolean;
74 Self_ID : constant Task_Id := STPO.Self;
75 Old_Base_Priority : System.Any_Priority;
77 begin
78 if Object.Finalized then
79 return;
80 end if;
82 STPO.Write_Lock (Object.L'Unrestricted_Access, Ceiling_Violation);
84 if Single_Lock then
85 Lock_RTS;
86 end if;
88 if Ceiling_Violation then
90 -- Dip our own priority down to ceiling of lock. See similar code in
91 -- Tasking.Entry_Calls.Lock_Server.
93 STPO.Write_Lock (Self_ID);
94 Old_Base_Priority := Self_ID.Common.Base_Priority;
95 Self_ID.New_Base_Priority := Object.Ceiling;
96 Initialization.Change_Base_Priority (Self_ID);
97 STPO.Unlock (Self_ID);
99 if Single_Lock then
100 Unlock_RTS;
101 end if;
103 STPO.Write_Lock (Object.L'Unrestricted_Access, Ceiling_Violation);
105 if Ceiling_Violation then
106 raise Program_Error with "ceiling violation";
107 end if;
109 if Single_Lock then
110 Lock_RTS;
111 end if;
113 Object.Old_Base_Priority := Old_Base_Priority;
114 Object.Pending_Action := True;
115 end if;
117 -- Send program_error to all tasks still queued on this object
119 for E in Object.Entry_Queues'Range loop
120 Entry_Call := Object.Entry_Queues (E).Head;
122 while Entry_Call /= null loop
123 Caller := Entry_Call.Self;
124 Entry_Call.Exception_To_Raise := Program_Error'Identity;
126 STPO.Write_Lock (Caller);
127 Initialization.Wakeup_Entry_Caller (Self_ID, Entry_Call, Done);
128 STPO.Unlock (Caller);
130 exit when Entry_Call = Object.Entry_Queues (E).Tail;
131 Entry_Call := Entry_Call.Next;
132 end loop;
133 end loop;
135 Object.Finalized := True;
137 if Single_Lock then
138 Unlock_RTS;
139 end if;
141 STPO.Unlock (Object.L'Unrestricted_Access);
143 STPO.Finalize_Lock (Object.L'Unrestricted_Access);
144 end Finalize;
146 -----------------
147 -- Get_Ceiling --
148 -----------------
150 function Get_Ceiling
151 (Object : Protection_Entries_Access) return System.Any_Priority is
152 begin
153 return Object.New_Ceiling;
154 end Get_Ceiling;
156 -------------------------------------
157 -- Has_Interrupt_Or_Attach_Handler --
158 -------------------------------------
160 function Has_Interrupt_Or_Attach_Handler
161 (Object : Protection_Entries_Access)
162 return Boolean
164 pragma Warnings (Off, Object);
165 begin
166 return False;
167 end Has_Interrupt_Or_Attach_Handler;
169 -----------------------------------
170 -- Initialize_Protection_Entries --
171 -----------------------------------
173 procedure Initialize_Protection_Entries
174 (Object : Protection_Entries_Access;
175 Ceiling_Priority : Integer;
176 Compiler_Info : System.Address;
177 Entry_Bodies : Protected_Entry_Body_Access;
178 Find_Body_Index : Find_Body_Index_Access)
180 Init_Priority : Integer := Ceiling_Priority;
181 Self_ID : constant Task_Id := STPO.Self;
183 begin
184 if Init_Priority = Unspecified_Priority then
185 Init_Priority := System.Priority'Last;
186 end if;
188 if Locking_Policy = 'C'
189 and then Has_Interrupt_Or_Attach_Handler (Object)
190 and then Init_Priority not in System.Interrupt_Priority
191 then
192 -- Required by C.3.1(11)
194 raise Program_Error;
195 end if;
197 -- If a PO is created from a controlled operation, abort is already
198 -- deferred at this point, so we need to use Defer_Abort_Nestable. In
199 -- some cases, the following assertion can help to spot inconsistencies,
200 -- outside the above scenario involving controlled types.
202 -- pragma Assert (Self_Id.Deferral_Level = 0);
204 Initialization.Defer_Abort_Nestable (Self_ID);
205 Initialize_Lock (Init_Priority, Object.L'Access);
206 Initialization.Undefer_Abort_Nestable (Self_ID);
208 Object.Ceiling := System.Any_Priority (Init_Priority);
209 Object.New_Ceiling := System.Any_Priority (Init_Priority);
210 Object.Owner := Null_Task;
211 Object.Compiler_Info := Compiler_Info;
212 Object.Pending_Action := False;
213 Object.Call_In_Progress := null;
214 Object.Entry_Bodies := Entry_Bodies;
215 Object.Find_Body_Index := Find_Body_Index;
217 for E in Object.Entry_Queues'Range loop
218 Object.Entry_Queues (E).Head := null;
219 Object.Entry_Queues (E).Tail := null;
220 end loop;
221 end Initialize_Protection_Entries;
223 ------------------
224 -- Lock_Entries --
225 ------------------
227 procedure Lock_Entries (Object : Protection_Entries_Access) is
228 Ceiling_Violation : Boolean;
230 begin
231 Lock_Entries_With_Status (Object, Ceiling_Violation);
233 if Ceiling_Violation then
234 raise Program_Error with "ceiling violation";
235 end if;
236 end Lock_Entries;
238 ------------------------------
239 -- Lock_Entries_With_Status --
240 ------------------------------
242 procedure Lock_Entries_With_Status
243 (Object : Protection_Entries_Access;
244 Ceiling_Violation : out Boolean)
246 begin
247 if Object.Finalized then
248 raise Program_Error with "protected object is finalized";
249 end if;
251 -- If pragma Detect_Blocking is active then, as described in the ARM
252 -- 9.5.1, par. 15, we must check whether this is an external call on a
253 -- protected subprogram with the same target object as that of the
254 -- protected action that is currently in progress (i.e., if the caller
255 -- is already the protected object's owner). If this is the case hence
256 -- Program_Error must be raised.
258 if Detect_Blocking and then Object.Owner = Self then
259 raise Program_Error;
260 end if;
262 -- The lock is made without deferring abort
264 -- Therefore the abort has to be deferred before calling this routine.
265 -- This means that the compiler has to generate a Defer_Abort call
266 -- before the call to Lock.
268 -- The caller is responsible for undeferring abort, and compiler
269 -- generated calls must be protected with cleanup handlers to ensure
270 -- that abort is undeferred in all cases.
272 pragma Assert
273 (STPO.Self.Deferral_Level > 0
274 or else not Restrictions.Abort_Allowed);
276 Write_Lock (Object.L'Access, Ceiling_Violation);
278 -- We are entering in a protected action, so that we increase the
279 -- protected object nesting level (if pragma Detect_Blocking is
280 -- active), and update the protected object's owner.
282 if Detect_Blocking then
283 declare
284 Self_Id : constant Task_Id := Self;
286 begin
287 -- Update the protected object's owner
289 Object.Owner := Self_Id;
291 -- Increase protected object nesting level
293 Self_Id.Common.Protected_Action_Nesting :=
294 Self_Id.Common.Protected_Action_Nesting + 1;
295 end;
296 end if;
297 end Lock_Entries_With_Status;
299 ----------------------------
300 -- Lock_Read_Only_Entries --
301 ----------------------------
303 procedure Lock_Read_Only_Entries (Object : Protection_Entries_Access) is
304 Ceiling_Violation : Boolean;
306 begin
307 if Object.Finalized then
308 raise Program_Error with "protected object is finalized";
309 end if;
311 -- If pragma Detect_Blocking is active then, as described in the ARM
312 -- 9.5.1, par. 15, we must check whether this is an external call on a
313 -- protected subprogram with the same target object as that of the
314 -- protected action that is currently in progress (i.e., if the caller
315 -- is already the protected object's owner). If this is the case hence
316 -- Program_Error must be raised.
318 -- Note that in this case (getting read access), several tasks may
319 -- have read ownership of the protected object, so that this method of
320 -- storing the (single) protected object's owner does not work
321 -- reliably for read locks. However, this is the approach taken for two
322 -- major reasons: first, this function is not currently being used (it
323 -- is provided for possible future use), and second, it largely
324 -- simplifies the implementation.
326 if Detect_Blocking and then Object.Owner = Self then
327 raise Program_Error;
328 end if;
330 Read_Lock (Object.L'Access, Ceiling_Violation);
332 if Ceiling_Violation then
333 raise Program_Error with "ceiling violation";
334 end if;
336 -- We are entering in a protected action, so that we increase the
337 -- protected object nesting level (if pragma Detect_Blocking is
338 -- active), and update the protected object's owner.
340 if Detect_Blocking then
341 declare
342 Self_Id : constant Task_Id := Self;
344 begin
345 -- Update the protected object's owner
347 Object.Owner := Self_Id;
349 -- Increase protected object nesting level
351 Self_Id.Common.Protected_Action_Nesting :=
352 Self_Id.Common.Protected_Action_Nesting + 1;
353 end;
354 end if;
355 end Lock_Read_Only_Entries;
357 -----------------------
358 -- Number_Of_Entries --
359 -----------------------
361 function Number_Of_Entries
362 (Object : Protection_Entries_Access) return Entry_Index
364 begin
365 return Entry_Index (Object.Num_Entries);
366 end Number_Of_Entries;
368 -----------------
369 -- Set_Ceiling --
370 -----------------
372 procedure Set_Ceiling
373 (Object : Protection_Entries_Access;
374 Prio : System.Any_Priority) is
375 begin
376 Object.New_Ceiling := Prio;
377 end Set_Ceiling;
379 ---------------------
380 -- Set_Entry_Names --
381 ---------------------
383 procedure Set_Entry_Names
384 (Object : Protection_Entries_Access;
385 Names : Protected_Entry_Names_Access)
387 begin
388 Object.Entry_Names := Names;
389 end Set_Entry_Names;
391 --------------------
392 -- Unlock_Entries --
393 --------------------
395 procedure Unlock_Entries (Object : Protection_Entries_Access) is
396 begin
397 -- We are exiting from a protected action, so that we decrease the
398 -- protected object nesting level (if pragma Detect_Blocking is
399 -- active), and remove ownership of the protected object.
401 if Detect_Blocking then
402 declare
403 Self_Id : constant Task_Id := Self;
405 begin
406 -- Calls to this procedure can only take place when being within
407 -- a protected action and when the caller is the protected
408 -- object's owner.
410 pragma Assert (Self_Id.Common.Protected_Action_Nesting > 0
411 and then Object.Owner = Self_Id);
413 -- Remove ownership of the protected object
415 Object.Owner := Null_Task;
417 Self_Id.Common.Protected_Action_Nesting :=
418 Self_Id.Common.Protected_Action_Nesting - 1;
419 end;
420 end if;
422 -- Before releasing the mutex we must actually update its ceiling
423 -- priority if it has been changed.
425 if Object.New_Ceiling /= Object.Ceiling then
426 if Locking_Policy = 'C' then
427 System.Task_Primitives.Operations.Set_Ceiling
428 (Object.L'Access, Object.New_Ceiling);
429 end if;
431 Object.Ceiling := Object.New_Ceiling;
432 end if;
434 Unlock (Object.L'Access);
435 end Unlock_Entries;
437 end System.Tasking.Protected_Objects.Entries;