1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- SYSTEM.TASKING.PROTECTED_OBJECTS.ENTRIES --
9 -- Copyright (C) 1998-2012, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
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
;
57 use Task_Primitives
.Operations
;
63 Locking_Policy
: Character;
64 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
70 overriding
procedure Finalize
(Object
: in out Protection_Entries
) is
71 Entry_Call
: Entry_Call_Link
;
73 Ceiling_Violation
: Boolean;
74 Self_ID
: constant Task_Id
:= STPO
.Self
;
75 Old_Base_Priority
: System
.Any_Priority
;
78 if Object
.Finalized
then
82 STPO
.Write_Lock
(Object
.L
'Unrestricted_Access, Ceiling_Violation
);
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
);
103 STPO
.Write_Lock
(Object
.L
'Unrestricted_Access, Ceiling_Violation
);
105 if Ceiling_Violation
then
106 raise Program_Error
with "Ceiling Violation";
113 Object
.Old_Base_Priority
:= Old_Base_Priority
;
114 Object
.Pending_Action
:= True;
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
;
135 Object
.Finalized
:= True;
141 STPO
.Unlock
(Object
.L
'Unrestricted_Access);
143 STPO
.Finalize_Lock
(Object
.L
'Unrestricted_Access);
151 (Object
: Protection_Entries_Access
) return System
.Any_Priority
is
153 return Object
.New_Ceiling
;
156 -------------------------------------
157 -- Has_Interrupt_Or_Attach_Handler --
158 -------------------------------------
160 function Has_Interrupt_Or_Attach_Handler
161 (Object
: Protection_Entries_Access
)
164 pragma Warnings
(Off
, Object
);
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
;
184 if Init_Priority
= Unspecified_Priority
then
185 Init_Priority
:= System
.Priority
'Last;
188 if Locking_Policy
= 'C'
189 and then Has_Interrupt_Or_Attach_Handler
(Object
)
190 and then Init_Priority
not in System
.Interrupt_Priority
192 -- Required by C.3.1(11)
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;
221 end Initialize_Protection_Entries
;
227 procedure Lock_Entries
(Object
: Protection_Entries_Access
) is
228 Ceiling_Violation
: Boolean;
231 Lock_Entries_With_Status
(Object
, Ceiling_Violation
);
233 if Ceiling_Violation
then
234 raise Program_Error
with "Ceiling Violation";
238 ------------------------------
239 -- Lock_Entries_With_Status --
240 ------------------------------
242 procedure Lock_Entries_With_Status
243 (Object
: Protection_Entries_Access
;
244 Ceiling_Violation
: out Boolean)
247 if Object
.Finalized
then
248 raise Program_Error
with "Protected Object is finalized";
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
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.
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
284 Self_Id
: constant Task_Id
:= Self
;
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;
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;
307 if Object
.Finalized
then
308 raise Program_Error
with "Protected Object is finalized";
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
330 Read_Lock
(Object
.L
'Access, Ceiling_Violation
);
332 if Ceiling_Violation
then
333 raise Program_Error
with "Ceiling Violation";
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
342 Self_Id
: constant Task_Id
:= Self
;
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;
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
365 return Entry_Index
(Object
.Num_Entries
);
366 end Number_Of_Entries
;
372 procedure Set_Ceiling
373 (Object
: Protection_Entries_Access
;
374 Prio
: System
.Any_Priority
) is
376 Object
.New_Ceiling
:= Prio
;
379 ---------------------
380 -- Set_Entry_Names --
381 ---------------------
383 procedure Set_Entry_Names
384 (Object
: Protection_Entries_Access
;
385 Names
: Protected_Entry_Names_Access
)
388 Object
.Entry_Names
:= Names
;
395 procedure Unlock_Entries
(Object
: Protection_Entries_Access
) is
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
403 Self_Id
: constant Task_Id
:= Self
;
406 -- Calls to this procedure can only take place when being within
407 -- a protected action and when the caller is the protected
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;
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
);
431 Object
.Ceiling
:= Object
.New_Ceiling
;
434 Unlock
(Object
.L
'Access);
437 end System
.Tasking
.Protected_Objects
.Entries
;