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-2018, 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 ------------------------------------------------------------------------------
34 -- Turn off polling, we do not want ATC polling to take place during tasking
35 -- operations. It causes infinite loops and other problems.
37 with System
.Task_Primitives
.Operations
;
38 with System
.Soft_Links
.Tasking
;
40 with System
.Secondary_Stack
;
41 pragma Elaborate_All
(System
.Secondary_Stack
);
42 pragma Unreferenced
(System
.Secondary_Stack
);
43 -- Make sure the body of Secondary_Stack is elaborated before calling
44 -- Init_Tasking_Soft_Links. See comments for this routine for explanation.
46 package body System
.Tasking
.Protected_Objects
is
48 use System
.Task_Primitives
.Operations
;
54 Locking_Policy
: Character;
55 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
57 -------------------------
58 -- Finalize_Protection --
59 -------------------------
61 procedure Finalize_Protection
(Object
: in out Protection
) is
63 Finalize_Lock
(Object
.L
'Unrestricted_Access);
64 end Finalize_Protection
;
66 ---------------------------
67 -- Initialize_Protection --
68 ---------------------------
70 procedure Initialize_Protection
71 (Object
: Protection_Access
;
72 Ceiling_Priority
: Integer)
74 Init_Priority
: Integer := Ceiling_Priority
;
77 if Init_Priority
= Unspecified_Priority
then
78 Init_Priority
:= System
.Priority
'Last;
81 Initialize_Lock
(Init_Priority
, Object
.L
'Access);
82 Object
.Ceiling
:= System
.Any_Priority
(Init_Priority
);
83 Object
.New_Ceiling
:= System
.Any_Priority
(Init_Priority
);
84 Object
.Owner
:= Null_Task
;
85 end Initialize_Protection
;
92 (Object
: Protection_Access
) return System
.Any_Priority
is
94 return Object
.New_Ceiling
;
101 procedure Lock
(Object
: Protection_Access
) is
102 Ceiling_Violation
: Boolean;
105 -- The lock is made without deferring abort
107 -- Therefore the abort has to be deferred before calling this routine.
108 -- This means that the compiler has to generate a Defer_Abort call
109 -- before the call to Lock.
111 -- The caller is responsible for undeferring abort, and compiler
112 -- generated calls must be protected with cleanup handlers to ensure
113 -- that abort is undeferred in all cases.
115 -- If pragma Detect_Blocking is active then, as described in the ARM
116 -- 9.5.1, par. 15, we must check whether this is an external call on a
117 -- protected subprogram with the same target object as that of the
118 -- protected action that is currently in progress (i.e., if the caller
119 -- is already the protected object's owner). If this is the case hence
120 -- Program_Error must be raised.
122 if Detect_Blocking
and then Object
.Owner
= Self
then
126 Write_Lock
(Object
.L
'Access, Ceiling_Violation
);
128 if Ceiling_Violation
then
132 -- We are entering in a protected action, so that we increase the
133 -- protected object nesting level (if pragma Detect_Blocking is
134 -- active), and update the protected object's owner.
136 if Detect_Blocking
then
138 Self_Id
: constant Task_Id
:= Self
;
140 -- Update the protected object's owner
142 Object
.Owner
:= Self_Id
;
144 -- Increase protected object nesting level
146 Self_Id
.Common
.Protected_Action_Nesting
:=
147 Self_Id
.Common
.Protected_Action_Nesting
+ 1;
156 procedure Lock_Read_Only
(Object
: Protection_Access
) is
157 Ceiling_Violation
: Boolean;
160 -- If pragma Detect_Blocking is active then, as described in the ARM
161 -- 9.5.1, par. 15, we must check whether this is an external call on
162 -- protected subprogram with the same target object as that of the
163 -- protected action that is currently in progress (i.e., if the caller
164 -- is already the protected object's owner). If this is the case hence
165 -- Program_Error must be raised.
167 -- Note that in this case (getting read access), several tasks may have
168 -- read ownership of the protected object, so that this method of
169 -- storing the (single) protected object's owner does not work reliably
170 -- for read locks. However, this is the approach taken for two major
171 -- reasons: first, this function is not currently being used (it is
172 -- provided for possible future use), and second, it largely simplifies
173 -- the implementation.
175 if Detect_Blocking
and then Object
.Owner
= Self
then
179 Read_Lock
(Object
.L
'Access, Ceiling_Violation
);
181 if Ceiling_Violation
then
185 -- We are entering in a protected action, so we increase the protected
186 -- object nesting level (if pragma Detect_Blocking is active).
188 if Detect_Blocking
then
190 Self_Id
: constant Task_Id
:= Self
;
192 -- Update the protected object's owner
194 Object
.Owner
:= Self_Id
;
196 -- Increase protected object nesting level
198 Self_Id
.Common
.Protected_Action_Nesting
:=
199 Self_Id
.Common
.Protected_Action_Nesting
+ 1;
208 procedure Set_Ceiling
209 (Object
: Protection_Access
;
210 Prio
: System
.Any_Priority
) is
212 Object
.New_Ceiling
:= Prio
;
219 procedure Unlock
(Object
: Protection_Access
) is
221 -- We are exiting from a protected action, so that we decrease the
222 -- protected object nesting level (if pragma Detect_Blocking is
223 -- active), and remove ownership of the protected object.
225 if Detect_Blocking
then
227 Self_Id
: constant Task_Id
:= Self
;
230 -- Calls to this procedure can only take place when being within
231 -- a protected action and when the caller is the protected
234 pragma Assert
(Self_Id
.Common
.Protected_Action_Nesting
> 0
235 and then Object
.Owner
= Self_Id
);
237 -- Remove ownership of the protected object
239 Object
.Owner
:= Null_Task
;
241 -- We are exiting from a protected action, so we decrease the
242 -- protected object nesting level.
244 Self_Id
.Common
.Protected_Action_Nesting
:=
245 Self_Id
.Common
.Protected_Action_Nesting
- 1;
249 -- Before releasing the mutex we must actually update its ceiling
250 -- priority if it has been changed.
252 if Object
.New_Ceiling
/= Object
.Ceiling
then
253 if Locking_Policy
= 'C' then
254 System
.Task_Primitives
.Operations
.Set_Ceiling
255 (Object
.L
'Access, Object
.New_Ceiling
);
258 Object
.Ceiling
:= Object
.New_Ceiling
;
261 Unlock
(Object
.L
'Access);
266 -- Ensure that tasking is initialized, as well as tasking soft links
267 -- when using protected objects.
270 System
.Soft_Links
.Tasking
.Init_Tasking_Soft_Links
;
271 end System
.Tasking
.Protected_Objects
;