1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- SYSTEM.TASKING.PROTECTED_OBJECTS.ENTRIES --
9 -- Copyright (C) 1998-2005, 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 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
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. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- This package contains all the simple primitives related to protected
35 -- objects with entries (i.e init, lock, unlock).
37 -- The handling of protected objects with no entries is done in
38 -- System.Tasking.Protected_Objects, the complex routines for protected
39 -- objects with entries in System.Tasking.Protected_Objects.Operations.
41 -- The split between Entries and Operations is needed to break circular
42 -- dependencies inside the run time.
44 -- Note: the compiler generates direct calls to this interface, via Rtsfind
47 -- Used for Exception_Occurrence_Access
50 with System
.Task_Primitives
.Operations
;
51 -- Used for Initialize_Lock
57 with System
.Tasking
.Initialization
;
58 -- Used for Defer_Abort,
60 -- Change_Base_Priority
62 pragma Elaborate_All
(System
.Tasking
.Initialization
);
63 -- This insures that tasking is initialized if any protected objects are
66 with System
.Parameters
;
67 -- Used for Single_Lock
69 package body System
.Tasking
.Protected_Objects
.Entries
is
71 package STPO
renames System
.Task_Primitives
.Operations
;
74 use Task_Primitives
.Operations
;
81 Locking_Policy
: Character;
82 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
88 procedure Finalize
(Object
: in out Protection_Entries
) is
89 Entry_Call
: Entry_Call_Link
;
91 Ceiling_Violation
: Boolean;
92 Self_ID
: constant Task_Id
:= STPO
.Self
;
93 Old_Base_Priority
: System
.Any_Priority
;
96 if Object
.Finalized
then
100 STPO
.Write_Lock
(Object
.L
'Unrestricted_Access, Ceiling_Violation
);
106 if Ceiling_Violation
then
108 -- Dip our own priority down to ceiling of lock. See similar code in
109 -- Tasking.Entry_Calls.Lock_Server.
111 STPO
.Write_Lock
(Self_ID
);
112 Old_Base_Priority
:= Self_ID
.Common
.Base_Priority
;
113 Self_ID
.New_Base_Priority
:= Object
.Ceiling
;
114 Initialization
.Change_Base_Priority
(Self_ID
);
115 STPO
.Unlock
(Self_ID
);
121 STPO
.Write_Lock
(Object
.L
'Unrestricted_Access, Ceiling_Violation
);
123 if Ceiling_Violation
then
124 Raise_Exception
(Program_Error
'Identity, "Ceiling Violation");
131 Object
.Old_Base_Priority
:= Old_Base_Priority
;
132 Object
.Pending_Action
:= True;
135 -- Send program_error to all tasks still queued on this object
137 for E
in Object
.Entry_Queues
'Range loop
138 Entry_Call
:= Object
.Entry_Queues
(E
).Head
;
140 while Entry_Call
/= null loop
141 Caller
:= Entry_Call
.Self
;
142 Entry_Call
.Exception_To_Raise
:= Program_Error
'Identity;
144 STPO
.Write_Lock
(Caller
);
145 Initialization
.Wakeup_Entry_Caller
(Self_ID
, Entry_Call
, Done
);
146 STPO
.Unlock
(Caller
);
148 exit when Entry_Call
= Object
.Entry_Queues
(E
).Tail
;
149 Entry_Call
:= Entry_Call
.Next
;
153 Object
.Finalized
:= True;
159 STPO
.Unlock
(Object
.L
'Unrestricted_Access);
161 STPO
.Finalize_Lock
(Object
.L
'Unrestricted_Access);
164 -------------------------------------
165 -- Has_Interrupt_Or_Attach_Handler --
166 -------------------------------------
168 function Has_Interrupt_Or_Attach_Handler
169 (Object
: Protection_Entries_Access
)
172 pragma Warnings
(Off
, Object
);
175 end Has_Interrupt_Or_Attach_Handler
;
177 -----------------------------------
178 -- Initialize_Protection_Entries --
179 -----------------------------------
181 procedure Initialize_Protection_Entries
182 (Object
: Protection_Entries_Access
;
183 Ceiling_Priority
: Integer;
184 Compiler_Info
: System
.Address
;
185 Entry_Bodies
: Protected_Entry_Body_Access
;
186 Find_Body_Index
: Find_Body_Index_Access
)
188 Init_Priority
: Integer := Ceiling_Priority
;
189 Self_ID
: constant Task_Id
:= STPO
.Self
;
192 if Init_Priority
= Unspecified_Priority
then
193 Init_Priority
:= System
.Priority
'Last;
196 if Locking_Policy
= 'C'
197 and then Has_Interrupt_Or_Attach_Handler
(Object
)
198 and then Init_Priority
not in System
.Interrupt_Priority
200 -- Required by C.3.1(11)
205 Initialization
.Defer_Abort
(Self_ID
);
206 Initialize_Lock
(Init_Priority
, Object
.L
'Access);
207 Initialization
.Undefer_Abort
(Self_ID
);
208 Object
.Ceiling
:= System
.Any_Priority
(Init_Priority
);
209 Object
.Owner
:= Null_Task
;
210 Object
.Compiler_Info
:= Compiler_Info
;
211 Object
.Pending_Action
:= False;
212 Object
.Call_In_Progress
:= null;
213 Object
.Entry_Bodies
:= Entry_Bodies
;
214 Object
.Find_Body_Index
:= Find_Body_Index
;
216 for E
in Object
.Entry_Queues
'Range loop
217 Object
.Entry_Queues
(E
).Head
:= null;
218 Object
.Entry_Queues
(E
).Tail
:= null;
220 end Initialize_Protection_Entries
;
226 procedure Lock_Entries
227 (Object
: Protection_Entries_Access
; Ceiling_Violation
: out Boolean)
230 if Object
.Finalized
then
232 (Program_Error
'Identity, "Protected Object is finalized");
235 -- If pragma Detect_Blocking is active then, as described in the ARM
236 -- 9.5.1, par. 15, we must check whether this is an external call on a
237 -- protected subprogram with the same target object as that of the
238 -- protected action that is currently in progress (i.e., if the caller
239 -- is already the protected object's owner). If this is the case hence
240 -- Program_Error must be raised.
242 if Detect_Blocking
and then Object
.Owner
= Self
then
246 -- The lock is made without defering abort
248 -- Therefore the abort has to be deferred before calling this routine.
249 -- This means that the compiler has to generate a Defer_Abort call
250 -- before the call to Lock.
252 -- The caller is responsible for undeferring abort, and compiler
253 -- generated calls must be protected with cleanup handlers to ensure
254 -- that abort is undeferred in all cases.
256 pragma Assert
(STPO
.Self
.Deferral_Level
> 0);
257 Write_Lock
(Object
.L
'Access, Ceiling_Violation
);
259 -- We are entering in a protected action, so that we increase the
260 -- protected object nesting level (if pragma Detect_Blocking is
261 -- active), and update the protected object's owner.
263 if Detect_Blocking
then
265 Self_Id
: constant Task_Id
:= Self
;
268 -- Update the protected object's owner
270 Object
.Owner
:= Self_Id
;
272 -- Increase protected object nesting level
274 Self_Id
.Common
.Protected_Action_Nesting
:=
275 Self_Id
.Common
.Protected_Action_Nesting
+ 1;
281 procedure Lock_Entries
(Object
: Protection_Entries_Access
) is
282 Ceiling_Violation
: Boolean;
285 Lock_Entries
(Object
, Ceiling_Violation
);
287 if Ceiling_Violation
then
288 Raise_Exception
(Program_Error
'Identity, "Ceiling Violation");
292 ----------------------------
293 -- Lock_Read_Only_Entries --
294 ----------------------------
296 procedure Lock_Read_Only_Entries
(Object
: Protection_Entries_Access
) is
297 Ceiling_Violation
: Boolean;
300 if Object
.Finalized
then
302 (Program_Error
'Identity, "Protected Object is finalized");
305 -- If pragma Detect_Blocking is active then, as described in the ARM
306 -- 9.5.1, par. 15, we must check whether this is an external call on a
307 -- protected subprogram with the same target object as that of the
308 -- protected action that is currently in progress (i.e., if the caller
309 -- is already the protected object's owner). If this is the case hence
310 -- Program_Error must be raised.
312 -- Note that in this case (getting read access), several tasks may
313 -- have read ownership of the protected object, so that this method of
314 -- storing the (single) protected object's owner does not work
315 -- reliably for read locks. However, this is the approach taken for two
316 -- major reasosn: first, this function is not currently being used (it
317 -- is provided for possible future use), and second, it largely
318 -- simplifies the implementation.
320 if Detect_Blocking
and then Object
.Owner
= Self
then
324 Read_Lock
(Object
.L
'Access, Ceiling_Violation
);
326 if Ceiling_Violation
then
327 Raise_Exception
(Program_Error
'Identity, "Ceiling Violation");
330 -- We are entering in a protected action, so that we increase the
331 -- protected object nesting level (if pragma Detect_Blocking is
332 -- active), and update the protected object's owner.
334 if Detect_Blocking
then
336 Self_Id
: constant Task_Id
:= Self
;
339 -- Update the protected object's owner
341 Object
.Owner
:= Self_Id
;
343 -- Increase protected object nesting level
345 Self_Id
.Common
.Protected_Action_Nesting
:=
346 Self_Id
.Common
.Protected_Action_Nesting
+ 1;
349 end Lock_Read_Only_Entries
;
355 procedure Unlock_Entries
(Object
: Protection_Entries_Access
) is
357 -- We are exiting from a protected action, so that we decrease the
358 -- protected object nesting level (if pragma Detect_Blocking is
359 -- active), and remove ownership of the protected object.
361 if Detect_Blocking
then
363 Self_Id
: constant Task_Id
:= Self
;
366 -- Calls to this procedure can only take place when being within
367 -- a protected action and when the caller is the protected
370 pragma Assert
(Self_Id
.Common
.Protected_Action_Nesting
> 0
371 and then Object
.Owner
= Self_Id
);
373 -- Remove ownership of the protected object
375 Object
.Owner
:= Null_Task
;
377 Self_Id
.Common
.Protected_Action_Nesting
:=
378 Self_Id
.Common
.Protected_Action_Nesting
- 1;
382 Unlock
(Object
.L
'Access);
385 end System
.Tasking
.Protected_Objects
.Entries
;