1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
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 --
9 -- Copyright (C) 1991-2017, Florida State University --
10 -- Copyright (C) 1995-2023, AdaCore --
12 -- GNAT 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 3, or (at your option) any later ver- --
15 -- sion. GNAT 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. --
19 -- As a special exception under Section 7 of GPL version 3, you are granted --
20 -- additional permissions described in the GCC Runtime Library Exception, --
21 -- version 3.1, as published by the Free Software Foundation. --
23 -- You should have received a copy of the GNU General Public License and --
24 -- a copy of the GCC Runtime Library Exception along with this program; --
25 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
26 -- <http://www.gnu.org/licenses/>. --
28 -- GNARL was developed by the GNARL team at Florida State University. --
29 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 ------------------------------------------------------------------------------
33 with System
.Task_Primitives
.Operations
;
34 with System
.Soft_Links
.Tasking
;
36 with System
.Secondary_Stack
;
37 pragma Elaborate_All
(System
.Secondary_Stack
);
38 pragma Unreferenced
(System
.Secondary_Stack
);
39 -- Make sure the body of Secondary_Stack is elaborated before calling
40 -- Init_Tasking_Soft_Links. See comments for this routine for explanation.
42 package body System
.Tasking
.Protected_Objects
is
44 use System
.Task_Primitives
.Operations
;
50 Locking_Policy
: constant Character;
51 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
53 -------------------------
54 -- Finalize_Protection --
55 -------------------------
57 procedure Finalize_Protection
(Object
: in out Protection
) is
59 Finalize_Lock
(Object
.L
'Unrestricted_Access);
60 end Finalize_Protection
;
62 ---------------------------
63 -- Initialize_Protection --
64 ---------------------------
66 procedure Initialize_Protection
67 (Object
: Protection_Access
;
68 Ceiling_Priority
: Integer)
70 Init_Priority
: Integer := Ceiling_Priority
;
73 if Init_Priority
= Unspecified_Priority
then
74 Init_Priority
:= System
.Priority
'Last;
77 Initialize_Lock
(Init_Priority
, Object
.L
'Access);
78 Object
.Ceiling
:= System
.Any_Priority
(Init_Priority
);
79 Object
.New_Ceiling
:= System
.Any_Priority
(Init_Priority
);
80 Object
.Owner
:= Null_Task
;
81 end Initialize_Protection
;
88 (Object
: Protection_Access
) return System
.Any_Priority
is
90 return Object
.New_Ceiling
;
97 procedure Lock
(Object
: Protection_Access
) is
98 Ceiling_Violation
: Boolean;
101 -- The lock is made without deferring abort
103 -- Therefore the abort has to be deferred before calling this routine.
104 -- This means that the compiler has to generate a Defer_Abort call
105 -- before the call to Lock.
107 -- The caller is responsible for undeferring abort, and compiler
108 -- generated calls must be protected with cleanup handlers to ensure
109 -- that abort is undeferred in all cases.
111 -- If pragma Detect_Blocking is active then, as described in the ARM
112 -- 9.5.1, par. 15, we must check whether this is an external call on a
113 -- protected subprogram with the same target object as that of the
114 -- protected action that is currently in progress (i.e., if the caller
115 -- is already the protected object's owner). If this is the case hence
116 -- Program_Error must be raised.
118 if Detect_Blocking
and then Object
.Owner
= Self
then
122 Write_Lock
(Object
.L
'Access, Ceiling_Violation
);
124 if Ceiling_Violation
then
128 -- We are entering in a protected action, so that we increase the
129 -- protected object nesting level (if pragma Detect_Blocking is
130 -- active), and update the protected object's owner.
132 if Detect_Blocking
then
134 Self_Id
: constant Task_Id
:= Self
;
136 -- Update the protected object's owner
138 Object
.Owner
:= Self_Id
;
140 -- Increase protected object nesting level
142 Self_Id
.Common
.Protected_Action_Nesting
:=
143 Self_Id
.Common
.Protected_Action_Nesting
+ 1;
152 procedure Lock_Read_Only
(Object
: Protection_Access
) is
153 Ceiling_Violation
: Boolean;
156 -- If pragma Detect_Blocking is active then, as described in the ARM
157 -- 9.5.1, par. 15, we must check whether this is an external call on
158 -- protected subprogram with the same target object as that of the
159 -- protected action that is currently in progress (i.e., if the caller
160 -- is already the protected object's owner). If this is the case hence
161 -- Program_Error must be raised.
163 -- Note that in this case (getting read access), several tasks may have
164 -- read ownership of the protected object, so that this method of
165 -- storing the (single) protected object's owner does not work reliably
166 -- for read locks. However, this is the approach taken for two major
167 -- reasons: first, this function is not currently being used (it is
168 -- provided for possible future use), and second, it largely simplifies
169 -- the implementation.
171 if Detect_Blocking
and then Object
.Owner
= Self
then
175 Read_Lock
(Object
.L
'Access, Ceiling_Violation
);
177 if Ceiling_Violation
then
181 -- We are entering in a protected action, so we increase the protected
182 -- object nesting level (if pragma Detect_Blocking is active).
184 if Detect_Blocking
then
186 Self_Id
: constant Task_Id
:= Self
;
188 -- Update the protected object's owner
190 Object
.Owner
:= Self_Id
;
192 -- Increase protected object nesting level
194 Self_Id
.Common
.Protected_Action_Nesting
:=
195 Self_Id
.Common
.Protected_Action_Nesting
+ 1;
204 procedure Set_Ceiling
205 (Object
: Protection_Access
;
206 Prio
: System
.Any_Priority
) is
208 Object
.New_Ceiling
:= Prio
;
215 procedure Unlock
(Object
: Protection_Access
) is
217 -- We are exiting from a protected action, so that we decrease the
218 -- protected object nesting level (if pragma Detect_Blocking is
219 -- active), and remove ownership of the protected object.
221 if Detect_Blocking
then
223 Self_Id
: constant Task_Id
:= Self
;
226 -- Calls to this procedure can only take place when being within
227 -- a protected action and when the caller is the protected
230 pragma Assert
(Self_Id
.Common
.Protected_Action_Nesting
> 0
231 and then Object
.Owner
= Self_Id
);
233 -- Remove ownership of the protected object
235 Object
.Owner
:= Null_Task
;
237 -- We are exiting from a protected action, so we decrease the
238 -- protected object nesting level.
240 Self_Id
.Common
.Protected_Action_Nesting
:=
241 Self_Id
.Common
.Protected_Action_Nesting
- 1;
245 -- Before releasing the mutex we must actually update its ceiling
246 -- priority if it has been changed.
248 if Object
.New_Ceiling
/= Object
.Ceiling
then
249 if Locking_Policy
= 'C' then
250 System
.Task_Primitives
.Operations
.Set_Ceiling
251 (Object
.L
'Access, Object
.New_Ceiling
);
254 Object
.Ceiling
:= Object
.New_Ceiling
;
257 Unlock
(Object
.L
'Access);
262 -- Ensure that tasking is initialized, as well as tasking soft links
263 -- when using protected objects.
266 System
.Soft_Links
.Tasking
.Init_Tasking_Soft_Links
;
267 end System
.Tasking
.Protected_Objects
;