annotate gcc/ada/libgnarl/s-taprob.adb @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
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 --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- B o d y --
kono
parents:
diff changeset
8 -- --
kono
parents:
diff changeset
9 -- Copyright (C) 1991-2017, Florida State University --
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
10 -- Copyright (C) 1995-2019, AdaCore --
111
kono
parents:
diff changeset
11 -- --
kono
parents:
diff changeset
12 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
13 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
14 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
17 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
18 -- --
kono
parents:
diff changeset
19 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
20 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
21 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
22 -- --
kono
parents:
diff changeset
23 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
24 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
25 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
26 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
27 -- --
kono
parents:
diff changeset
28 -- GNARL was developed by the GNARL team at Florida State University. --
kono
parents:
diff changeset
29 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
kono
parents:
diff changeset
30 -- --
kono
parents:
diff changeset
31 ------------------------------------------------------------------------------
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 pragma Polling (Off);
kono
parents:
diff changeset
34 -- Turn off polling, we do not want ATC polling to take place during tasking
kono
parents:
diff changeset
35 -- operations. It causes infinite loops and other problems.
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 with System.Task_Primitives.Operations;
kono
parents:
diff changeset
38 with System.Soft_Links.Tasking;
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 with System.Secondary_Stack;
kono
parents:
diff changeset
41 pragma Elaborate_All (System.Secondary_Stack);
kono
parents:
diff changeset
42 pragma Unreferenced (System.Secondary_Stack);
kono
parents:
diff changeset
43 -- Make sure the body of Secondary_Stack is elaborated before calling
kono
parents:
diff changeset
44 -- Init_Tasking_Soft_Links. See comments for this routine for explanation.
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 package body System.Tasking.Protected_Objects is
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 use System.Task_Primitives.Operations;
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 ----------------
kono
parents:
diff changeset
51 -- Local Data --
kono
parents:
diff changeset
52 ----------------
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 Locking_Policy : Character;
kono
parents:
diff changeset
55 pragma Import (C, Locking_Policy, "__gl_locking_policy");
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 -------------------------
kono
parents:
diff changeset
58 -- Finalize_Protection --
kono
parents:
diff changeset
59 -------------------------
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 procedure Finalize_Protection (Object : in out Protection) is
kono
parents:
diff changeset
62 begin
kono
parents:
diff changeset
63 Finalize_Lock (Object.L'Unrestricted_Access);
kono
parents:
diff changeset
64 end Finalize_Protection;
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 ---------------------------
kono
parents:
diff changeset
67 -- Initialize_Protection --
kono
parents:
diff changeset
68 ---------------------------
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 procedure Initialize_Protection
kono
parents:
diff changeset
71 (Object : Protection_Access;
kono
parents:
diff changeset
72 Ceiling_Priority : Integer)
kono
parents:
diff changeset
73 is
kono
parents:
diff changeset
74 Init_Priority : Integer := Ceiling_Priority;
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 begin
kono
parents:
diff changeset
77 if Init_Priority = Unspecified_Priority then
kono
parents:
diff changeset
78 Init_Priority := System.Priority'Last;
kono
parents:
diff changeset
79 end if;
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 Initialize_Lock (Init_Priority, Object.L'Access);
kono
parents:
diff changeset
82 Object.Ceiling := System.Any_Priority (Init_Priority);
kono
parents:
diff changeset
83 Object.New_Ceiling := System.Any_Priority (Init_Priority);
kono
parents:
diff changeset
84 Object.Owner := Null_Task;
kono
parents:
diff changeset
85 end Initialize_Protection;
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 -----------------
kono
parents:
diff changeset
88 -- Get_Ceiling --
kono
parents:
diff changeset
89 -----------------
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 function Get_Ceiling
kono
parents:
diff changeset
92 (Object : Protection_Access) return System.Any_Priority is
kono
parents:
diff changeset
93 begin
kono
parents:
diff changeset
94 return Object.New_Ceiling;
kono
parents:
diff changeset
95 end Get_Ceiling;
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 ----------
kono
parents:
diff changeset
98 -- Lock --
kono
parents:
diff changeset
99 ----------
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 procedure Lock (Object : Protection_Access) is
kono
parents:
diff changeset
102 Ceiling_Violation : Boolean;
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 begin
kono
parents:
diff changeset
105 -- The lock is made without deferring abort
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 -- Therefore the abort has to be deferred before calling this routine.
kono
parents:
diff changeset
108 -- This means that the compiler has to generate a Defer_Abort call
kono
parents:
diff changeset
109 -- before the call to Lock.
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 -- The caller is responsible for undeferring abort, and compiler
kono
parents:
diff changeset
112 -- generated calls must be protected with cleanup handlers to ensure
kono
parents:
diff changeset
113 -- that abort is undeferred in all cases.
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 -- If pragma Detect_Blocking is active then, as described in the ARM
kono
parents:
diff changeset
116 -- 9.5.1, par. 15, we must check whether this is an external call on a
kono
parents:
diff changeset
117 -- protected subprogram with the same target object as that of the
kono
parents:
diff changeset
118 -- protected action that is currently in progress (i.e., if the caller
kono
parents:
diff changeset
119 -- is already the protected object's owner). If this is the case hence
kono
parents:
diff changeset
120 -- Program_Error must be raised.
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 if Detect_Blocking and then Object.Owner = Self then
kono
parents:
diff changeset
123 raise Program_Error;
kono
parents:
diff changeset
124 end if;
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 Write_Lock (Object.L'Access, Ceiling_Violation);
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 if Ceiling_Violation then
kono
parents:
diff changeset
129 raise Program_Error;
kono
parents:
diff changeset
130 end if;
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 -- We are entering in a protected action, so that we increase the
kono
parents:
diff changeset
133 -- protected object nesting level (if pragma Detect_Blocking is
kono
parents:
diff changeset
134 -- active), and update the protected object's owner.
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 if Detect_Blocking then
kono
parents:
diff changeset
137 declare
kono
parents:
diff changeset
138 Self_Id : constant Task_Id := Self;
kono
parents:
diff changeset
139 begin
kono
parents:
diff changeset
140 -- Update the protected object's owner
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 Object.Owner := Self_Id;
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 -- Increase protected object nesting level
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 Self_Id.Common.Protected_Action_Nesting :=
kono
parents:
diff changeset
147 Self_Id.Common.Protected_Action_Nesting + 1;
kono
parents:
diff changeset
148 end;
kono
parents:
diff changeset
149 end if;
kono
parents:
diff changeset
150 end Lock;
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 --------------------
kono
parents:
diff changeset
153 -- Lock_Read_Only --
kono
parents:
diff changeset
154 --------------------
kono
parents:
diff changeset
155
kono
parents:
diff changeset
156 procedure Lock_Read_Only (Object : Protection_Access) is
kono
parents:
diff changeset
157 Ceiling_Violation : Boolean;
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 begin
kono
parents:
diff changeset
160 -- If pragma Detect_Blocking is active then, as described in the ARM
kono
parents:
diff changeset
161 -- 9.5.1, par. 15, we must check whether this is an external call on
kono
parents:
diff changeset
162 -- protected subprogram with the same target object as that of the
kono
parents:
diff changeset
163 -- protected action that is currently in progress (i.e., if the caller
kono
parents:
diff changeset
164 -- is already the protected object's owner). If this is the case hence
kono
parents:
diff changeset
165 -- Program_Error must be raised.
kono
parents:
diff changeset
166 --
kono
parents:
diff changeset
167 -- Note that in this case (getting read access), several tasks may have
kono
parents:
diff changeset
168 -- read ownership of the protected object, so that this method of
kono
parents:
diff changeset
169 -- storing the (single) protected object's owner does not work reliably
kono
parents:
diff changeset
170 -- for read locks. However, this is the approach taken for two major
kono
parents:
diff changeset
171 -- reasons: first, this function is not currently being used (it is
kono
parents:
diff changeset
172 -- provided for possible future use), and second, it largely simplifies
kono
parents:
diff changeset
173 -- the implementation.
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 if Detect_Blocking and then Object.Owner = Self then
kono
parents:
diff changeset
176 raise Program_Error;
kono
parents:
diff changeset
177 end if;
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 Read_Lock (Object.L'Access, Ceiling_Violation);
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 if Ceiling_Violation then
kono
parents:
diff changeset
182 raise Program_Error;
kono
parents:
diff changeset
183 end if;
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 -- We are entering in a protected action, so we increase the protected
kono
parents:
diff changeset
186 -- object nesting level (if pragma Detect_Blocking is active).
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 if Detect_Blocking then
kono
parents:
diff changeset
189 declare
kono
parents:
diff changeset
190 Self_Id : constant Task_Id := Self;
kono
parents:
diff changeset
191 begin
kono
parents:
diff changeset
192 -- Update the protected object's owner
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 Object.Owner := Self_Id;
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 -- Increase protected object nesting level
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 Self_Id.Common.Protected_Action_Nesting :=
kono
parents:
diff changeset
199 Self_Id.Common.Protected_Action_Nesting + 1;
kono
parents:
diff changeset
200 end;
kono
parents:
diff changeset
201 end if;
kono
parents:
diff changeset
202 end Lock_Read_Only;
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 -----------------
kono
parents:
diff changeset
205 -- Set_Ceiling --
kono
parents:
diff changeset
206 -----------------
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208 procedure Set_Ceiling
kono
parents:
diff changeset
209 (Object : Protection_Access;
kono
parents:
diff changeset
210 Prio : System.Any_Priority) is
kono
parents:
diff changeset
211 begin
kono
parents:
diff changeset
212 Object.New_Ceiling := Prio;
kono
parents:
diff changeset
213 end Set_Ceiling;
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 ------------
kono
parents:
diff changeset
216 -- Unlock --
kono
parents:
diff changeset
217 ------------
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 procedure Unlock (Object : Protection_Access) is
kono
parents:
diff changeset
220 begin
kono
parents:
diff changeset
221 -- We are exiting from a protected action, so that we decrease the
kono
parents:
diff changeset
222 -- protected object nesting level (if pragma Detect_Blocking is
kono
parents:
diff changeset
223 -- active), and remove ownership of the protected object.
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225 if Detect_Blocking then
kono
parents:
diff changeset
226 declare
kono
parents:
diff changeset
227 Self_Id : constant Task_Id := Self;
kono
parents:
diff changeset
228
kono
parents:
diff changeset
229 begin
kono
parents:
diff changeset
230 -- Calls to this procedure can only take place when being within
kono
parents:
diff changeset
231 -- a protected action and when the caller is the protected
kono
parents:
diff changeset
232 -- object's owner.
kono
parents:
diff changeset
233
kono
parents:
diff changeset
234 pragma Assert (Self_Id.Common.Protected_Action_Nesting > 0
kono
parents:
diff changeset
235 and then Object.Owner = Self_Id);
kono
parents:
diff changeset
236
kono
parents:
diff changeset
237 -- Remove ownership of the protected object
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 Object.Owner := Null_Task;
kono
parents:
diff changeset
240
kono
parents:
diff changeset
241 -- We are exiting from a protected action, so we decrease the
kono
parents:
diff changeset
242 -- protected object nesting level.
kono
parents:
diff changeset
243
kono
parents:
diff changeset
244 Self_Id.Common.Protected_Action_Nesting :=
kono
parents:
diff changeset
245 Self_Id.Common.Protected_Action_Nesting - 1;
kono
parents:
diff changeset
246 end;
kono
parents:
diff changeset
247 end if;
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249 -- Before releasing the mutex we must actually update its ceiling
kono
parents:
diff changeset
250 -- priority if it has been changed.
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 if Object.New_Ceiling /= Object.Ceiling then
kono
parents:
diff changeset
253 if Locking_Policy = 'C' then
kono
parents:
diff changeset
254 System.Task_Primitives.Operations.Set_Ceiling
kono
parents:
diff changeset
255 (Object.L'Access, Object.New_Ceiling);
kono
parents:
diff changeset
256 end if;
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 Object.Ceiling := Object.New_Ceiling;
kono
parents:
diff changeset
259 end if;
kono
parents:
diff changeset
260
kono
parents:
diff changeset
261 Unlock (Object.L'Access);
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 end Unlock;
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 begin
kono
parents:
diff changeset
266 -- Ensure that tasking is initialized, as well as tasking soft links
kono
parents:
diff changeset
267 -- when using protected objects.
kono
parents:
diff changeset
268
kono
parents:
diff changeset
269 Tasking.Initialize;
kono
parents:
diff changeset
270 System.Soft_Links.Tasking.Init_Tasking_Soft_Links;
kono
parents:
diff changeset
271 end System.Tasking.Protected_Objects;