annotate gcc/ada/libgnarl/s-tassta.adb @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
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 . S T A G E S --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- B o d y --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNARL is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNARL was developed by the GNARL team at Florida State University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 pragma Polling (Off);
kono
parents:
diff changeset
33 -- Turn off polling, we do not want ATC polling to take place during tasking
kono
parents:
diff changeset
34 -- operations. It causes infinite loops and other problems.
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 pragma Partition_Elaboration_Policy (Concurrent);
kono
parents:
diff changeset
37 -- This package only implements the concurrent elaboration policy. This pragma
kono
parents:
diff changeset
38 -- will enforce it (and detect conflicts with user specified policy).
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 with Ada.Exceptions;
kono
parents:
diff changeset
41 with Ada.Unchecked_Deallocation;
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 with System.Interrupt_Management;
kono
parents:
diff changeset
44 with System.Tasking.Debug;
kono
parents:
diff changeset
45 with System.Address_Image;
kono
parents:
diff changeset
46 with System.Task_Primitives;
kono
parents:
diff changeset
47 with System.Task_Primitives.Operations;
kono
parents:
diff changeset
48 with System.Tasking.Utilities;
kono
parents:
diff changeset
49 with System.Tasking.Queuing;
kono
parents:
diff changeset
50 with System.Tasking.Rendezvous;
kono
parents:
diff changeset
51 with System.OS_Primitives;
kono
parents:
diff changeset
52 with System.Secondary_Stack;
kono
parents:
diff changeset
53 with System.Restrictions;
kono
parents:
diff changeset
54 with System.Standard_Library;
kono
parents:
diff changeset
55 with System.Stack_Usage;
kono
parents:
diff changeset
56 with System.Storage_Elements;
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 with System.Soft_Links;
kono
parents:
diff changeset
59 -- These are procedure pointers to non-tasking routines that use task
kono
parents:
diff changeset
60 -- specific data. In the absence of tasking, these routines refer to global
kono
parents:
diff changeset
61 -- data. In the presence of tasking, they must be replaced with pointers to
kono
parents:
diff changeset
62 -- task-specific versions. Also used for Create_TSD, Destroy_TSD, Get_Current
kono
parents:
diff changeset
63 -- _Excep, Finalize_Library_Objects, Task_Termination, Handler.
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 with System.Tasking.Initialization;
kono
parents:
diff changeset
66 pragma Elaborate_All (System.Tasking.Initialization);
kono
parents:
diff changeset
67 -- This insures that tasking is initialized if any tasks are created
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 package body System.Tasking.Stages is
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 package STPO renames System.Task_Primitives.Operations;
kono
parents:
diff changeset
72 package SSL renames System.Soft_Links;
kono
parents:
diff changeset
73 package SSE renames System.Storage_Elements;
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 use Ada.Exceptions;
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 use Parameters;
kono
parents:
diff changeset
78 use Secondary_Stack;
kono
parents:
diff changeset
79 use Task_Primitives;
kono
parents:
diff changeset
80 use Task_Primitives.Operations;
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 -----------------------
kono
parents:
diff changeset
83 -- Local Subprograms --
kono
parents:
diff changeset
84 -----------------------
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 procedure Free is new
kono
parents:
diff changeset
87 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 procedure Trace_Unhandled_Exception_In_Task (Self_Id : Task_Id);
kono
parents:
diff changeset
90 -- This procedure outputs the task specific message for exception
kono
parents:
diff changeset
91 -- tracing purposes.
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 procedure Task_Wrapper (Self_ID : Task_Id);
kono
parents:
diff changeset
94 pragma Convention (C, Task_Wrapper);
kono
parents:
diff changeset
95 -- This is the procedure that is called by the GNULL from the new context
kono
parents:
diff changeset
96 -- when a task is created. It waits for activation and then calls the task
kono
parents:
diff changeset
97 -- body procedure. When the task body procedure completes, it terminates
kono
parents:
diff changeset
98 -- the task.
kono
parents:
diff changeset
99 --
kono
parents:
diff changeset
100 -- The Task_Wrapper's address will be provided to the underlying threads
kono
parents:
diff changeset
101 -- library as the task entry point. Convention C is what makes most sense
kono
parents:
diff changeset
102 -- for that purpose (Export C would make the function globally visible,
kono
parents:
diff changeset
103 -- and affect the link name on which GDB depends). This will in addition
kono
parents:
diff changeset
104 -- trigger an automatic stack alignment suitable for GCC's assumptions if
kono
parents:
diff changeset
105 -- need be.
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 -- "Vulnerable_..." in the procedure names below means they must be called
kono
parents:
diff changeset
108 -- with abort deferred.
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 procedure Vulnerable_Complete_Task (Self_ID : Task_Id);
kono
parents:
diff changeset
111 -- Complete the calling task. This procedure must be called with
kono
parents:
diff changeset
112 -- abort deferred. It should only be called by Complete_Task and
kono
parents:
diff changeset
113 -- Finalize_Global_Tasks (for the environment task).
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 procedure Vulnerable_Complete_Master (Self_ID : Task_Id);
kono
parents:
diff changeset
116 -- Complete the current master of the calling task. This procedure
kono
parents:
diff changeset
117 -- must be called with abort deferred. It should only be called by
kono
parents:
diff changeset
118 -- Vulnerable_Complete_Task and Complete_Master.
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 procedure Vulnerable_Complete_Activation (Self_ID : Task_Id);
kono
parents:
diff changeset
121 -- Signal to Self_ID's activator that Self_ID has completed activation.
kono
parents:
diff changeset
122 -- This procedure must be called with abort deferred.
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124 procedure Abort_Dependents (Self_ID : Task_Id);
kono
parents:
diff changeset
125 -- Abort all the direct dependents of Self at its current master nesting
kono
parents:
diff changeset
126 -- level, plus all of their dependents, transitively. RTS_Lock should be
kono
parents:
diff changeset
127 -- locked by the caller.
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 procedure Vulnerable_Free_Task (T : Task_Id);
kono
parents:
diff changeset
130 -- Recover all runtime system storage associated with the task T. This
kono
parents:
diff changeset
131 -- should only be called after T has terminated and will no longer be
kono
parents:
diff changeset
132 -- referenced.
kono
parents:
diff changeset
133 --
kono
parents:
diff changeset
134 -- For tasks created by an allocator that fails, due to an exception, it is
kono
parents:
diff changeset
135 -- called from Expunge_Unactivated_Tasks.
kono
parents:
diff changeset
136 --
kono
parents:
diff changeset
137 -- Different code is used at master completion, in Terminate_Dependents,
kono
parents:
diff changeset
138 -- due to a need for tighter synchronization with the master.
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 ----------------------
kono
parents:
diff changeset
141 -- Abort_Dependents --
kono
parents:
diff changeset
142 ----------------------
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 procedure Abort_Dependents (Self_ID : Task_Id) is
kono
parents:
diff changeset
145 C : Task_Id;
kono
parents:
diff changeset
146 P : Task_Id;
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 -- Each task C will take care of its own dependents, so there is no
kono
parents:
diff changeset
149 -- need to worry about them here. In fact, it would be wrong to abort
kono
parents:
diff changeset
150 -- indirect dependents here, because we can't distinguish between
kono
parents:
diff changeset
151 -- duplicate master ids. For example, suppose we have three nested
kono
parents:
diff changeset
152 -- task bodies T1,T2,T3. And suppose T1 also calls P which calls Q (and
kono
parents:
diff changeset
153 -- both P and Q are task masters). Q will have the same master id as
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
154 -- Master_Of_Task of T3. Previous versions of this would abort T3 when
111
kono
parents:
diff changeset
155 -- Q calls Complete_Master, which was completely wrong.
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 begin
kono
parents:
diff changeset
158 C := All_Tasks_List;
kono
parents:
diff changeset
159 while C /= null loop
kono
parents:
diff changeset
160 P := C.Common.Parent;
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 if P = Self_ID then
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
163 if C.Master_Of_Task = Self_ID.Master_Within then
111
kono
parents:
diff changeset
164 pragma Debug
kono
parents:
diff changeset
165 (Debug.Trace (Self_ID, "Aborting", 'X', C));
kono
parents:
diff changeset
166 Utilities.Abort_One_Task (Self_ID, C);
kono
parents:
diff changeset
167 C.Dependents_Aborted := True;
kono
parents:
diff changeset
168 end if;
kono
parents:
diff changeset
169 end if;
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 C := C.Common.All_Tasks_Link;
kono
parents:
diff changeset
172 end loop;
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 Self_ID.Dependents_Aborted := True;
kono
parents:
diff changeset
175 end Abort_Dependents;
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 -----------------
kono
parents:
diff changeset
178 -- Abort_Tasks --
kono
parents:
diff changeset
179 -----------------
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 procedure Abort_Tasks (Tasks : Task_List) is
kono
parents:
diff changeset
182 begin
kono
parents:
diff changeset
183 Utilities.Abort_Tasks (Tasks);
kono
parents:
diff changeset
184 end Abort_Tasks;
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 --------------------
kono
parents:
diff changeset
187 -- Activate_Tasks --
kono
parents:
diff changeset
188 --------------------
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 -- Note that locks of activator and activated task are both locked here.
kono
parents:
diff changeset
191 -- This is necessary because C.Common.State and Self.Common.Wait_Count have
kono
parents:
diff changeset
192 -- to be synchronized. This is safe from deadlock because the activator is
kono
parents:
diff changeset
193 -- always created before the activated task. That satisfies our
kono
parents:
diff changeset
194 -- in-order-of-creation ATCB locking policy.
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 -- At one point, we may also lock the parent, if the parent is different
kono
parents:
diff changeset
197 -- from the activator. That is also consistent with the lock ordering
kono
parents:
diff changeset
198 -- policy, since the activator cannot be created before the parent.
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 -- Since we are holding both the activator's lock, and Task_Wrapper locks
kono
parents:
diff changeset
201 -- that before it does anything more than initialize the low-level ATCB
kono
parents:
diff changeset
202 -- components, it should be safe to wait to update the counts until we see
kono
parents:
diff changeset
203 -- that the thread creation is successful.
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 -- If the thread creation fails, we do need to close the entries of the
kono
parents:
diff changeset
206 -- task. The first phase, of dequeuing calls, only requires locking the
kono
parents:
diff changeset
207 -- acceptor's ATCB, but the waking up of the callers requires locking the
kono
parents:
diff changeset
208 -- caller's ATCB. We cannot safely do this while we are holding other
kono
parents:
diff changeset
209 -- locks. Therefore, the queue-clearing operation is done in a separate
kono
parents:
diff changeset
210 -- pass over the activation chain.
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 procedure Activate_Tasks (Chain_Access : Activation_Chain_Access) is
kono
parents:
diff changeset
213 Self_ID : constant Task_Id := STPO.Self;
kono
parents:
diff changeset
214 P : Task_Id;
kono
parents:
diff changeset
215 C : Task_Id;
kono
parents:
diff changeset
216 Next_C, Last_C : Task_Id;
kono
parents:
diff changeset
217 Activate_Prio : System.Any_Priority;
kono
parents:
diff changeset
218 Success : Boolean;
kono
parents:
diff changeset
219 All_Elaborated : Boolean := True;
kono
parents:
diff changeset
220
kono
parents:
diff changeset
221 begin
kono
parents:
diff changeset
222 -- If pragma Detect_Blocking is active, then we must check whether this
kono
parents:
diff changeset
223 -- potentially blocking operation is called from a protected action.
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225 if System.Tasking.Detect_Blocking
kono
parents:
diff changeset
226 and then Self_ID.Common.Protected_Action_Nesting > 0
kono
parents:
diff changeset
227 then
kono
parents:
diff changeset
228 raise Program_Error with "potentially blocking operation";
kono
parents:
diff changeset
229 end if;
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 pragma Debug
kono
parents:
diff changeset
232 (Debug.Trace (Self_ID, "Activate_Tasks", 'C'));
kono
parents:
diff changeset
233
kono
parents:
diff changeset
234 Initialization.Defer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 pragma Assert (Self_ID.Common.Wait_Count = 0);
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 -- Lock RTS_Lock, to prevent activated tasks from racing ahead before
kono
parents:
diff changeset
239 -- we finish activating the chain.
kono
parents:
diff changeset
240
kono
parents:
diff changeset
241 Lock_RTS;
kono
parents:
diff changeset
242
kono
parents:
diff changeset
243 -- Check that all task bodies have been elaborated
kono
parents:
diff changeset
244
kono
parents:
diff changeset
245 C := Chain_Access.T_ID;
kono
parents:
diff changeset
246 Last_C := null;
kono
parents:
diff changeset
247 while C /= null loop
kono
parents:
diff changeset
248 if C.Common.Elaborated /= null
kono
parents:
diff changeset
249 and then not C.Common.Elaborated.all
kono
parents:
diff changeset
250 then
kono
parents:
diff changeset
251 All_Elaborated := False;
kono
parents:
diff changeset
252 end if;
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 -- Reverse the activation chain so that tasks are activated in the
kono
parents:
diff changeset
255 -- same order they're declared.
kono
parents:
diff changeset
256
kono
parents:
diff changeset
257 Next_C := C.Common.Activation_Link;
kono
parents:
diff changeset
258 C.Common.Activation_Link := Last_C;
kono
parents:
diff changeset
259 Last_C := C;
kono
parents:
diff changeset
260 C := Next_C;
kono
parents:
diff changeset
261 end loop;
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 Chain_Access.T_ID := Last_C;
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 if not All_Elaborated then
kono
parents:
diff changeset
266 Unlock_RTS;
kono
parents:
diff changeset
267 Initialization.Undefer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
268 raise Program_Error with "Some tasks have not been elaborated";
kono
parents:
diff changeset
269 end if;
kono
parents:
diff changeset
270
kono
parents:
diff changeset
271 -- Activate all the tasks in the chain. Creation of the thread of
kono
parents:
diff changeset
272 -- control was deferred until activation. So create it now.
kono
parents:
diff changeset
273
kono
parents:
diff changeset
274 C := Chain_Access.T_ID;
kono
parents:
diff changeset
275 while C /= null loop
kono
parents:
diff changeset
276 if C.Common.State /= Terminated then
kono
parents:
diff changeset
277 pragma Assert (C.Common.State = Unactivated);
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279 P := C.Common.Parent;
kono
parents:
diff changeset
280 Write_Lock (P);
kono
parents:
diff changeset
281 Write_Lock (C);
kono
parents:
diff changeset
282
kono
parents:
diff changeset
283 Activate_Prio :=
kono
parents:
diff changeset
284 (if C.Common.Base_Priority < Get_Priority (Self_ID)
kono
parents:
diff changeset
285 then Get_Priority (Self_ID)
kono
parents:
diff changeset
286 else C.Common.Base_Priority);
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 System.Task_Primitives.Operations.Create_Task
kono
parents:
diff changeset
289 (C, Task_Wrapper'Address,
kono
parents:
diff changeset
290 Parameters.Size_Type
kono
parents:
diff changeset
291 (C.Common.Compiler_Data.Pri_Stack_Info.Size),
kono
parents:
diff changeset
292 Activate_Prio, Success);
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 -- There would be a race between the created task and the creator
kono
parents:
diff changeset
295 -- to do the following initialization, if we did not have a
kono
parents:
diff changeset
296 -- Lock/Unlock_RTS pair in the task wrapper to prevent it from
kono
parents:
diff changeset
297 -- racing ahead.
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 if Success then
kono
parents:
diff changeset
300 C.Common.State := Activating;
kono
parents:
diff changeset
301 C.Awake_Count := 1;
kono
parents:
diff changeset
302 C.Alive_Count := 1;
kono
parents:
diff changeset
303 P.Awake_Count := P.Awake_Count + 1;
kono
parents:
diff changeset
304 P.Alive_Count := P.Alive_Count + 1;
kono
parents:
diff changeset
305
kono
parents:
diff changeset
306 if P.Common.State = Master_Completion_Sleep and then
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
307 C.Master_Of_Task = P.Master_Within
111
kono
parents:
diff changeset
308 then
kono
parents:
diff changeset
309 pragma Assert (Self_ID /= P);
kono
parents:
diff changeset
310 P.Common.Wait_Count := P.Common.Wait_Count + 1;
kono
parents:
diff changeset
311 end if;
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 for J in System.Tasking.Debug.Known_Tasks'Range loop
kono
parents:
diff changeset
314 if System.Tasking.Debug.Known_Tasks (J) = null then
kono
parents:
diff changeset
315 System.Tasking.Debug.Known_Tasks (J) := C;
kono
parents:
diff changeset
316 C.Known_Tasks_Index := J;
kono
parents:
diff changeset
317 exit;
kono
parents:
diff changeset
318 end if;
kono
parents:
diff changeset
319 end loop;
kono
parents:
diff changeset
320
kono
parents:
diff changeset
321 if Global_Task_Debug_Event_Set then
kono
parents:
diff changeset
322 Debug.Signal_Debug_Event
kono
parents:
diff changeset
323 (Debug.Debug_Event_Activating, C);
kono
parents:
diff changeset
324 end if;
kono
parents:
diff changeset
325
kono
parents:
diff changeset
326 C.Common.State := Runnable;
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 Unlock (C);
kono
parents:
diff changeset
329 Unlock (P);
kono
parents:
diff changeset
330
kono
parents:
diff changeset
331 else
kono
parents:
diff changeset
332 -- No need to set Awake_Count, State, etc. here since the loop
kono
parents:
diff changeset
333 -- below will do that for any Unactivated tasks.
kono
parents:
diff changeset
334
kono
parents:
diff changeset
335 Unlock (C);
kono
parents:
diff changeset
336 Unlock (P);
kono
parents:
diff changeset
337 Self_ID.Common.Activation_Failed := True;
kono
parents:
diff changeset
338 end if;
kono
parents:
diff changeset
339 end if;
kono
parents:
diff changeset
340
kono
parents:
diff changeset
341 C := C.Common.Activation_Link;
kono
parents:
diff changeset
342 end loop;
kono
parents:
diff changeset
343
kono
parents:
diff changeset
344 if not Single_Lock then
kono
parents:
diff changeset
345 Unlock_RTS;
kono
parents:
diff changeset
346 end if;
kono
parents:
diff changeset
347
kono
parents:
diff changeset
348 -- Close the entries of any tasks that failed thread creation, and count
kono
parents:
diff changeset
349 -- those that have not finished activation.
kono
parents:
diff changeset
350
kono
parents:
diff changeset
351 Write_Lock (Self_ID);
kono
parents:
diff changeset
352 Self_ID.Common.State := Activator_Sleep;
kono
parents:
diff changeset
353
kono
parents:
diff changeset
354 C := Chain_Access.T_ID;
kono
parents:
diff changeset
355 while C /= null loop
kono
parents:
diff changeset
356 Write_Lock (C);
kono
parents:
diff changeset
357
kono
parents:
diff changeset
358 if C.Common.State = Unactivated then
kono
parents:
diff changeset
359 C.Common.Activator := null;
kono
parents:
diff changeset
360 C.Common.State := Terminated;
kono
parents:
diff changeset
361 C.Callable := False;
kono
parents:
diff changeset
362 Utilities.Cancel_Queued_Entry_Calls (C);
kono
parents:
diff changeset
363
kono
parents:
diff changeset
364 elsif C.Common.Activator /= null then
kono
parents:
diff changeset
365 Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1;
kono
parents:
diff changeset
366 end if;
kono
parents:
diff changeset
367
kono
parents:
diff changeset
368 Unlock (C);
kono
parents:
diff changeset
369 P := C.Common.Activation_Link;
kono
parents:
diff changeset
370 C.Common.Activation_Link := null;
kono
parents:
diff changeset
371 C := P;
kono
parents:
diff changeset
372 end loop;
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 -- Wait for the activated tasks to complete activation. It is
kono
parents:
diff changeset
375 -- unsafe to abort any of these tasks until the count goes to zero.
kono
parents:
diff changeset
376
kono
parents:
diff changeset
377 loop
kono
parents:
diff changeset
378 exit when Self_ID.Common.Wait_Count = 0;
kono
parents:
diff changeset
379 Sleep (Self_ID, Activator_Sleep);
kono
parents:
diff changeset
380 end loop;
kono
parents:
diff changeset
381
kono
parents:
diff changeset
382 Self_ID.Common.State := Runnable;
kono
parents:
diff changeset
383 Unlock (Self_ID);
kono
parents:
diff changeset
384
kono
parents:
diff changeset
385 if Single_Lock then
kono
parents:
diff changeset
386 Unlock_RTS;
kono
parents:
diff changeset
387 end if;
kono
parents:
diff changeset
388
kono
parents:
diff changeset
389 -- Remove the tasks from the chain
kono
parents:
diff changeset
390
kono
parents:
diff changeset
391 Chain_Access.T_ID := null;
kono
parents:
diff changeset
392 Initialization.Undefer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
393
kono
parents:
diff changeset
394 if Self_ID.Common.Activation_Failed then
kono
parents:
diff changeset
395 Self_ID.Common.Activation_Failed := False;
kono
parents:
diff changeset
396 raise Tasking_Error with "Failure during activation";
kono
parents:
diff changeset
397 end if;
kono
parents:
diff changeset
398 end Activate_Tasks;
kono
parents:
diff changeset
399
kono
parents:
diff changeset
400 -------------------------
kono
parents:
diff changeset
401 -- Complete_Activation --
kono
parents:
diff changeset
402 -------------------------
kono
parents:
diff changeset
403
kono
parents:
diff changeset
404 procedure Complete_Activation is
kono
parents:
diff changeset
405 Self_ID : constant Task_Id := STPO.Self;
kono
parents:
diff changeset
406
kono
parents:
diff changeset
407 begin
kono
parents:
diff changeset
408 Initialization.Defer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
409
kono
parents:
diff changeset
410 if Single_Lock then
kono
parents:
diff changeset
411 Lock_RTS;
kono
parents:
diff changeset
412 end if;
kono
parents:
diff changeset
413
kono
parents:
diff changeset
414 Vulnerable_Complete_Activation (Self_ID);
kono
parents:
diff changeset
415
kono
parents:
diff changeset
416 if Single_Lock then
kono
parents:
diff changeset
417 Unlock_RTS;
kono
parents:
diff changeset
418 end if;
kono
parents:
diff changeset
419
kono
parents:
diff changeset
420 Initialization.Undefer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
421
kono
parents:
diff changeset
422 -- ??? Why do we need to allow for nested deferral here?
kono
parents:
diff changeset
423
kono
parents:
diff changeset
424 end Complete_Activation;
kono
parents:
diff changeset
425
kono
parents:
diff changeset
426 ---------------------
kono
parents:
diff changeset
427 -- Complete_Master --
kono
parents:
diff changeset
428 ---------------------
kono
parents:
diff changeset
429
kono
parents:
diff changeset
430 procedure Complete_Master is
kono
parents:
diff changeset
431 Self_ID : constant Task_Id := STPO.Self;
kono
parents:
diff changeset
432 begin
kono
parents:
diff changeset
433 pragma Assert
kono
parents:
diff changeset
434 (Self_ID.Deferral_Level > 0
kono
parents:
diff changeset
435 or else not System.Restrictions.Abort_Allowed);
kono
parents:
diff changeset
436 Vulnerable_Complete_Master (Self_ID);
kono
parents:
diff changeset
437 end Complete_Master;
kono
parents:
diff changeset
438
kono
parents:
diff changeset
439 -------------------
kono
parents:
diff changeset
440 -- Complete_Task --
kono
parents:
diff changeset
441 -------------------
kono
parents:
diff changeset
442
kono
parents:
diff changeset
443 -- See comments on Vulnerable_Complete_Task for details
kono
parents:
diff changeset
444
kono
parents:
diff changeset
445 procedure Complete_Task is
kono
parents:
diff changeset
446 Self_ID : constant Task_Id := STPO.Self;
kono
parents:
diff changeset
447
kono
parents:
diff changeset
448 begin
kono
parents:
diff changeset
449 pragma Assert
kono
parents:
diff changeset
450 (Self_ID.Deferral_Level > 0
kono
parents:
diff changeset
451 or else not System.Restrictions.Abort_Allowed);
kono
parents:
diff changeset
452
kono
parents:
diff changeset
453 Vulnerable_Complete_Task (Self_ID);
kono
parents:
diff changeset
454
kono
parents:
diff changeset
455 -- All of our dependents have terminated, never undefer abort again
kono
parents:
diff changeset
456
kono
parents:
diff changeset
457 end Complete_Task;
kono
parents:
diff changeset
458
kono
parents:
diff changeset
459 -----------------
kono
parents:
diff changeset
460 -- Create_Task --
kono
parents:
diff changeset
461 -----------------
kono
parents:
diff changeset
462
kono
parents:
diff changeset
463 -- Compiler interface only. Do not call from within the RTS. This must be
kono
parents:
diff changeset
464 -- called to create a new task.
kono
parents:
diff changeset
465
kono
parents:
diff changeset
466 procedure Create_Task
kono
parents:
diff changeset
467 (Priority : Integer;
kono
parents:
diff changeset
468 Stack_Size : System.Parameters.Size_Type;
kono
parents:
diff changeset
469 Secondary_Stack_Size : System.Parameters.Size_Type;
kono
parents:
diff changeset
470 Task_Info : System.Task_Info.Task_Info_Type;
kono
parents:
diff changeset
471 CPU : Integer;
kono
parents:
diff changeset
472 Relative_Deadline : Ada.Real_Time.Time_Span;
kono
parents:
diff changeset
473 Domain : Dispatching_Domain_Access;
kono
parents:
diff changeset
474 Num_Entries : Task_Entry_Index;
kono
parents:
diff changeset
475 Master : Master_Level;
kono
parents:
diff changeset
476 State : Task_Procedure_Access;
kono
parents:
diff changeset
477 Discriminants : System.Address;
kono
parents:
diff changeset
478 Elaborated : Access_Boolean;
kono
parents:
diff changeset
479 Chain : in out Activation_Chain;
kono
parents:
diff changeset
480 Task_Image : String;
kono
parents:
diff changeset
481 Created_Task : out Task_Id)
kono
parents:
diff changeset
482 is
kono
parents:
diff changeset
483 T, P : Task_Id;
kono
parents:
diff changeset
484 Self_ID : constant Task_Id := STPO.Self;
kono
parents:
diff changeset
485 Success : Boolean;
kono
parents:
diff changeset
486 Base_Priority : System.Any_Priority;
kono
parents:
diff changeset
487 Len : Natural;
kono
parents:
diff changeset
488 Base_CPU : System.Multiprocessors.CPU_Range;
kono
parents:
diff changeset
489
kono
parents:
diff changeset
490 use type System.Multiprocessors.CPU_Range;
kono
parents:
diff changeset
491
kono
parents:
diff changeset
492 pragma Unreferenced (Relative_Deadline);
kono
parents:
diff changeset
493 -- EDF scheduling is not supported by any of the target platforms so
kono
parents:
diff changeset
494 -- this parameter is not passed any further.
kono
parents:
diff changeset
495
kono
parents:
diff changeset
496 begin
kono
parents:
diff changeset
497 -- If Master is greater than the current master, it means that Master
kono
parents:
diff changeset
498 -- has already awaited its dependent tasks. This raises Program_Error,
kono
parents:
diff changeset
499 -- by 4.8(10.3/2). See AI-280. Ignore this check for foreign threads.
kono
parents:
diff changeset
500
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
501 if Self_ID.Master_Of_Task /= Foreign_Task_Level
111
kono
parents:
diff changeset
502 and then Master > Self_ID.Master_Within
kono
parents:
diff changeset
503 then
kono
parents:
diff changeset
504 raise Program_Error with
kono
parents:
diff changeset
505 "create task after awaiting termination";
kono
parents:
diff changeset
506 end if;
kono
parents:
diff changeset
507
kono
parents:
diff changeset
508 -- If pragma Detect_Blocking is active must be checked whether this
kono
parents:
diff changeset
509 -- potentially blocking operation is called from a protected action.
kono
parents:
diff changeset
510
kono
parents:
diff changeset
511 if System.Tasking.Detect_Blocking
kono
parents:
diff changeset
512 and then Self_ID.Common.Protected_Action_Nesting > 0
kono
parents:
diff changeset
513 then
kono
parents:
diff changeset
514 raise Program_Error with "potentially blocking operation";
kono
parents:
diff changeset
515 end if;
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 pragma Debug (Debug.Trace (Self_ID, "Create_Task", 'C'));
kono
parents:
diff changeset
518
kono
parents:
diff changeset
519 Base_Priority :=
kono
parents:
diff changeset
520 (if Priority = Unspecified_Priority
kono
parents:
diff changeset
521 then Self_ID.Common.Base_Priority
kono
parents:
diff changeset
522 else System.Any_Priority (Priority));
kono
parents:
diff changeset
523
kono
parents:
diff changeset
524 -- Legal values of CPU are the special Unspecified_CPU value which is
kono
parents:
diff changeset
525 -- inserted by the compiler for tasks without CPU aspect, and those in
kono
parents:
diff changeset
526 -- the range of CPU_Range but no greater than Number_Of_CPUs. Otherwise
kono
parents:
diff changeset
527 -- the task is defined to have failed, and it becomes a completed task
kono
parents:
diff changeset
528 -- (RM D.16(14/3)).
kono
parents:
diff changeset
529
kono
parents:
diff changeset
530 if CPU /= Unspecified_CPU
kono
parents:
diff changeset
531 and then (CPU < Integer (System.Multiprocessors.CPU_Range'First)
kono
parents:
diff changeset
532 or else
kono
parents:
diff changeset
533 CPU > Integer (System.Multiprocessors.Number_Of_CPUs))
kono
parents:
diff changeset
534 then
kono
parents:
diff changeset
535 raise Tasking_Error with "CPU not in range";
kono
parents:
diff changeset
536
kono
parents:
diff changeset
537 -- Normal CPU affinity
kono
parents:
diff changeset
538
kono
parents:
diff changeset
539 else
kono
parents:
diff changeset
540 -- When the application code says nothing about the task affinity
kono
parents:
diff changeset
541 -- (task without CPU aspect) then the compiler inserts the value
kono
parents:
diff changeset
542 -- Unspecified_CPU which indicates to the run-time library that
kono
parents:
diff changeset
543 -- the task will activate and execute on the same processor as its
kono
parents:
diff changeset
544 -- activating task if the activating task is assigned a processor
kono
parents:
diff changeset
545 -- (RM D.16(14/3)).
kono
parents:
diff changeset
546
kono
parents:
diff changeset
547 Base_CPU :=
kono
parents:
diff changeset
548 (if CPU = Unspecified_CPU
kono
parents:
diff changeset
549 then Self_ID.Common.Base_CPU
kono
parents:
diff changeset
550 else System.Multiprocessors.CPU_Range (CPU));
kono
parents:
diff changeset
551 end if;
kono
parents:
diff changeset
552
kono
parents:
diff changeset
553 -- Find parent P of new Task, via master level number. Independent
kono
parents:
diff changeset
554 -- tasks should have Parent = Environment_Task, and all tasks created
kono
parents:
diff changeset
555 -- by independent tasks are also independent. See, for example,
kono
parents:
diff changeset
556 -- s-interr.adb, where Interrupt_Manager does "new Server_Task". The
kono
parents:
diff changeset
557 -- access type is at library level, so the parent of the Server_Task
kono
parents:
diff changeset
558 -- is Environment_Task.
kono
parents:
diff changeset
559
kono
parents:
diff changeset
560 P := Self_ID;
kono
parents:
diff changeset
561
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
562 if P.Master_Of_Task <= Independent_Task_Level then
111
kono
parents:
diff changeset
563 P := Environment_Task;
kono
parents:
diff changeset
564 else
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
565 while P /= null and then P.Master_Of_Task >= Master loop
111
kono
parents:
diff changeset
566 P := P.Common.Parent;
kono
parents:
diff changeset
567 end loop;
kono
parents:
diff changeset
568 end if;
kono
parents:
diff changeset
569
kono
parents:
diff changeset
570 Initialization.Defer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
571
kono
parents:
diff changeset
572 begin
kono
parents:
diff changeset
573 T := New_ATCB (Num_Entries);
kono
parents:
diff changeset
574 exception
kono
parents:
diff changeset
575 when others =>
kono
parents:
diff changeset
576 Initialization.Undefer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
577 raise Storage_Error with "Cannot allocate task";
kono
parents:
diff changeset
578 end;
kono
parents:
diff changeset
579
kono
parents:
diff changeset
580 -- RTS_Lock is used by Abort_Dependents and Abort_Tasks. Up to this
kono
parents:
diff changeset
581 -- point, it is possible that we may be part of a family of tasks that
kono
parents:
diff changeset
582 -- is being aborted.
kono
parents:
diff changeset
583
kono
parents:
diff changeset
584 Lock_RTS;
kono
parents:
diff changeset
585 Write_Lock (Self_ID);
kono
parents:
diff changeset
586
kono
parents:
diff changeset
587 -- Now, we must check that we have not been aborted. If so, we should
kono
parents:
diff changeset
588 -- give up on creating this task, and simply return.
kono
parents:
diff changeset
589
kono
parents:
diff changeset
590 if not Self_ID.Callable then
kono
parents:
diff changeset
591 pragma Assert (Self_ID.Pending_ATC_Level = 0);
kono
parents:
diff changeset
592 pragma Assert (Self_ID.Pending_Action);
kono
parents:
diff changeset
593 pragma Assert
kono
parents:
diff changeset
594 (Chain.T_ID = null or else Chain.T_ID.Common.State = Unactivated);
kono
parents:
diff changeset
595
kono
parents:
diff changeset
596 Unlock (Self_ID);
kono
parents:
diff changeset
597 Unlock_RTS;
kono
parents:
diff changeset
598 Initialization.Undefer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
599
kono
parents:
diff changeset
600 -- ??? Should never get here
kono
parents:
diff changeset
601
kono
parents:
diff changeset
602 pragma Assert (False);
kono
parents:
diff changeset
603 raise Standard'Abort_Signal;
kono
parents:
diff changeset
604 end if;
kono
parents:
diff changeset
605
kono
parents:
diff changeset
606 Initialize_ATCB (Self_ID, State, Discriminants, P, Elaborated,
kono
parents:
diff changeset
607 Base_Priority, Base_CPU, Domain, Task_Info, Stack_Size, T, Success);
kono
parents:
diff changeset
608
kono
parents:
diff changeset
609 if not Success then
kono
parents:
diff changeset
610 Free (T);
kono
parents:
diff changeset
611 Unlock (Self_ID);
kono
parents:
diff changeset
612 Unlock_RTS;
kono
parents:
diff changeset
613 Initialization.Undefer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
614 raise Storage_Error with "Failed to initialize task";
kono
parents:
diff changeset
615 end if;
kono
parents:
diff changeset
616
kono
parents:
diff changeset
617 if Master = Foreign_Task_Level + 2 then
kono
parents:
diff changeset
618
kono
parents:
diff changeset
619 -- This should not happen, except when a foreign task creates non
kono
parents:
diff changeset
620 -- library-level Ada tasks. In this case, we pretend the master is
kono
parents:
diff changeset
621 -- a regular library level task, otherwise the run-time will get
kono
parents:
diff changeset
622 -- confused when waiting for these tasks to terminate.
kono
parents:
diff changeset
623
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
624 T.Master_Of_Task := Library_Task_Level;
111
kono
parents:
diff changeset
625
kono
parents:
diff changeset
626 else
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
627 T.Master_Of_Task := Master;
111
kono
parents:
diff changeset
628 end if;
kono
parents:
diff changeset
629
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
630 T.Master_Within := T.Master_Of_Task + 1;
111
kono
parents:
diff changeset
631
kono
parents:
diff changeset
632 for L in T.Entry_Calls'Range loop
kono
parents:
diff changeset
633 T.Entry_Calls (L).Self := T;
kono
parents:
diff changeset
634 T.Entry_Calls (L).Level := L;
kono
parents:
diff changeset
635 end loop;
kono
parents:
diff changeset
636
kono
parents:
diff changeset
637 if Task_Image'Length = 0 then
kono
parents:
diff changeset
638 T.Common.Task_Image_Len := 0;
kono
parents:
diff changeset
639 else
kono
parents:
diff changeset
640 Len := 1;
kono
parents:
diff changeset
641 T.Common.Task_Image (1) := Task_Image (Task_Image'First);
kono
parents:
diff changeset
642
kono
parents:
diff changeset
643 -- Remove unwanted blank space generated by 'Image
kono
parents:
diff changeset
644
kono
parents:
diff changeset
645 for J in Task_Image'First + 1 .. Task_Image'Last loop
kono
parents:
diff changeset
646 if Task_Image (J) /= ' '
kono
parents:
diff changeset
647 or else Task_Image (J - 1) /= '('
kono
parents:
diff changeset
648 then
kono
parents:
diff changeset
649 Len := Len + 1;
kono
parents:
diff changeset
650 T.Common.Task_Image (Len) := Task_Image (J);
kono
parents:
diff changeset
651 exit when Len = T.Common.Task_Image'Last;
kono
parents:
diff changeset
652 end if;
kono
parents:
diff changeset
653 end loop;
kono
parents:
diff changeset
654
kono
parents:
diff changeset
655 T.Common.Task_Image_Len := Len;
kono
parents:
diff changeset
656 end if;
kono
parents:
diff changeset
657
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
658 -- Note: we used to have code here to initialize T.Common.Domain, but
111
kono
parents:
diff changeset
659 -- that is not needed, since this is initialized in System.Tasking.
kono
parents:
diff changeset
660
kono
parents:
diff changeset
661 Unlock (Self_ID);
kono
parents:
diff changeset
662 Unlock_RTS;
kono
parents:
diff changeset
663
kono
parents:
diff changeset
664 -- The CPU associated to the task (if any) must belong to the
kono
parents:
diff changeset
665 -- dispatching domain.
kono
parents:
diff changeset
666
kono
parents:
diff changeset
667 if Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU
kono
parents:
diff changeset
668 and then
kono
parents:
diff changeset
669 (Base_CPU not in T.Common.Domain'Range
kono
parents:
diff changeset
670 or else not T.Common.Domain (Base_CPU))
kono
parents:
diff changeset
671 then
kono
parents:
diff changeset
672 Initialization.Undefer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
673 raise Tasking_Error with "CPU not in dispatching domain";
kono
parents:
diff changeset
674 end if;
kono
parents:
diff changeset
675
kono
parents:
diff changeset
676 -- To handle the interaction between pragma CPU and dispatching domains
kono
parents:
diff changeset
677 -- we need to signal that this task is being allocated to a processor.
kono
parents:
diff changeset
678 -- This is needed only for tasks belonging to the system domain (the
kono
parents:
diff changeset
679 -- creation of new dispatching domains can only take processors from the
kono
parents:
diff changeset
680 -- system domain) and only before the environment task calls the main
kono
parents:
diff changeset
681 -- procedure (dispatching domains cannot be created after this).
kono
parents:
diff changeset
682
kono
parents:
diff changeset
683 if Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU
kono
parents:
diff changeset
684 and then T.Common.Domain = System.Tasking.System_Domain
kono
parents:
diff changeset
685 and then not System.Tasking.Dispatching_Domains_Frozen
kono
parents:
diff changeset
686 then
kono
parents:
diff changeset
687 -- Increase the number of tasks attached to the CPU to which this
kono
parents:
diff changeset
688 -- task is being moved.
kono
parents:
diff changeset
689
kono
parents:
diff changeset
690 Dispatching_Domain_Tasks (Base_CPU) :=
kono
parents:
diff changeset
691 Dispatching_Domain_Tasks (Base_CPU) + 1;
kono
parents:
diff changeset
692 end if;
kono
parents:
diff changeset
693
kono
parents:
diff changeset
694 -- Create the secondary stack for the task as early as possible during
kono
parents:
diff changeset
695 -- in the creation of a task, since it may be used by the operation of
kono
parents:
diff changeset
696 -- Ada code within the task.
kono
parents:
diff changeset
697
kono
parents:
diff changeset
698 begin
kono
parents:
diff changeset
699 SSL.Create_TSD (T.Common.Compiler_Data, null, Secondary_Stack_Size);
kono
parents:
diff changeset
700 exception
kono
parents:
diff changeset
701 when others =>
kono
parents:
diff changeset
702 Initialization.Undefer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
703 raise Storage_Error with "Secondary stack could not be allocated";
kono
parents:
diff changeset
704 end;
kono
parents:
diff changeset
705
kono
parents:
diff changeset
706 T.Common.Activation_Link := Chain.T_ID;
kono
parents:
diff changeset
707 Chain.T_ID := T;
kono
parents:
diff changeset
708 Created_Task := T;
kono
parents:
diff changeset
709 Initialization.Undefer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
710
kono
parents:
diff changeset
711 pragma Debug
kono
parents:
diff changeset
712 (Debug.Trace
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
713 (Self_ID, "Created task in " & T.Master_Of_Task'Img, 'C', T));
111
kono
parents:
diff changeset
714 end Create_Task;
kono
parents:
diff changeset
715
kono
parents:
diff changeset
716 --------------------
kono
parents:
diff changeset
717 -- Current_Master --
kono
parents:
diff changeset
718 --------------------
kono
parents:
diff changeset
719
kono
parents:
diff changeset
720 function Current_Master return Master_Level is
kono
parents:
diff changeset
721 begin
kono
parents:
diff changeset
722 return STPO.Self.Master_Within;
kono
parents:
diff changeset
723 end Current_Master;
kono
parents:
diff changeset
724
kono
parents:
diff changeset
725 ------------------
kono
parents:
diff changeset
726 -- Enter_Master --
kono
parents:
diff changeset
727 ------------------
kono
parents:
diff changeset
728
kono
parents:
diff changeset
729 procedure Enter_Master is
kono
parents:
diff changeset
730 Self_ID : constant Task_Id := STPO.Self;
kono
parents:
diff changeset
731 begin
kono
parents:
diff changeset
732 Self_ID.Master_Within := Self_ID.Master_Within + 1;
kono
parents:
diff changeset
733 pragma Debug
kono
parents:
diff changeset
734 (Debug.Trace
kono
parents:
diff changeset
735 (Self_ID, "Enter_Master ->" & Self_ID.Master_Within'Img, 'M'));
kono
parents:
diff changeset
736 end Enter_Master;
kono
parents:
diff changeset
737
kono
parents:
diff changeset
738 -------------------------------
kono
parents:
diff changeset
739 -- Expunge_Unactivated_Tasks --
kono
parents:
diff changeset
740 -------------------------------
kono
parents:
diff changeset
741
kono
parents:
diff changeset
742 -- See procedure Close_Entries for the general case
kono
parents:
diff changeset
743
kono
parents:
diff changeset
744 procedure Expunge_Unactivated_Tasks (Chain : in out Activation_Chain) is
kono
parents:
diff changeset
745 Self_ID : constant Task_Id := STPO.Self;
kono
parents:
diff changeset
746 C : Task_Id;
kono
parents:
diff changeset
747 Call : Entry_Call_Link;
kono
parents:
diff changeset
748 Temp : Task_Id;
kono
parents:
diff changeset
749
kono
parents:
diff changeset
750 begin
kono
parents:
diff changeset
751 pragma Debug
kono
parents:
diff changeset
752 (Debug.Trace (Self_ID, "Expunge_Unactivated_Tasks", 'C'));
kono
parents:
diff changeset
753
kono
parents:
diff changeset
754 Initialization.Defer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
755
kono
parents:
diff changeset
756 -- ???
kono
parents:
diff changeset
757 -- Experimentation has shown that abort is sometimes (but not always)
kono
parents:
diff changeset
758 -- already deferred when this is called.
kono
parents:
diff changeset
759
kono
parents:
diff changeset
760 -- That may indicate an error. Find out what is going on
kono
parents:
diff changeset
761
kono
parents:
diff changeset
762 C := Chain.T_ID;
kono
parents:
diff changeset
763 while C /= null loop
kono
parents:
diff changeset
764 pragma Assert (C.Common.State = Unactivated);
kono
parents:
diff changeset
765
kono
parents:
diff changeset
766 Temp := C.Common.Activation_Link;
kono
parents:
diff changeset
767
kono
parents:
diff changeset
768 if C.Common.State = Unactivated then
kono
parents:
diff changeset
769 Lock_RTS;
kono
parents:
diff changeset
770 Write_Lock (C);
kono
parents:
diff changeset
771
kono
parents:
diff changeset
772 for J in 1 .. C.Entry_Num loop
kono
parents:
diff changeset
773 Queuing.Dequeue_Head (C.Entry_Queues (J), Call);
kono
parents:
diff changeset
774 pragma Assert (Call = null);
kono
parents:
diff changeset
775 end loop;
kono
parents:
diff changeset
776
kono
parents:
diff changeset
777 Unlock (C);
kono
parents:
diff changeset
778
kono
parents:
diff changeset
779 Initialization.Remove_From_All_Tasks_List (C);
kono
parents:
diff changeset
780 Unlock_RTS;
kono
parents:
diff changeset
781
kono
parents:
diff changeset
782 Vulnerable_Free_Task (C);
kono
parents:
diff changeset
783 C := Temp;
kono
parents:
diff changeset
784 end if;
kono
parents:
diff changeset
785 end loop;
kono
parents:
diff changeset
786
kono
parents:
diff changeset
787 Chain.T_ID := null;
kono
parents:
diff changeset
788 Initialization.Undefer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
789 end Expunge_Unactivated_Tasks;
kono
parents:
diff changeset
790
kono
parents:
diff changeset
791 ---------------------------
kono
parents:
diff changeset
792 -- Finalize_Global_Tasks --
kono
parents:
diff changeset
793 ---------------------------
kono
parents:
diff changeset
794
kono
parents:
diff changeset
795 -- ???
kono
parents:
diff changeset
796 -- We have a potential problem here if finalization of global objects does
kono
parents:
diff changeset
797 -- anything with signals or the timer server, since by that time those
kono
parents:
diff changeset
798 -- servers have terminated.
kono
parents:
diff changeset
799
kono
parents:
diff changeset
800 -- It is hard to see how that would occur
kono
parents:
diff changeset
801
kono
parents:
diff changeset
802 -- However, a better solution might be to do all this finalization
kono
parents:
diff changeset
803 -- using the global finalization chain.
kono
parents:
diff changeset
804
kono
parents:
diff changeset
805 procedure Finalize_Global_Tasks is
kono
parents:
diff changeset
806 Self_ID : constant Task_Id := STPO.Self;
kono
parents:
diff changeset
807
kono
parents:
diff changeset
808 Ignore_1 : Boolean;
kono
parents:
diff changeset
809 Ignore_2 : Boolean;
kono
parents:
diff changeset
810
kono
parents:
diff changeset
811 function State
kono
parents:
diff changeset
812 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
kono
parents:
diff changeset
813 pragma Import (C, State, "__gnat_get_interrupt_state");
kono
parents:
diff changeset
814 -- Get interrupt state for interrupt number Int. Defined in init.c
kono
parents:
diff changeset
815
kono
parents:
diff changeset
816 Default : constant Character := 's';
kono
parents:
diff changeset
817 -- 's' Interrupt_State pragma set state to System (use "default"
kono
parents:
diff changeset
818 -- system handler)
kono
parents:
diff changeset
819
kono
parents:
diff changeset
820 begin
kono
parents:
diff changeset
821 if Self_ID.Deferral_Level = 0 then
kono
parents:
diff changeset
822 -- ???
kono
parents:
diff changeset
823 -- In principle, we should be able to predict whether abort is
kono
parents:
diff changeset
824 -- already deferred here (and it should not be deferred yet but in
kono
parents:
diff changeset
825 -- practice it seems Finalize_Global_Tasks is being called sometimes,
kono
parents:
diff changeset
826 -- from RTS code for exceptions, with abort already deferred.
kono
parents:
diff changeset
827
kono
parents:
diff changeset
828 Initialization.Defer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
829
kono
parents:
diff changeset
830 -- Never undefer again
kono
parents:
diff changeset
831 end if;
kono
parents:
diff changeset
832
kono
parents:
diff changeset
833 -- This code is only executed by the environment task
kono
parents:
diff changeset
834
kono
parents:
diff changeset
835 pragma Assert (Self_ID = Environment_Task);
kono
parents:
diff changeset
836
kono
parents:
diff changeset
837 -- Set Environment_Task'Callable to false to notify library-level tasks
kono
parents:
diff changeset
838 -- that it is waiting for them.
kono
parents:
diff changeset
839
kono
parents:
diff changeset
840 Self_ID.Callable := False;
kono
parents:
diff changeset
841
kono
parents:
diff changeset
842 -- Exit level 2 master, for normal tasks in library-level packages
kono
parents:
diff changeset
843
kono
parents:
diff changeset
844 Complete_Master;
kono
parents:
diff changeset
845
kono
parents:
diff changeset
846 -- Force termination of "independent" library-level server tasks
kono
parents:
diff changeset
847
kono
parents:
diff changeset
848 Lock_RTS;
kono
parents:
diff changeset
849
kono
parents:
diff changeset
850 Abort_Dependents (Self_ID);
kono
parents:
diff changeset
851
kono
parents:
diff changeset
852 if not Single_Lock then
kono
parents:
diff changeset
853 Unlock_RTS;
kono
parents:
diff changeset
854 end if;
kono
parents:
diff changeset
855
kono
parents:
diff changeset
856 -- We need to explicitly wait for the task to be terminated here
kono
parents:
diff changeset
857 -- because on true concurrent system, we may end this procedure before
kono
parents:
diff changeset
858 -- the tasks are really terminated.
kono
parents:
diff changeset
859
kono
parents:
diff changeset
860 Write_Lock (Self_ID);
kono
parents:
diff changeset
861
kono
parents:
diff changeset
862 -- If the Abort_Task signal is set to system, it means that we may
kono
parents:
diff changeset
863 -- not have been able to abort all independent tasks (in particular,
kono
parents:
diff changeset
864 -- Server_Task may be blocked, waiting for a signal), in which case, do
kono
parents:
diff changeset
865 -- not wait for Independent_Task_Count to go down to 0. We arbitrarily
kono
parents:
diff changeset
866 -- limit the number of loop iterations; if an independent task does not
kono
parents:
diff changeset
867 -- terminate, we do not want to hang here. In that case, the thread will
kono
parents:
diff changeset
868 -- be terminated when the process exits.
kono
parents:
diff changeset
869
kono
parents:
diff changeset
870 if State (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
kono
parents:
diff changeset
871 then
kono
parents:
diff changeset
872 for J in 1 .. 10 loop
kono
parents:
diff changeset
873 exit when Utilities.Independent_Task_Count = 0;
kono
parents:
diff changeset
874
kono
parents:
diff changeset
875 -- We used to yield here, but this did not take into account low
kono
parents:
diff changeset
876 -- priority tasks that would cause dead lock in some cases (true
kono
parents:
diff changeset
877 -- FIFO scheduling).
kono
parents:
diff changeset
878
kono
parents:
diff changeset
879 Timed_Sleep
kono
parents:
diff changeset
880 (Self_ID, 0.01, System.OS_Primitives.Relative,
kono
parents:
diff changeset
881 Self_ID.Common.State, Ignore_1, Ignore_2);
kono
parents:
diff changeset
882 end loop;
kono
parents:
diff changeset
883 end if;
kono
parents:
diff changeset
884
kono
parents:
diff changeset
885 -- ??? On multi-processor environments, it seems that the above loop
kono
parents:
diff changeset
886 -- isn't sufficient, so we need to add an additional delay.
kono
parents:
diff changeset
887
kono
parents:
diff changeset
888 Timed_Sleep
kono
parents:
diff changeset
889 (Self_ID, 0.01, System.OS_Primitives.Relative,
kono
parents:
diff changeset
890 Self_ID.Common.State, Ignore_1, Ignore_2);
kono
parents:
diff changeset
891
kono
parents:
diff changeset
892 Unlock (Self_ID);
kono
parents:
diff changeset
893
kono
parents:
diff changeset
894 if Single_Lock then
kono
parents:
diff changeset
895 Unlock_RTS;
kono
parents:
diff changeset
896 end if;
kono
parents:
diff changeset
897
kono
parents:
diff changeset
898 -- Complete the environment task
kono
parents:
diff changeset
899
kono
parents:
diff changeset
900 Vulnerable_Complete_Task (Self_ID);
kono
parents:
diff changeset
901
kono
parents:
diff changeset
902 -- Handle normal task termination by the environment task, but only
kono
parents:
diff changeset
903 -- for the normal task termination. In the case of Abnormal and
kono
parents:
diff changeset
904 -- Unhandled_Exception they must have been handled before, and the
kono
parents:
diff changeset
905 -- task termination soft link must have been changed so the task
kono
parents:
diff changeset
906 -- termination routine is not executed twice.
kono
parents:
diff changeset
907
kono
parents:
diff changeset
908 SSL.Task_Termination_Handler.all (Ada.Exceptions.Null_Occurrence);
kono
parents:
diff changeset
909
kono
parents:
diff changeset
910 -- Finalize all library-level controlled objects
kono
parents:
diff changeset
911
kono
parents:
diff changeset
912 if not SSL."=" (SSL.Finalize_Library_Objects, null) then
kono
parents:
diff changeset
913 SSL.Finalize_Library_Objects.all;
kono
parents:
diff changeset
914 end if;
kono
parents:
diff changeset
915
kono
parents:
diff changeset
916 -- Reset the soft links to non-tasking
kono
parents:
diff changeset
917
kono
parents:
diff changeset
918 SSL.Abort_Defer := SSL.Abort_Defer_NT'Access;
kono
parents:
diff changeset
919 SSL.Abort_Undefer := SSL.Abort_Undefer_NT'Access;
kono
parents:
diff changeset
920 SSL.Lock_Task := SSL.Task_Lock_NT'Access;
kono
parents:
diff changeset
921 SSL.Unlock_Task := SSL.Task_Unlock_NT'Access;
kono
parents:
diff changeset
922 SSL.Get_Jmpbuf_Address := SSL.Get_Jmpbuf_Address_NT'Access;
kono
parents:
diff changeset
923 SSL.Set_Jmpbuf_Address := SSL.Set_Jmpbuf_Address_NT'Access;
kono
parents:
diff changeset
924 SSL.Get_Sec_Stack := SSL.Get_Sec_Stack_NT'Access;
kono
parents:
diff changeset
925 SSL.Set_Sec_Stack := SSL.Set_Sec_Stack_NT'Access;
kono
parents:
diff changeset
926 SSL.Check_Abort_Status := SSL.Check_Abort_Status_NT'Access;
kono
parents:
diff changeset
927 SSL.Get_Stack_Info := SSL.Get_Stack_Info_NT'Access;
kono
parents:
diff changeset
928
kono
parents:
diff changeset
929 -- Don't bother trying to finalize Initialization.Global_Task_Lock
kono
parents:
diff changeset
930 -- and System.Task_Primitives.RTS_Lock.
kono
parents:
diff changeset
931
kono
parents:
diff changeset
932 end Finalize_Global_Tasks;
kono
parents:
diff changeset
933
kono
parents:
diff changeset
934 ---------------
kono
parents:
diff changeset
935 -- Free_Task --
kono
parents:
diff changeset
936 ---------------
kono
parents:
diff changeset
937
kono
parents:
diff changeset
938 procedure Free_Task (T : Task_Id) is
kono
parents:
diff changeset
939 Self_Id : constant Task_Id := Self;
kono
parents:
diff changeset
940
kono
parents:
diff changeset
941 begin
kono
parents:
diff changeset
942 if T.Common.State = Terminated then
kono
parents:
diff changeset
943
kono
parents:
diff changeset
944 -- It is not safe to call Abort_Defer or Write_Lock at this stage
kono
parents:
diff changeset
945
kono
parents:
diff changeset
946 Initialization.Task_Lock (Self_Id);
kono
parents:
diff changeset
947
kono
parents:
diff changeset
948 Lock_RTS;
kono
parents:
diff changeset
949 Initialization.Finalize_Attributes (T);
kono
parents:
diff changeset
950 Initialization.Remove_From_All_Tasks_List (T);
kono
parents:
diff changeset
951 Unlock_RTS;
kono
parents:
diff changeset
952
kono
parents:
diff changeset
953 Initialization.Task_Unlock (Self_Id);
kono
parents:
diff changeset
954
kono
parents:
diff changeset
955 System.Task_Primitives.Operations.Finalize_TCB (T);
kono
parents:
diff changeset
956
kono
parents:
diff changeset
957 else
kono
parents:
diff changeset
958 -- If the task is not terminated, then mark the task as to be freed
kono
parents:
diff changeset
959 -- upon termination.
kono
parents:
diff changeset
960
kono
parents:
diff changeset
961 T.Free_On_Termination := True;
kono
parents:
diff changeset
962 end if;
kono
parents:
diff changeset
963 end Free_Task;
kono
parents:
diff changeset
964
kono
parents:
diff changeset
965 ---------------------------
kono
parents:
diff changeset
966 -- Move_Activation_Chain --
kono
parents:
diff changeset
967 ---------------------------
kono
parents:
diff changeset
968
kono
parents:
diff changeset
969 procedure Move_Activation_Chain
kono
parents:
diff changeset
970 (From, To : Activation_Chain_Access;
kono
parents:
diff changeset
971 New_Master : Master_ID)
kono
parents:
diff changeset
972 is
kono
parents:
diff changeset
973 Self_ID : constant Task_Id := STPO.Self;
kono
parents:
diff changeset
974 C : Task_Id;
kono
parents:
diff changeset
975
kono
parents:
diff changeset
976 begin
kono
parents:
diff changeset
977 pragma Debug
kono
parents:
diff changeset
978 (Debug.Trace (Self_ID, "Move_Activation_Chain", 'C'));
kono
parents:
diff changeset
979
kono
parents:
diff changeset
980 -- Nothing to do if From is empty, and we can check that without
kono
parents:
diff changeset
981 -- deferring aborts.
kono
parents:
diff changeset
982
kono
parents:
diff changeset
983 C := From.all.T_ID;
kono
parents:
diff changeset
984
kono
parents:
diff changeset
985 if C = null then
kono
parents:
diff changeset
986 return;
kono
parents:
diff changeset
987 end if;
kono
parents:
diff changeset
988
kono
parents:
diff changeset
989 Initialization.Defer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
990
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
991 -- Loop through the From chain, changing their Master_Of_Task fields,
111
kono
parents:
diff changeset
992 -- and to find the end of the chain.
kono
parents:
diff changeset
993
kono
parents:
diff changeset
994 loop
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
995 C.Master_Of_Task := New_Master;
111
kono
parents:
diff changeset
996 exit when C.Common.Activation_Link = null;
kono
parents:
diff changeset
997 C := C.Common.Activation_Link;
kono
parents:
diff changeset
998 end loop;
kono
parents:
diff changeset
999
kono
parents:
diff changeset
1000 -- Hook From in at the start of To
kono
parents:
diff changeset
1001
kono
parents:
diff changeset
1002 C.Common.Activation_Link := To.all.T_ID;
kono
parents:
diff changeset
1003 To.all.T_ID := From.all.T_ID;
kono
parents:
diff changeset
1004
kono
parents:
diff changeset
1005 -- Set From to empty
kono
parents:
diff changeset
1006
kono
parents:
diff changeset
1007 From.all.T_ID := null;
kono
parents:
diff changeset
1008
kono
parents:
diff changeset
1009 Initialization.Undefer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
1010 end Move_Activation_Chain;
kono
parents:
diff changeset
1011
kono
parents:
diff changeset
1012 ------------------
kono
parents:
diff changeset
1013 -- Task_Wrapper --
kono
parents:
diff changeset
1014 ------------------
kono
parents:
diff changeset
1015
kono
parents:
diff changeset
1016 -- The task wrapper is a procedure that is called first for each task body
kono
parents:
diff changeset
1017 -- and which in turn calls the compiler-generated task body procedure.
kono
parents:
diff changeset
1018 -- The wrapper's main job is to do initialization for the task. It also
kono
parents:
diff changeset
1019 -- has some locally declared objects that serve as per-task local data.
kono
parents:
diff changeset
1020 -- Task finalization is done by Complete_Task, which is called from an
kono
parents:
diff changeset
1021 -- at-end handler that the compiler generates.
kono
parents:
diff changeset
1022
kono
parents:
diff changeset
1023 procedure Task_Wrapper (Self_ID : Task_Id) is
kono
parents:
diff changeset
1024 use System.Standard_Library;
kono
parents:
diff changeset
1025 use System.Stack_Usage;
kono
parents:
diff changeset
1026
kono
parents:
diff changeset
1027 Bottom_Of_Stack : aliased Integer;
kono
parents:
diff changeset
1028
kono
parents:
diff changeset
1029 Task_Alternate_Stack :
kono
parents:
diff changeset
1030 aliased SSE.Storage_Array (1 .. Alternate_Stack_Size);
kono
parents:
diff changeset
1031 -- The alternate signal stack for this task, if any
kono
parents:
diff changeset
1032
kono
parents:
diff changeset
1033 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
kono
parents:
diff changeset
1034 -- Whether to use above alternate signal stack for stack overflows
kono
parents:
diff changeset
1035
kono
parents:
diff changeset
1036 SEH_Table : aliased SSE.Storage_Array (1 .. 8);
kono
parents:
diff changeset
1037 -- Structured Exception Registration table (2 words)
kono
parents:
diff changeset
1038
kono
parents:
diff changeset
1039 procedure Install_SEH_Handler (Addr : System.Address);
kono
parents:
diff changeset
1040 pragma Import (C, Install_SEH_Handler, "__gnat_install_SEH_handler");
kono
parents:
diff changeset
1041 -- Install the SEH (Structured Exception Handling) handler
kono
parents:
diff changeset
1042
kono
parents:
diff changeset
1043 Cause : Cause_Of_Termination := Normal;
kono
parents:
diff changeset
1044 -- Indicates the reason why this task terminates. Normal corresponds to
kono
parents:
diff changeset
1045 -- a task terminating due to completing the last statement of its body,
kono
parents:
diff changeset
1046 -- or as a result of waiting on a terminate alternative. If the task
kono
parents:
diff changeset
1047 -- terminates because it is being aborted then Cause will be set
kono
parents:
diff changeset
1048 -- to Abnormal. If the task terminates because of an exception
kono
parents:
diff changeset
1049 -- raised by the execution of its task body, then Cause is set
kono
parents:
diff changeset
1050 -- to Unhandled_Exception.
kono
parents:
diff changeset
1051
kono
parents:
diff changeset
1052 EO : Exception_Occurrence;
kono
parents:
diff changeset
1053 -- If the task terminates because of an exception raised by the
kono
parents:
diff changeset
1054 -- execution of its task body, then EO will contain the associated
kono
parents:
diff changeset
1055 -- exception occurrence. Otherwise, it will contain Null_Occurrence.
kono
parents:
diff changeset
1056
kono
parents:
diff changeset
1057 TH : Termination_Handler := null;
kono
parents:
diff changeset
1058 -- Pointer to the protected procedure to be executed upon task
kono
parents:
diff changeset
1059 -- termination.
kono
parents:
diff changeset
1060
kono
parents:
diff changeset
1061 procedure Search_Fall_Back_Handler (ID : Task_Id);
kono
parents:
diff changeset
1062 -- Procedure that searches recursively a fall-back handler through the
kono
parents:
diff changeset
1063 -- master relationship. If the handler is found, its pointer is stored
kono
parents:
diff changeset
1064 -- in TH. It stops when the handler is found or when the ID is null.
kono
parents:
diff changeset
1065
kono
parents:
diff changeset
1066 ------------------------------
kono
parents:
diff changeset
1067 -- Search_Fall_Back_Handler --
kono
parents:
diff changeset
1068 ------------------------------
kono
parents:
diff changeset
1069
kono
parents:
diff changeset
1070 procedure Search_Fall_Back_Handler (ID : Task_Id) is
kono
parents:
diff changeset
1071 begin
kono
parents:
diff changeset
1072 -- A null Task_Id indicates that we have reached the root of the
kono
parents:
diff changeset
1073 -- task hierarchy and no handler has been found.
kono
parents:
diff changeset
1074
kono
parents:
diff changeset
1075 if ID = null then
kono
parents:
diff changeset
1076 return;
kono
parents:
diff changeset
1077
kono
parents:
diff changeset
1078 -- If there is a fall back handler, store its pointer for later
kono
parents:
diff changeset
1079 -- execution.
kono
parents:
diff changeset
1080
kono
parents:
diff changeset
1081 elsif ID.Common.Fall_Back_Handler /= null then
kono
parents:
diff changeset
1082 TH := ID.Common.Fall_Back_Handler;
kono
parents:
diff changeset
1083
kono
parents:
diff changeset
1084 -- Otherwise look for a fall back handler in the parent
kono
parents:
diff changeset
1085
kono
parents:
diff changeset
1086 else
kono
parents:
diff changeset
1087 Search_Fall_Back_Handler (ID.Common.Parent);
kono
parents:
diff changeset
1088 end if;
kono
parents:
diff changeset
1089 end Search_Fall_Back_Handler;
kono
parents:
diff changeset
1090
kono
parents:
diff changeset
1091 -- Start of processing for Task_Wrapper
kono
parents:
diff changeset
1092
kono
parents:
diff changeset
1093 begin
kono
parents:
diff changeset
1094 pragma Assert (Self_ID.Deferral_Level = 1);
kono
parents:
diff changeset
1095
kono
parents:
diff changeset
1096 Debug.Master_Hook
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1097 (Self_ID, Self_ID.Common.Parent, Self_ID.Master_Of_Task);
111
kono
parents:
diff changeset
1098
kono
parents:
diff changeset
1099 if Use_Alternate_Stack then
kono
parents:
diff changeset
1100 Self_ID.Common.Task_Alternate_Stack := Task_Alternate_Stack'Address;
kono
parents:
diff changeset
1101 end if;
kono
parents:
diff changeset
1102
kono
parents:
diff changeset
1103 -- Set the guard page at the bottom of the stack. The call to unprotect
kono
parents:
diff changeset
1104 -- the page is done in Terminate_Task
kono
parents:
diff changeset
1105
kono
parents:
diff changeset
1106 Stack_Guard (Self_ID, True);
kono
parents:
diff changeset
1107
kono
parents:
diff changeset
1108 -- Initialize low-level TCB components, that cannot be initialized by
kono
parents:
diff changeset
1109 -- the creator. Enter_Task sets Self_ID.LL.Thread.
kono
parents:
diff changeset
1110
kono
parents:
diff changeset
1111 Enter_Task (Self_ID);
kono
parents:
diff changeset
1112
kono
parents:
diff changeset
1113 -- Initialize dynamic stack usage
kono
parents:
diff changeset
1114
kono
parents:
diff changeset
1115 if System.Stack_Usage.Is_Enabled then
kono
parents:
diff changeset
1116 declare
kono
parents:
diff changeset
1117 Guard_Page_Size : constant := 16 * 1024;
kono
parents:
diff changeset
1118 -- Part of the stack used as a guard page. This is an OS dependent
kono
parents:
diff changeset
1119 -- value, so we need to use the maximum. This value is only used
kono
parents:
diff changeset
1120 -- when the stack address is known, that is currently Windows.
kono
parents:
diff changeset
1121
kono
parents:
diff changeset
1122 Small_Overflow_Guard : constant := 12 * 1024;
kono
parents:
diff changeset
1123 -- Note: this used to be 4K, but was changed to 12K, since
kono
parents:
diff changeset
1124 -- smaller values resulted in segmentation faults from dynamic
kono
parents:
diff changeset
1125 -- stack analysis.
kono
parents:
diff changeset
1126
kono
parents:
diff changeset
1127 Big_Overflow_Guard : constant := 64 * 1024 + 8 * 1024;
kono
parents:
diff changeset
1128 Small_Stack_Limit : constant := 64 * 1024;
kono
parents:
diff changeset
1129 -- ??? These three values are experimental, and seem to work on
kono
parents:
diff changeset
1130 -- most platforms. They still need to be analyzed further. They
kono
parents:
diff changeset
1131 -- also need documentation, what are they and why does the logic
kono
parents:
diff changeset
1132 -- differ depending on whether the stack is large or small???
kono
parents:
diff changeset
1133
kono
parents:
diff changeset
1134 Pattern_Size : Natural :=
kono
parents:
diff changeset
1135 Natural (Self_ID.Common.
kono
parents:
diff changeset
1136 Compiler_Data.Pri_Stack_Info.Size);
kono
parents:
diff changeset
1137 -- Size of the pattern
kono
parents:
diff changeset
1138
kono
parents:
diff changeset
1139 Stack_Base : Address;
kono
parents:
diff changeset
1140 -- Address of the base of the stack
kono
parents:
diff changeset
1141
kono
parents:
diff changeset
1142 begin
kono
parents:
diff changeset
1143 Stack_Base := Self_ID.Common.Compiler_Data.Pri_Stack_Info.Base;
kono
parents:
diff changeset
1144
kono
parents:
diff changeset
1145 if Stack_Base = Null_Address then
kono
parents:
diff changeset
1146
kono
parents:
diff changeset
1147 -- On many platforms, we don't know the real stack base
kono
parents:
diff changeset
1148 -- address. Estimate it using an address in the frame.
kono
parents:
diff changeset
1149
kono
parents:
diff changeset
1150 Stack_Base := Bottom_Of_Stack'Address;
kono
parents:
diff changeset
1151
kono
parents:
diff changeset
1152 -- Adjustments for inner frames
kono
parents:
diff changeset
1153
kono
parents:
diff changeset
1154 Pattern_Size := Pattern_Size -
kono
parents:
diff changeset
1155 (if Pattern_Size < Small_Stack_Limit
kono
parents:
diff changeset
1156 then Small_Overflow_Guard
kono
parents:
diff changeset
1157 else Big_Overflow_Guard);
kono
parents:
diff changeset
1158 else
kono
parents:
diff changeset
1159 -- Reduce by the size of the final guard page
kono
parents:
diff changeset
1160
kono
parents:
diff changeset
1161 Pattern_Size := Pattern_Size - Guard_Page_Size;
kono
parents:
diff changeset
1162 end if;
kono
parents:
diff changeset
1163
kono
parents:
diff changeset
1164 STPO.Lock_RTS;
kono
parents:
diff changeset
1165 Initialize_Analyzer
kono
parents:
diff changeset
1166 (Self_ID.Common.Analyzer,
kono
parents:
diff changeset
1167 Self_ID.Common.Task_Image (1 .. Self_ID.Common.Task_Image_Len),
kono
parents:
diff changeset
1168 Natural (Self_ID.Common.Compiler_Data.Pri_Stack_Info.Size),
kono
parents:
diff changeset
1169 SSE.To_Integer (Stack_Base),
kono
parents:
diff changeset
1170 Pattern_Size);
kono
parents:
diff changeset
1171 STPO.Unlock_RTS;
kono
parents:
diff changeset
1172 Fill_Stack (Self_ID.Common.Analyzer);
kono
parents:
diff changeset
1173 end;
kono
parents:
diff changeset
1174 end if;
kono
parents:
diff changeset
1175
kono
parents:
diff changeset
1176 -- We setup the SEH (Structured Exception Handling) handler if supported
kono
parents:
diff changeset
1177 -- on the target.
kono
parents:
diff changeset
1178
kono
parents:
diff changeset
1179 Install_SEH_Handler (SEH_Table'Address);
kono
parents:
diff changeset
1180
kono
parents:
diff changeset
1181 -- Initialize exception occurrence
kono
parents:
diff changeset
1182
kono
parents:
diff changeset
1183 Save_Occurrence (EO, Ada.Exceptions.Null_Occurrence);
kono
parents:
diff changeset
1184
kono
parents:
diff changeset
1185 -- We lock RTS_Lock to wait for activator to finish activating the rest
kono
parents:
diff changeset
1186 -- of the chain, so that everyone in the chain comes out in priority
kono
parents:
diff changeset
1187 -- order.
kono
parents:
diff changeset
1188
kono
parents:
diff changeset
1189 -- This also protects the value of
kono
parents:
diff changeset
1190 -- Self_ID.Common.Activator.Common.Wait_Count.
kono
parents:
diff changeset
1191
kono
parents:
diff changeset
1192 Lock_RTS;
kono
parents:
diff changeset
1193 Unlock_RTS;
kono
parents:
diff changeset
1194
kono
parents:
diff changeset
1195 if not System.Restrictions.Abort_Allowed then
kono
parents:
diff changeset
1196
kono
parents:
diff changeset
1197 -- If Abort is not allowed, reset the deferral level since it will
kono
parents:
diff changeset
1198 -- not get changed by the generated code. Keeping a default value
kono
parents:
diff changeset
1199 -- of one would prevent some operations (e.g. select or delay) to
kono
parents:
diff changeset
1200 -- proceed successfully.
kono
parents:
diff changeset
1201
kono
parents:
diff changeset
1202 Self_ID.Deferral_Level := 0;
kono
parents:
diff changeset
1203 end if;
kono
parents:
diff changeset
1204
kono
parents:
diff changeset
1205 if Global_Task_Debug_Event_Set then
kono
parents:
diff changeset
1206 Debug.Signal_Debug_Event (Debug.Debug_Event_Run, Self_ID);
kono
parents:
diff changeset
1207 end if;
kono
parents:
diff changeset
1208
kono
parents:
diff changeset
1209 begin
kono
parents:
diff changeset
1210 -- We are separating the following portion of the code in order to
kono
parents:
diff changeset
1211 -- place the exception handlers in a different block. In this way,
kono
parents:
diff changeset
1212 -- we do not call Set_Jmpbuf_Address (which needs Self) before we
kono
parents:
diff changeset
1213 -- set Self in Enter_Task
kono
parents:
diff changeset
1214
kono
parents:
diff changeset
1215 -- Call the task body procedure
kono
parents:
diff changeset
1216
kono
parents:
diff changeset
1217 -- The task body is called with abort still deferred. That
kono
parents:
diff changeset
1218 -- eliminates a dangerous window, for which we had to patch-up in
kono
parents:
diff changeset
1219 -- Terminate_Task.
kono
parents:
diff changeset
1220
kono
parents:
diff changeset
1221 -- During the expansion of the task body, we insert an RTS-call
kono
parents:
diff changeset
1222 -- to Abort_Undefer, at the first point where abort should be
kono
parents:
diff changeset
1223 -- allowed.
kono
parents:
diff changeset
1224
kono
parents:
diff changeset
1225 Self_ID.Common.Task_Entry_Point (Self_ID.Common.Task_Arg);
kono
parents:
diff changeset
1226 Initialization.Defer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
1227
kono
parents:
diff changeset
1228 exception
kono
parents:
diff changeset
1229 -- We can't call Terminate_Task in the exception handlers below,
kono
parents:
diff changeset
1230 -- since there may be (e.g. in the case of GCC exception handling)
kono
parents:
diff changeset
1231 -- clean ups associated with the exception handler that need to
kono
parents:
diff changeset
1232 -- access task specific data.
kono
parents:
diff changeset
1233
kono
parents:
diff changeset
1234 -- Defer abort so that this task can't be aborted while exiting
kono
parents:
diff changeset
1235
kono
parents:
diff changeset
1236 when Standard'Abort_Signal =>
kono
parents:
diff changeset
1237 Initialization.Defer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
1238
kono
parents:
diff changeset
1239 -- Update the cause that motivated the task termination so that
kono
parents:
diff changeset
1240 -- the appropriate information is passed to the task termination
kono
parents:
diff changeset
1241 -- procedure. Task termination as a result of waiting on a
kono
parents:
diff changeset
1242 -- terminate alternative is a normal termination, although it is
kono
parents:
diff changeset
1243 -- implemented using the abort mechanisms.
kono
parents:
diff changeset
1244
kono
parents:
diff changeset
1245 if Self_ID.Terminate_Alternative then
kono
parents:
diff changeset
1246 Cause := Normal;
kono
parents:
diff changeset
1247
kono
parents:
diff changeset
1248 if Global_Task_Debug_Event_Set then
kono
parents:
diff changeset
1249 Debug.Signal_Debug_Event
kono
parents:
diff changeset
1250 (Debug.Debug_Event_Terminated, Self_ID);
kono
parents:
diff changeset
1251 end if;
kono
parents:
diff changeset
1252 else
kono
parents:
diff changeset
1253 Cause := Abnormal;
kono
parents:
diff changeset
1254
kono
parents:
diff changeset
1255 if Global_Task_Debug_Event_Set then
kono
parents:
diff changeset
1256 Debug.Signal_Debug_Event
kono
parents:
diff changeset
1257 (Debug.Debug_Event_Abort_Terminated, Self_ID);
kono
parents:
diff changeset
1258 end if;
kono
parents:
diff changeset
1259 end if;
kono
parents:
diff changeset
1260
kono
parents:
diff changeset
1261 when others =>
kono
parents:
diff changeset
1262 -- ??? Using an E : others here causes CD2C11A to fail on Tru64
kono
parents:
diff changeset
1263
kono
parents:
diff changeset
1264 Initialization.Defer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
1265
kono
parents:
diff changeset
1266 -- Perform the task specific exception tracing duty. We handle
kono
parents:
diff changeset
1267 -- these outputs here and not in the common notification routine
kono
parents:
diff changeset
1268 -- because we need access to tasking related data and we don't
kono
parents:
diff changeset
1269 -- want to drag dependencies against tasking related units in the
kono
parents:
diff changeset
1270 -- the common notification units. Additionally, no trace is ever
kono
parents:
diff changeset
1271 -- triggered from the common routine for the Unhandled_Raise case
kono
parents:
diff changeset
1272 -- in tasks, since an exception never appears unhandled in this
kono
parents:
diff changeset
1273 -- context because of this handler.
kono
parents:
diff changeset
1274
kono
parents:
diff changeset
1275 if Exception_Trace = Unhandled_Raise then
kono
parents:
diff changeset
1276 Trace_Unhandled_Exception_In_Task (Self_ID);
kono
parents:
diff changeset
1277 end if;
kono
parents:
diff changeset
1278
kono
parents:
diff changeset
1279 -- Update the cause that motivated the task termination so that
kono
parents:
diff changeset
1280 -- the appropriate information is passed to the task termination
kono
parents:
diff changeset
1281 -- procedure, as well as the associated Exception_Occurrence.
kono
parents:
diff changeset
1282
kono
parents:
diff changeset
1283 Cause := Unhandled_Exception;
kono
parents:
diff changeset
1284
kono
parents:
diff changeset
1285 Save_Occurrence (EO, SSL.Get_Current_Excep.all.all);
kono
parents:
diff changeset
1286
kono
parents:
diff changeset
1287 if Global_Task_Debug_Event_Set then
kono
parents:
diff changeset
1288 Debug.Signal_Debug_Event
kono
parents:
diff changeset
1289 (Debug.Debug_Event_Exception_Terminated, Self_ID);
kono
parents:
diff changeset
1290 end if;
kono
parents:
diff changeset
1291 end;
kono
parents:
diff changeset
1292
kono
parents:
diff changeset
1293 -- Look for a task termination handler. This code is for all tasks but
kono
parents:
diff changeset
1294 -- the environment task. The task termination code for the environment
kono
parents:
diff changeset
1295 -- task is executed by SSL.Task_Termination_Handler.
kono
parents:
diff changeset
1296
kono
parents:
diff changeset
1297 if Single_Lock then
kono
parents:
diff changeset
1298 Lock_RTS;
kono
parents:
diff changeset
1299 end if;
kono
parents:
diff changeset
1300
kono
parents:
diff changeset
1301 Write_Lock (Self_ID);
kono
parents:
diff changeset
1302
kono
parents:
diff changeset
1303 if Self_ID.Common.Specific_Handler /= null then
kono
parents:
diff changeset
1304 TH := Self_ID.Common.Specific_Handler;
kono
parents:
diff changeset
1305
kono
parents:
diff changeset
1306 -- Independent tasks should not call the Fall_Back_Handler (of the
kono
parents:
diff changeset
1307 -- environment task), because they are implementation artifacts that
kono
parents:
diff changeset
1308 -- should be invisible to Ada programs.
kono
parents:
diff changeset
1309
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1310 elsif Self_ID.Master_Of_Task /= Independent_Task_Level then
111
kono
parents:
diff changeset
1311
kono
parents:
diff changeset
1312 -- Look for a fall-back handler following the master relationship
kono
parents:
diff changeset
1313 -- for the task. As specified in ARM C.7.3 par. 9/2, "the fall-back
kono
parents:
diff changeset
1314 -- handler applies only to the dependent tasks of the task". Hence,
kono
parents:
diff changeset
1315 -- if the terminating tasks (Self_ID) had a fall-back handler, it
kono
parents:
diff changeset
1316 -- would not apply to itself, so we start the search with the parent.
kono
parents:
diff changeset
1317
kono
parents:
diff changeset
1318 Search_Fall_Back_Handler (Self_ID.Common.Parent);
kono
parents:
diff changeset
1319 end if;
kono
parents:
diff changeset
1320
kono
parents:
diff changeset
1321 Unlock (Self_ID);
kono
parents:
diff changeset
1322
kono
parents:
diff changeset
1323 if Single_Lock then
kono
parents:
diff changeset
1324 Unlock_RTS;
kono
parents:
diff changeset
1325 end if;
kono
parents:
diff changeset
1326
kono
parents:
diff changeset
1327 -- Execute the task termination handler if we found it
kono
parents:
diff changeset
1328
kono
parents:
diff changeset
1329 if TH /= null then
kono
parents:
diff changeset
1330 begin
kono
parents:
diff changeset
1331 TH.all (Cause, Self_ID, EO);
kono
parents:
diff changeset
1332
kono
parents:
diff changeset
1333 exception
kono
parents:
diff changeset
1334
kono
parents:
diff changeset
1335 -- RM-C.7.3 requires all exceptions raised here to be ignored
kono
parents:
diff changeset
1336
kono
parents:
diff changeset
1337 when others =>
kono
parents:
diff changeset
1338 null;
kono
parents:
diff changeset
1339 end;
kono
parents:
diff changeset
1340 end if;
kono
parents:
diff changeset
1341
kono
parents:
diff changeset
1342 if System.Stack_Usage.Is_Enabled then
kono
parents:
diff changeset
1343 Compute_Result (Self_ID.Common.Analyzer);
kono
parents:
diff changeset
1344 Report_Result (Self_ID.Common.Analyzer);
kono
parents:
diff changeset
1345 end if;
kono
parents:
diff changeset
1346
kono
parents:
diff changeset
1347 Terminate_Task (Self_ID);
kono
parents:
diff changeset
1348 end Task_Wrapper;
kono
parents:
diff changeset
1349
kono
parents:
diff changeset
1350 --------------------
kono
parents:
diff changeset
1351 -- Terminate_Task --
kono
parents:
diff changeset
1352 --------------------
kono
parents:
diff changeset
1353
kono
parents:
diff changeset
1354 -- Before we allow the thread to exit, we must clean up. This is a delicate
kono
parents:
diff changeset
1355 -- job. We must wake up the task's master, who may immediately try to
kono
parents:
diff changeset
1356 -- deallocate the ATCB from the current task WHILE IT IS STILL EXECUTING.
kono
parents:
diff changeset
1357
kono
parents:
diff changeset
1358 -- To avoid this, the parent task must be blocked up to the latest
kono
parents:
diff changeset
1359 -- statement executed. The trouble is that we have another step that we
kono
parents:
diff changeset
1360 -- also want to postpone to the very end, i.e., calling SSL.Destroy_TSD.
kono
parents:
diff changeset
1361 -- We have to postpone that until the end because compiler-generated code
kono
parents:
diff changeset
1362 -- is likely to try to access that data at just about any point.
kono
parents:
diff changeset
1363
kono
parents:
diff changeset
1364 -- We can't call Destroy_TSD while we are holding any other locks, because
kono
parents:
diff changeset
1365 -- it locks Global_Task_Lock, and our deadlock prevention rules require
kono
parents:
diff changeset
1366 -- that to be the outermost lock. Our first "solution" was to just lock
kono
parents:
diff changeset
1367 -- Global_Task_Lock in addition to the other locks, and force the parent to
kono
parents:
diff changeset
1368 -- also lock this lock between its wakeup and its freeing of the ATCB. See
kono
parents:
diff changeset
1369 -- Complete_Task for the parent-side of the code that has the matching
kono
parents:
diff changeset
1370 -- calls to Task_Lock and Task_Unlock. That was not really a solution,
kono
parents:
diff changeset
1371 -- since the operation Task_Unlock continued to access the ATCB after
kono
parents:
diff changeset
1372 -- unlocking, after which the parent was observed to race ahead, deallocate
kono
parents:
diff changeset
1373 -- the ATCB, and then reallocate it to another task. The call to
kono
parents:
diff changeset
1374 -- Undefer_Abort in Task_Unlock by the "terminated" task was overwriting
kono
parents:
diff changeset
1375 -- the data of the new task that reused the ATCB. To solve this problem, we
kono
parents:
diff changeset
1376 -- introduced the new operation Final_Task_Unlock.
kono
parents:
diff changeset
1377
kono
parents:
diff changeset
1378 procedure Terminate_Task (Self_ID : Task_Id) is
kono
parents:
diff changeset
1379 Environment_Task : constant Task_Id := STPO.Environment_Task;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1380 Master_Of_Task : Integer;
111
kono
parents:
diff changeset
1381 Deallocate : Boolean;
kono
parents:
diff changeset
1382
kono
parents:
diff changeset
1383 begin
kono
parents:
diff changeset
1384 Debug.Task_Termination_Hook;
kono
parents:
diff changeset
1385
kono
parents:
diff changeset
1386 -- Since GCC cannot allocate stack chunks efficiently without reordering
kono
parents:
diff changeset
1387 -- some of the allocations, we have to handle this unexpected situation
kono
parents:
diff changeset
1388 -- here. Normally we never have to call Vulnerable_Complete_Task here.
kono
parents:
diff changeset
1389
kono
parents:
diff changeset
1390 if Self_ID.Common.Activator /= null then
kono
parents:
diff changeset
1391 Vulnerable_Complete_Task (Self_ID);
kono
parents:
diff changeset
1392 end if;
kono
parents:
diff changeset
1393
kono
parents:
diff changeset
1394 Initialization.Task_Lock (Self_ID);
kono
parents:
diff changeset
1395
kono
parents:
diff changeset
1396 if Single_Lock then
kono
parents:
diff changeset
1397 Lock_RTS;
kono
parents:
diff changeset
1398 end if;
kono
parents:
diff changeset
1399
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1400 Master_Of_Task := Self_ID.Master_Of_Task;
111
kono
parents:
diff changeset
1401
kono
parents:
diff changeset
1402 -- Check if the current task is an independent task If so, decrement
kono
parents:
diff changeset
1403 -- the Independent_Task_Count value.
kono
parents:
diff changeset
1404
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1405 if Master_Of_Task = Independent_Task_Level then
111
kono
parents:
diff changeset
1406 if Single_Lock then
kono
parents:
diff changeset
1407 Utilities.Independent_Task_Count :=
kono
parents:
diff changeset
1408 Utilities.Independent_Task_Count - 1;
kono
parents:
diff changeset
1409
kono
parents:
diff changeset
1410 else
kono
parents:
diff changeset
1411 Write_Lock (Environment_Task);
kono
parents:
diff changeset
1412 Utilities.Independent_Task_Count :=
kono
parents:
diff changeset
1413 Utilities.Independent_Task_Count - 1;
kono
parents:
diff changeset
1414 Unlock (Environment_Task);
kono
parents:
diff changeset
1415 end if;
kono
parents:
diff changeset
1416 end if;
kono
parents:
diff changeset
1417
kono
parents:
diff changeset
1418 -- Unprotect the guard page if needed
kono
parents:
diff changeset
1419
kono
parents:
diff changeset
1420 Stack_Guard (Self_ID, False);
kono
parents:
diff changeset
1421
kono
parents:
diff changeset
1422 Utilities.Make_Passive (Self_ID, Task_Completed => True);
kono
parents:
diff changeset
1423 Deallocate := Self_ID.Free_On_Termination;
kono
parents:
diff changeset
1424
kono
parents:
diff changeset
1425 if Single_Lock then
kono
parents:
diff changeset
1426 Unlock_RTS;
kono
parents:
diff changeset
1427 end if;
kono
parents:
diff changeset
1428
kono
parents:
diff changeset
1429 pragma Assert (Check_Exit (Self_ID));
kono
parents:
diff changeset
1430
kono
parents:
diff changeset
1431 SSL.Destroy_TSD (Self_ID.Common.Compiler_Data);
kono
parents:
diff changeset
1432 Initialization.Final_Task_Unlock (Self_ID);
kono
parents:
diff changeset
1433
kono
parents:
diff changeset
1434 -- WARNING: past this point, this thread must assume that the ATCB has
kono
parents:
diff changeset
1435 -- been deallocated, and can't access it anymore (which is why we have
kono
parents:
diff changeset
1436 -- saved the Free_On_Termination flag in a temporary variable).
kono
parents:
diff changeset
1437
kono
parents:
diff changeset
1438 if Deallocate then
kono
parents:
diff changeset
1439 Free_Task (Self_ID);
kono
parents:
diff changeset
1440 end if;
kono
parents:
diff changeset
1441
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1442 if Master_Of_Task > 0 then
111
kono
parents:
diff changeset
1443 STPO.Exit_Task;
kono
parents:
diff changeset
1444 end if;
kono
parents:
diff changeset
1445 end Terminate_Task;
kono
parents:
diff changeset
1446
kono
parents:
diff changeset
1447 ----------------
kono
parents:
diff changeset
1448 -- Terminated --
kono
parents:
diff changeset
1449 ----------------
kono
parents:
diff changeset
1450
kono
parents:
diff changeset
1451 function Terminated (T : Task_Id) return Boolean is
kono
parents:
diff changeset
1452 Self_ID : constant Task_Id := STPO.Self;
kono
parents:
diff changeset
1453 Result : Boolean;
kono
parents:
diff changeset
1454
kono
parents:
diff changeset
1455 begin
kono
parents:
diff changeset
1456 Initialization.Defer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
1457
kono
parents:
diff changeset
1458 if Single_Lock then
kono
parents:
diff changeset
1459 Lock_RTS;
kono
parents:
diff changeset
1460 end if;
kono
parents:
diff changeset
1461
kono
parents:
diff changeset
1462 Write_Lock (T);
kono
parents:
diff changeset
1463 Result := T.Common.State = Terminated;
kono
parents:
diff changeset
1464 Unlock (T);
kono
parents:
diff changeset
1465
kono
parents:
diff changeset
1466 if Single_Lock then
kono
parents:
diff changeset
1467 Unlock_RTS;
kono
parents:
diff changeset
1468 end if;
kono
parents:
diff changeset
1469
kono
parents:
diff changeset
1470 Initialization.Undefer_Abort_Nestable (Self_ID);
kono
parents:
diff changeset
1471 return Result;
kono
parents:
diff changeset
1472 end Terminated;
kono
parents:
diff changeset
1473
kono
parents:
diff changeset
1474 ----------------------------------------
kono
parents:
diff changeset
1475 -- Trace_Unhandled_Exception_In_Task --
kono
parents:
diff changeset
1476 ----------------------------------------
kono
parents:
diff changeset
1477
kono
parents:
diff changeset
1478 procedure Trace_Unhandled_Exception_In_Task (Self_Id : Task_Id) is
kono
parents:
diff changeset
1479 procedure To_Stderr (S : String);
kono
parents:
diff changeset
1480 pragma Import (Ada, To_Stderr, "__gnat_to_stderr");
kono
parents:
diff changeset
1481
kono
parents:
diff changeset
1482 use System.Soft_Links;
kono
parents:
diff changeset
1483
kono
parents:
diff changeset
1484 function To_Address is new
kono
parents:
diff changeset
1485 Ada.Unchecked_Conversion
kono
parents:
diff changeset
1486 (Task_Id, System.Task_Primitives.Task_Address);
kono
parents:
diff changeset
1487
kono
parents:
diff changeset
1488 Excep : constant Exception_Occurrence_Access :=
kono
parents:
diff changeset
1489 SSL.Get_Current_Excep.all;
kono
parents:
diff changeset
1490
kono
parents:
diff changeset
1491 begin
kono
parents:
diff changeset
1492 -- This procedure is called by the task outermost handler in
kono
parents:
diff changeset
1493 -- Task_Wrapper below, so only once the task stack has been fully
kono
parents:
diff changeset
1494 -- unwound. The common notification routine has been called at the
kono
parents:
diff changeset
1495 -- raise point already.
kono
parents:
diff changeset
1496
kono
parents:
diff changeset
1497 -- Lock to prevent unsynchronized output
kono
parents:
diff changeset
1498
kono
parents:
diff changeset
1499 Initialization.Task_Lock (Self_Id);
kono
parents:
diff changeset
1500 To_Stderr ("task ");
kono
parents:
diff changeset
1501
kono
parents:
diff changeset
1502 if Self_Id.Common.Task_Image_Len /= 0 then
kono
parents:
diff changeset
1503 To_Stderr
kono
parents:
diff changeset
1504 (Self_Id.Common.Task_Image (1 .. Self_Id.Common.Task_Image_Len));
kono
parents:
diff changeset
1505 To_Stderr ("_");
kono
parents:
diff changeset
1506 end if;
kono
parents:
diff changeset
1507
kono
parents:
diff changeset
1508 To_Stderr (System.Address_Image (To_Address (Self_Id)));
kono
parents:
diff changeset
1509 To_Stderr (" terminated by unhandled exception");
kono
parents:
diff changeset
1510 To_Stderr ((1 => ASCII.LF));
kono
parents:
diff changeset
1511 To_Stderr (Exception_Information (Excep.all));
kono
parents:
diff changeset
1512 Initialization.Task_Unlock (Self_Id);
kono
parents:
diff changeset
1513 end Trace_Unhandled_Exception_In_Task;
kono
parents:
diff changeset
1514
kono
parents:
diff changeset
1515 ------------------------------------
kono
parents:
diff changeset
1516 -- Vulnerable_Complete_Activation --
kono
parents:
diff changeset
1517 ------------------------------------
kono
parents:
diff changeset
1518
kono
parents:
diff changeset
1519 -- As in several other places, the locks of the activator and activated
kono
parents:
diff changeset
1520 -- task are both locked here. This follows our deadlock prevention lock
kono
parents:
diff changeset
1521 -- ordering policy, since the activated task must be created after the
kono
parents:
diff changeset
1522 -- activator.
kono
parents:
diff changeset
1523
kono
parents:
diff changeset
1524 procedure Vulnerable_Complete_Activation (Self_ID : Task_Id) is
kono
parents:
diff changeset
1525 Activator : constant Task_Id := Self_ID.Common.Activator;
kono
parents:
diff changeset
1526
kono
parents:
diff changeset
1527 begin
kono
parents:
diff changeset
1528 pragma Debug (Debug.Trace (Self_ID, "V_Complete_Activation", 'C'));
kono
parents:
diff changeset
1529
kono
parents:
diff changeset
1530 Write_Lock (Activator);
kono
parents:
diff changeset
1531 Write_Lock (Self_ID);
kono
parents:
diff changeset
1532
kono
parents:
diff changeset
1533 pragma Assert (Self_ID.Common.Activator /= null);
kono
parents:
diff changeset
1534
kono
parents:
diff changeset
1535 -- Remove dangling reference to Activator, since a task may outlive its
kono
parents:
diff changeset
1536 -- activator.
kono
parents:
diff changeset
1537
kono
parents:
diff changeset
1538 Self_ID.Common.Activator := null;
kono
parents:
diff changeset
1539
kono
parents:
diff changeset
1540 -- Wake up the activator, if it is waiting for a chain of tasks to
kono
parents:
diff changeset
1541 -- activate, and we are the last in the chain to complete activation.
kono
parents:
diff changeset
1542
kono
parents:
diff changeset
1543 if Activator.Common.State = Activator_Sleep then
kono
parents:
diff changeset
1544 Activator.Common.Wait_Count := Activator.Common.Wait_Count - 1;
kono
parents:
diff changeset
1545
kono
parents:
diff changeset
1546 if Activator.Common.Wait_Count = 0 then
kono
parents:
diff changeset
1547 Wakeup (Activator, Activator_Sleep);
kono
parents:
diff changeset
1548 end if;
kono
parents:
diff changeset
1549 end if;
kono
parents:
diff changeset
1550
kono
parents:
diff changeset
1551 -- The activator raises a Tasking_Error if any task it is activating
kono
parents:
diff changeset
1552 -- is completed before the activation is done. However, if the reason
kono
parents:
diff changeset
1553 -- for the task completion is an abort, we do not raise an exception.
kono
parents:
diff changeset
1554 -- See RM 9.2(5).
kono
parents:
diff changeset
1555
kono
parents:
diff changeset
1556 if not Self_ID.Callable and then Self_ID.Pending_ATC_Level /= 0 then
kono
parents:
diff changeset
1557 Activator.Common.Activation_Failed := True;
kono
parents:
diff changeset
1558 end if;
kono
parents:
diff changeset
1559
kono
parents:
diff changeset
1560 Unlock (Self_ID);
kono
parents:
diff changeset
1561 Unlock (Activator);
kono
parents:
diff changeset
1562
kono
parents:
diff changeset
1563 -- After the activation, active priority should be the same as base
kono
parents:
diff changeset
1564 -- priority. We must unlock the Activator first, though, since it
kono
parents:
diff changeset
1565 -- should not wait if we have lower priority.
kono
parents:
diff changeset
1566
kono
parents:
diff changeset
1567 if Get_Priority (Self_ID) /= Self_ID.Common.Base_Priority then
kono
parents:
diff changeset
1568 Write_Lock (Self_ID);
kono
parents:
diff changeset
1569 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
kono
parents:
diff changeset
1570 Unlock (Self_ID);
kono
parents:
diff changeset
1571 end if;
kono
parents:
diff changeset
1572 end Vulnerable_Complete_Activation;
kono
parents:
diff changeset
1573
kono
parents:
diff changeset
1574 --------------------------------
kono
parents:
diff changeset
1575 -- Vulnerable_Complete_Master --
kono
parents:
diff changeset
1576 --------------------------------
kono
parents:
diff changeset
1577
kono
parents:
diff changeset
1578 procedure Vulnerable_Complete_Master (Self_ID : Task_Id) is
kono
parents:
diff changeset
1579 C : Task_Id;
kono
parents:
diff changeset
1580 P : Task_Id;
kono
parents:
diff changeset
1581 CM : constant Master_Level := Self_ID.Master_Within;
kono
parents:
diff changeset
1582 T : aliased Task_Id;
kono
parents:
diff changeset
1583
kono
parents:
diff changeset
1584 To_Be_Freed : Task_Id;
kono
parents:
diff changeset
1585 -- This is a list of ATCBs to be freed, after we have released all RTS
kono
parents:
diff changeset
1586 -- locks. This is necessary because of the locking order rules, since
kono
parents:
diff changeset
1587 -- the storage manager uses Global_Task_Lock.
kono
parents:
diff changeset
1588
kono
parents:
diff changeset
1589 pragma Warnings (Off);
kono
parents:
diff changeset
1590 function Check_Unactivated_Tasks return Boolean;
kono
parents:
diff changeset
1591 pragma Warnings (On);
kono
parents:
diff changeset
1592 -- Temporary error-checking code below. This is part of the checks
kono
parents:
diff changeset
1593 -- added in the new run time. Call it only inside a pragma Assert.
kono
parents:
diff changeset
1594
kono
parents:
diff changeset
1595 -----------------------------
kono
parents:
diff changeset
1596 -- Check_Unactivated_Tasks --
kono
parents:
diff changeset
1597 -----------------------------
kono
parents:
diff changeset
1598
kono
parents:
diff changeset
1599 function Check_Unactivated_Tasks return Boolean is
kono
parents:
diff changeset
1600 begin
kono
parents:
diff changeset
1601 if not Single_Lock then
kono
parents:
diff changeset
1602 Lock_RTS;
kono
parents:
diff changeset
1603 end if;
kono
parents:
diff changeset
1604
kono
parents:
diff changeset
1605 Write_Lock (Self_ID);
kono
parents:
diff changeset
1606
kono
parents:
diff changeset
1607 C := All_Tasks_List;
kono
parents:
diff changeset
1608 while C /= null loop
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1609 if C.Common.Activator = Self_ID and then C.Master_Of_Task = CM then
111
kono
parents:
diff changeset
1610 return False;
kono
parents:
diff changeset
1611 end if;
kono
parents:
diff changeset
1612
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1613 if C.Common.Parent = Self_ID and then C.Master_Of_Task = CM then
111
kono
parents:
diff changeset
1614 Write_Lock (C);
kono
parents:
diff changeset
1615
kono
parents:
diff changeset
1616 if C.Common.State = Unactivated then
kono
parents:
diff changeset
1617 return False;
kono
parents:
diff changeset
1618 end if;
kono
parents:
diff changeset
1619
kono
parents:
diff changeset
1620 Unlock (C);
kono
parents:
diff changeset
1621 end if;
kono
parents:
diff changeset
1622
kono
parents:
diff changeset
1623 C := C.Common.All_Tasks_Link;
kono
parents:
diff changeset
1624 end loop;
kono
parents:
diff changeset
1625
kono
parents:
diff changeset
1626 Unlock (Self_ID);
kono
parents:
diff changeset
1627
kono
parents:
diff changeset
1628 if not Single_Lock then
kono
parents:
diff changeset
1629 Unlock_RTS;
kono
parents:
diff changeset
1630 end if;
kono
parents:
diff changeset
1631
kono
parents:
diff changeset
1632 return True;
kono
parents:
diff changeset
1633 end Check_Unactivated_Tasks;
kono
parents:
diff changeset
1634
kono
parents:
diff changeset
1635 -- Start of processing for Vulnerable_Complete_Master
kono
parents:
diff changeset
1636
kono
parents:
diff changeset
1637 begin
kono
parents:
diff changeset
1638 pragma Debug
kono
parents:
diff changeset
1639 (Debug.Trace (Self_ID, "V_Complete_Master(" & CM'Img & ")", 'C'));
kono
parents:
diff changeset
1640
kono
parents:
diff changeset
1641 pragma Assert (Self_ID.Common.Wait_Count = 0);
kono
parents:
diff changeset
1642 pragma Assert
kono
parents:
diff changeset
1643 (Self_ID.Deferral_Level > 0
kono
parents:
diff changeset
1644 or else not System.Restrictions.Abort_Allowed);
kono
parents:
diff changeset
1645
kono
parents:
diff changeset
1646 -- Count how many active dependent tasks this master currently has, and
kono
parents:
diff changeset
1647 -- record this in Wait_Count.
kono
parents:
diff changeset
1648
kono
parents:
diff changeset
1649 -- This count should start at zero, since it is initialized to zero for
kono
parents:
diff changeset
1650 -- new tasks, and the task should not exit the sleep-loops that use this
kono
parents:
diff changeset
1651 -- count until the count reaches zero.
kono
parents:
diff changeset
1652
kono
parents:
diff changeset
1653 -- While we're counting, if we run across any unactivated tasks that
kono
parents:
diff changeset
1654 -- belong to this master, we summarily terminate them as required by
kono
parents:
diff changeset
1655 -- RM-9.2(6).
kono
parents:
diff changeset
1656
kono
parents:
diff changeset
1657 Lock_RTS;
kono
parents:
diff changeset
1658 Write_Lock (Self_ID);
kono
parents:
diff changeset
1659
kono
parents:
diff changeset
1660 C := All_Tasks_List;
kono
parents:
diff changeset
1661 while C /= null loop
kono
parents:
diff changeset
1662
kono
parents:
diff changeset
1663 -- Terminate unactivated (never-to-be activated) tasks
kono
parents:
diff changeset
1664
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1665 if C.Common.Activator = Self_ID and then C.Master_Of_Task = CM then
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1666
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1667 -- Usually, C.Common.Activator = Self_ID implies C.Master_Of_Task
111
kono
parents:
diff changeset
1668 -- = CM. The only case where C is pending activation by this
kono
parents:
diff changeset
1669 -- task, but the master of C is not CM is in Ada 2005, when C is
kono
parents:
diff changeset
1670 -- part of a return object of a build-in-place function.
kono
parents:
diff changeset
1671
kono
parents:
diff changeset
1672 pragma Assert (C.Common.State = Unactivated);
kono
parents:
diff changeset
1673
kono
parents:
diff changeset
1674 Write_Lock (C);
kono
parents:
diff changeset
1675 C.Common.Activator := null;
kono
parents:
diff changeset
1676 C.Common.State := Terminated;
kono
parents:
diff changeset
1677 C.Callable := False;
kono
parents:
diff changeset
1678 Utilities.Cancel_Queued_Entry_Calls (C);
kono
parents:
diff changeset
1679 Unlock (C);
kono
parents:
diff changeset
1680 end if;
kono
parents:
diff changeset
1681
kono
parents:
diff changeset
1682 -- Count it if directly dependent on this master
kono
parents:
diff changeset
1683
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1684 if C.Common.Parent = Self_ID and then C.Master_Of_Task = CM then
111
kono
parents:
diff changeset
1685 Write_Lock (C);
kono
parents:
diff changeset
1686
kono
parents:
diff changeset
1687 if C.Awake_Count /= 0 then
kono
parents:
diff changeset
1688 Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1;
kono
parents:
diff changeset
1689 end if;
kono
parents:
diff changeset
1690
kono
parents:
diff changeset
1691 Unlock (C);
kono
parents:
diff changeset
1692 end if;
kono
parents:
diff changeset
1693
kono
parents:
diff changeset
1694 C := C.Common.All_Tasks_Link;
kono
parents:
diff changeset
1695 end loop;
kono
parents:
diff changeset
1696
kono
parents:
diff changeset
1697 Self_ID.Common.State := Master_Completion_Sleep;
kono
parents:
diff changeset
1698 Unlock (Self_ID);
kono
parents:
diff changeset
1699
kono
parents:
diff changeset
1700 if not Single_Lock then
kono
parents:
diff changeset
1701 Unlock_RTS;
kono
parents:
diff changeset
1702 end if;
kono
parents:
diff changeset
1703
kono
parents:
diff changeset
1704 -- Wait until dependent tasks are all terminated or ready to terminate.
kono
parents:
diff changeset
1705 -- While waiting, the task may be awakened if the task's priority needs
kono
parents:
diff changeset
1706 -- changing, or this master is aborted. In the latter case, we abort the
kono
parents:
diff changeset
1707 -- dependents, and resume waiting until Wait_Count goes to zero.
kono
parents:
diff changeset
1708
kono
parents:
diff changeset
1709 Write_Lock (Self_ID);
kono
parents:
diff changeset
1710
kono
parents:
diff changeset
1711 loop
kono
parents:
diff changeset
1712 exit when Self_ID.Common.Wait_Count = 0;
kono
parents:
diff changeset
1713
kono
parents:
diff changeset
1714 -- Here is a difference as compared to Complete_Master
kono
parents:
diff changeset
1715
kono
parents:
diff changeset
1716 if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
kono
parents:
diff changeset
1717 and then not Self_ID.Dependents_Aborted
kono
parents:
diff changeset
1718 then
kono
parents:
diff changeset
1719 if Single_Lock then
kono
parents:
diff changeset
1720 Abort_Dependents (Self_ID);
kono
parents:
diff changeset
1721 else
kono
parents:
diff changeset
1722 Unlock (Self_ID);
kono
parents:
diff changeset
1723 Lock_RTS;
kono
parents:
diff changeset
1724 Abort_Dependents (Self_ID);
kono
parents:
diff changeset
1725 Unlock_RTS;
kono
parents:
diff changeset
1726 Write_Lock (Self_ID);
kono
parents:
diff changeset
1727 end if;
kono
parents:
diff changeset
1728 else
kono
parents:
diff changeset
1729 pragma Debug
kono
parents:
diff changeset
1730 (Debug.Trace (Self_ID, "master_completion_sleep", 'C'));
kono
parents:
diff changeset
1731 Sleep (Self_ID, Master_Completion_Sleep);
kono
parents:
diff changeset
1732 end if;
kono
parents:
diff changeset
1733 end loop;
kono
parents:
diff changeset
1734
kono
parents:
diff changeset
1735 Self_ID.Common.State := Runnable;
kono
parents:
diff changeset
1736 Unlock (Self_ID);
kono
parents:
diff changeset
1737
kono
parents:
diff changeset
1738 -- Dependents are all terminated or on terminate alternatives. Now,
kono
parents:
diff changeset
1739 -- force those on terminate alternatives to terminate, by aborting them.
kono
parents:
diff changeset
1740
kono
parents:
diff changeset
1741 pragma Assert (Check_Unactivated_Tasks);
kono
parents:
diff changeset
1742
kono
parents:
diff changeset
1743 if Self_ID.Alive_Count > 1 then
kono
parents:
diff changeset
1744 -- ???
kono
parents:
diff changeset
1745 -- Consider finding a way to skip the following extra steps if there
kono
parents:
diff changeset
1746 -- are no dependents with terminate alternatives. This could be done
kono
parents:
diff changeset
1747 -- by adding another count to the ATCB, similar to Awake_Count, but
kono
parents:
diff changeset
1748 -- keeping track of tasks that are on terminate alternatives.
kono
parents:
diff changeset
1749
kono
parents:
diff changeset
1750 pragma Assert (Self_ID.Common.Wait_Count = 0);
kono
parents:
diff changeset
1751
kono
parents:
diff changeset
1752 -- Force any remaining dependents to terminate by aborting them
kono
parents:
diff changeset
1753
kono
parents:
diff changeset
1754 if not Single_Lock then
kono
parents:
diff changeset
1755 Lock_RTS;
kono
parents:
diff changeset
1756 end if;
kono
parents:
diff changeset
1757
kono
parents:
diff changeset
1758 Abort_Dependents (Self_ID);
kono
parents:
diff changeset
1759
kono
parents:
diff changeset
1760 -- Above, when we "abort" the dependents we are simply using this
kono
parents:
diff changeset
1761 -- operation for convenience. We are not required to support the full
kono
parents:
diff changeset
1762 -- abort-statement semantics; in particular, we are not required to
kono
parents:
diff changeset
1763 -- immediately cancel any queued or in-service entry calls. That is
kono
parents:
diff changeset
1764 -- good, because if we tried to cancel a call we would need to lock
kono
parents:
diff changeset
1765 -- the caller, in order to wake the caller up. Our anti-deadlock
kono
parents:
diff changeset
1766 -- rules prevent us from doing that without releasing the locks on C
kono
parents:
diff changeset
1767 -- and Self_ID. Releasing and retaking those locks would be wasteful
kono
parents:
diff changeset
1768 -- at best, and should not be considered further without more
kono
parents:
diff changeset
1769 -- detailed analysis of potential concurrent accesses to the ATCBs
kono
parents:
diff changeset
1770 -- of C and Self_ID.
kono
parents:
diff changeset
1771
kono
parents:
diff changeset
1772 -- Count how many "alive" dependent tasks this master currently has,
kono
parents:
diff changeset
1773 -- and record this in Wait_Count. This count should start at zero,
kono
parents:
diff changeset
1774 -- since it is initialized to zero for new tasks, and the task should
kono
parents:
diff changeset
1775 -- not exit the sleep-loops that use this count until the count
kono
parents:
diff changeset
1776 -- reaches zero.
kono
parents:
diff changeset
1777
kono
parents:
diff changeset
1778 pragma Assert (Self_ID.Common.Wait_Count = 0);
kono
parents:
diff changeset
1779
kono
parents:
diff changeset
1780 Write_Lock (Self_ID);
kono
parents:
diff changeset
1781
kono
parents:
diff changeset
1782 C := All_Tasks_List;
kono
parents:
diff changeset
1783 while C /= null loop
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1784 if C.Common.Parent = Self_ID and then C.Master_Of_Task = CM then
111
kono
parents:
diff changeset
1785 Write_Lock (C);
kono
parents:
diff changeset
1786
kono
parents:
diff changeset
1787 pragma Assert (C.Awake_Count = 0);
kono
parents:
diff changeset
1788
kono
parents:
diff changeset
1789 if C.Alive_Count > 0 then
kono
parents:
diff changeset
1790 pragma Assert (C.Terminate_Alternative);
kono
parents:
diff changeset
1791 Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1;
kono
parents:
diff changeset
1792 end if;
kono
parents:
diff changeset
1793
kono
parents:
diff changeset
1794 Unlock (C);
kono
parents:
diff changeset
1795 end if;
kono
parents:
diff changeset
1796
kono
parents:
diff changeset
1797 C := C.Common.All_Tasks_Link;
kono
parents:
diff changeset
1798 end loop;
kono
parents:
diff changeset
1799
kono
parents:
diff changeset
1800 Self_ID.Common.State := Master_Phase_2_Sleep;
kono
parents:
diff changeset
1801 Unlock (Self_ID);
kono
parents:
diff changeset
1802
kono
parents:
diff changeset
1803 if not Single_Lock then
kono
parents:
diff changeset
1804 Unlock_RTS;
kono
parents:
diff changeset
1805 end if;
kono
parents:
diff changeset
1806
kono
parents:
diff changeset
1807 -- Wait for all counted tasks to finish terminating themselves
kono
parents:
diff changeset
1808
kono
parents:
diff changeset
1809 Write_Lock (Self_ID);
kono
parents:
diff changeset
1810
kono
parents:
diff changeset
1811 loop
kono
parents:
diff changeset
1812 exit when Self_ID.Common.Wait_Count = 0;
kono
parents:
diff changeset
1813 Sleep (Self_ID, Master_Phase_2_Sleep);
kono
parents:
diff changeset
1814 end loop;
kono
parents:
diff changeset
1815
kono
parents:
diff changeset
1816 Self_ID.Common.State := Runnable;
kono
parents:
diff changeset
1817 Unlock (Self_ID);
kono
parents:
diff changeset
1818 end if;
kono
parents:
diff changeset
1819
kono
parents:
diff changeset
1820 -- We don't wake up for abort here. We are already terminating just as
kono
parents:
diff changeset
1821 -- fast as we can, so there is no point.
kono
parents:
diff changeset
1822
kono
parents:
diff changeset
1823 -- Remove terminated tasks from the list of Self_ID's dependents, but
kono
parents:
diff changeset
1824 -- don't free their ATCBs yet, because of lock order restrictions, which
kono
parents:
diff changeset
1825 -- don't allow us to call "free" or "malloc" while holding any other
kono
parents:
diff changeset
1826 -- locks. Instead, we put those ATCBs to be freed onto a temporary list,
kono
parents:
diff changeset
1827 -- called To_Be_Freed.
kono
parents:
diff changeset
1828
kono
parents:
diff changeset
1829 if not Single_Lock then
kono
parents:
diff changeset
1830 Lock_RTS;
kono
parents:
diff changeset
1831 end if;
kono
parents:
diff changeset
1832
kono
parents:
diff changeset
1833 C := All_Tasks_List;
kono
parents:
diff changeset
1834 P := null;
kono
parents:
diff changeset
1835 while C /= null loop
kono
parents:
diff changeset
1836
kono
parents:
diff changeset
1837 -- If Free_On_Termination is set, do nothing here, and let the
kono
parents:
diff changeset
1838 -- task free itself if not already done, otherwise we risk a race
kono
parents:
diff changeset
1839 -- condition where Vulnerable_Free_Task is called in the loop below,
kono
parents:
diff changeset
1840 -- while the task calls Free_Task itself, in Terminate_Task.
kono
parents:
diff changeset
1841
kono
parents:
diff changeset
1842 if C.Common.Parent = Self_ID
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1843 and then C.Master_Of_Task >= CM
111
kono
parents:
diff changeset
1844 and then not C.Free_On_Termination
kono
parents:
diff changeset
1845 then
kono
parents:
diff changeset
1846 if P /= null then
kono
parents:
diff changeset
1847 P.Common.All_Tasks_Link := C.Common.All_Tasks_Link;
kono
parents:
diff changeset
1848 else
kono
parents:
diff changeset
1849 All_Tasks_List := C.Common.All_Tasks_Link;
kono
parents:
diff changeset
1850 end if;
kono
parents:
diff changeset
1851
kono
parents:
diff changeset
1852 T := C.Common.All_Tasks_Link;
kono
parents:
diff changeset
1853 C.Common.All_Tasks_Link := To_Be_Freed;
kono
parents:
diff changeset
1854 To_Be_Freed := C;
kono
parents:
diff changeset
1855 C := T;
kono
parents:
diff changeset
1856
kono
parents:
diff changeset
1857 else
kono
parents:
diff changeset
1858 P := C;
kono
parents:
diff changeset
1859 C := C.Common.All_Tasks_Link;
kono
parents:
diff changeset
1860 end if;
kono
parents:
diff changeset
1861 end loop;
kono
parents:
diff changeset
1862
kono
parents:
diff changeset
1863 Unlock_RTS;
kono
parents:
diff changeset
1864
kono
parents:
diff changeset
1865 -- Free all the ATCBs on the list To_Be_Freed
kono
parents:
diff changeset
1866
kono
parents:
diff changeset
1867 -- The ATCBs in the list are no longer in All_Tasks_List, and after
kono
parents:
diff changeset
1868 -- any interrupt entries are detached from them they should no longer
kono
parents:
diff changeset
1869 -- be referenced.
kono
parents:
diff changeset
1870
kono
parents:
diff changeset
1871 -- Global_Task_Lock (Task_Lock/Unlock) is locked in the loop below to
kono
parents:
diff changeset
1872 -- avoid a race between a terminating task and its parent. The parent
kono
parents:
diff changeset
1873 -- might try to deallocate the ACTB out from underneath the exiting
kono
parents:
diff changeset
1874 -- task. Note that Free will also lock Global_Task_Lock, but that is
kono
parents:
diff changeset
1875 -- OK, since this is the *one* lock for which we have a mechanism to
kono
parents:
diff changeset
1876 -- support nested locking. See Task_Wrapper and its finalizer for more
kono
parents:
diff changeset
1877 -- explanation.
kono
parents:
diff changeset
1878
kono
parents:
diff changeset
1879 -- ???
kono
parents:
diff changeset
1880 -- The check "T.Common.Parent /= null ..." below is to prevent dangling
kono
parents:
diff changeset
1881 -- references to terminated library-level tasks, which could otherwise
kono
parents:
diff changeset
1882 -- occur during finalization of library-level objects. A better solution
kono
parents:
diff changeset
1883 -- might be to hook task objects into the finalization chain and
kono
parents:
diff changeset
1884 -- deallocate the ATCB when the task object is deallocated. However,
kono
parents:
diff changeset
1885 -- this change is not likely to gain anything significant, since all
kono
parents:
diff changeset
1886 -- this storage should be recovered en-masse when the process exits.
kono
parents:
diff changeset
1887
kono
parents:
diff changeset
1888 while To_Be_Freed /= null loop
kono
parents:
diff changeset
1889 T := To_Be_Freed;
kono
parents:
diff changeset
1890 To_Be_Freed := T.Common.All_Tasks_Link;
kono
parents:
diff changeset
1891
kono
parents:
diff changeset
1892 -- ??? On SGI there is currently no Interrupt_Manager, that's why we
kono
parents:
diff changeset
1893 -- need to check if the Interrupt_Manager_ID is null.
kono
parents:
diff changeset
1894
kono
parents:
diff changeset
1895 if T.Interrupt_Entry and then Interrupt_Manager_ID /= null then
kono
parents:
diff changeset
1896 declare
kono
parents:
diff changeset
1897 Detach_Interrupt_Entries_Index : constant Task_Entry_Index := 1;
kono
parents:
diff changeset
1898 -- Corresponds to the entry index of System.Interrupts.
kono
parents:
diff changeset
1899 -- Interrupt_Manager.Detach_Interrupt_Entries. Be sure
kono
parents:
diff changeset
1900 -- to update this value when changing Interrupt_Manager specs.
kono
parents:
diff changeset
1901
kono
parents:
diff changeset
1902 type Param_Type is access all Task_Id;
kono
parents:
diff changeset
1903
kono
parents:
diff changeset
1904 Param : aliased Param_Type := T'Access;
kono
parents:
diff changeset
1905
kono
parents:
diff changeset
1906 begin
kono
parents:
diff changeset
1907 System.Tasking.Rendezvous.Call_Simple
kono
parents:
diff changeset
1908 (Interrupt_Manager_ID, Detach_Interrupt_Entries_Index,
kono
parents:
diff changeset
1909 Param'Address);
kono
parents:
diff changeset
1910 end;
kono
parents:
diff changeset
1911 end if;
kono
parents:
diff changeset
1912
kono
parents:
diff changeset
1913 if (T.Common.Parent /= null
kono
parents:
diff changeset
1914 and then T.Common.Parent.Common.Parent /= null)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1915 or else T.Master_Of_Task > Library_Task_Level
111
kono
parents:
diff changeset
1916 then
kono
parents:
diff changeset
1917 Initialization.Task_Lock (Self_ID);
kono
parents:
diff changeset
1918
kono
parents:
diff changeset
1919 -- If Sec_Stack_Ptr is not null, it means that Destroy_TSD
kono
parents:
diff changeset
1920 -- has not been called yet (case of an unactivated task).
kono
parents:
diff changeset
1921
kono
parents:
diff changeset
1922 if T.Common.Compiler_Data.Sec_Stack_Ptr /= null then
kono
parents:
diff changeset
1923 SSL.Destroy_TSD (T.Common.Compiler_Data);
kono
parents:
diff changeset
1924 end if;
kono
parents:
diff changeset
1925
kono
parents:
diff changeset
1926 Vulnerable_Free_Task (T);
kono
parents:
diff changeset
1927 Initialization.Task_Unlock (Self_ID);
kono
parents:
diff changeset
1928 end if;
kono
parents:
diff changeset
1929 end loop;
kono
parents:
diff changeset
1930
kono
parents:
diff changeset
1931 -- It might seem nice to let the terminated task deallocate its own
kono
parents:
diff changeset
1932 -- ATCB. That would not cover the case of unactivated tasks. It also
kono
parents:
diff changeset
1933 -- would force us to keep the underlying thread around past termination,
kono
parents:
diff changeset
1934 -- since references to the ATCB are possible past termination.
kono
parents:
diff changeset
1935
kono
parents:
diff changeset
1936 -- Currently, we get rid of the thread as soon as the task terminates,
kono
parents:
diff changeset
1937 -- and let the parent recover the ATCB later.
kono
parents:
diff changeset
1938
kono
parents:
diff changeset
1939 -- Some day, if we want to recover the ATCB earlier, at task
kono
parents:
diff changeset
1940 -- termination, we could consider using "fat task IDs", that include the
kono
parents:
diff changeset
1941 -- serial number with the ATCB pointer, to catch references to tasks
kono
parents:
diff changeset
1942 -- that no longer have ATCBs. It is not clear how much this would gain,
kono
parents:
diff changeset
1943 -- since the user-level task object would still be occupying storage.
kono
parents:
diff changeset
1944
kono
parents:
diff changeset
1945 -- Make next master level up active. We don't need to lock the ATCB,
kono
parents:
diff changeset
1946 -- since the value is only updated by each task for itself.
kono
parents:
diff changeset
1947
kono
parents:
diff changeset
1948 Self_ID.Master_Within := CM - 1;
kono
parents:
diff changeset
1949
kono
parents:
diff changeset
1950 Debug.Master_Completed_Hook (Self_ID, CM);
kono
parents:
diff changeset
1951 end Vulnerable_Complete_Master;
kono
parents:
diff changeset
1952
kono
parents:
diff changeset
1953 ------------------------------
kono
parents:
diff changeset
1954 -- Vulnerable_Complete_Task --
kono
parents:
diff changeset
1955 ------------------------------
kono
parents:
diff changeset
1956
kono
parents:
diff changeset
1957 -- Complete the calling task
kono
parents:
diff changeset
1958
kono
parents:
diff changeset
1959 -- This procedure must be called with abort deferred. It should only be
kono
parents:
diff changeset
1960 -- called by Complete_Task and Finalize_Global_Tasks (for the environment
kono
parents:
diff changeset
1961 -- task).
kono
parents:
diff changeset
1962
kono
parents:
diff changeset
1963 -- The effect is similar to that of Complete_Master. Differences include
kono
parents:
diff changeset
1964 -- the closing of entries here, and computation of the number of active
kono
parents:
diff changeset
1965 -- dependent tasks in Complete_Master.
kono
parents:
diff changeset
1966
kono
parents:
diff changeset
1967 -- We don't lock Self_ID before the call to Vulnerable_Complete_Activation,
kono
parents:
diff changeset
1968 -- because that does its own locking, and because we do not need the lock
kono
parents:
diff changeset
1969 -- to test Self_ID.Common.Activator. That value should only be read and
kono
parents:
diff changeset
1970 -- modified by Self.
kono
parents:
diff changeset
1971
kono
parents:
diff changeset
1972 procedure Vulnerable_Complete_Task (Self_ID : Task_Id) is
kono
parents:
diff changeset
1973 begin
kono
parents:
diff changeset
1974 pragma Assert
kono
parents:
diff changeset
1975 (Self_ID.Deferral_Level > 0
kono
parents:
diff changeset
1976 or else not System.Restrictions.Abort_Allowed);
kono
parents:
diff changeset
1977 pragma Assert (Self_ID = Self);
kono
parents:
diff changeset
1978 pragma Assert
kono
parents:
diff changeset
1979 (Self_ID.Master_Within in
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1980 Self_ID.Master_Of_Task .. Self_ID.Master_Of_Task + 3);
111
kono
parents:
diff changeset
1981 pragma Assert (Self_ID.Common.Wait_Count = 0);
kono
parents:
diff changeset
1982 pragma Assert (Self_ID.Open_Accepts = null);
kono
parents:
diff changeset
1983 pragma Assert (Self_ID.ATC_Nesting_Level = 1);
kono
parents:
diff changeset
1984
kono
parents:
diff changeset
1985 pragma Debug (Debug.Trace (Self_ID, "V_Complete_Task", 'C'));
kono
parents:
diff changeset
1986
kono
parents:
diff changeset
1987 if Single_Lock then
kono
parents:
diff changeset
1988 Lock_RTS;
kono
parents:
diff changeset
1989 end if;
kono
parents:
diff changeset
1990
kono
parents:
diff changeset
1991 Write_Lock (Self_ID);
kono
parents:
diff changeset
1992 Self_ID.Callable := False;
kono
parents:
diff changeset
1993
kono
parents:
diff changeset
1994 -- In theory, Self should have no pending entry calls left on its
kono
parents:
diff changeset
1995 -- call-stack. Each async. select statement should clean its own call,
kono
parents:
diff changeset
1996 -- and blocking entry calls should defer abort until the calls are
kono
parents:
diff changeset
1997 -- cancelled, then clean up.
kono
parents:
diff changeset
1998
kono
parents:
diff changeset
1999 Utilities.Cancel_Queued_Entry_Calls (Self_ID);
kono
parents:
diff changeset
2000 Unlock (Self_ID);
kono
parents:
diff changeset
2001
kono
parents:
diff changeset
2002 if Self_ID.Common.Activator /= null then
kono
parents:
diff changeset
2003 Vulnerable_Complete_Activation (Self_ID);
kono
parents:
diff changeset
2004 end if;
kono
parents:
diff changeset
2005
kono
parents:
diff changeset
2006 if Single_Lock then
kono
parents:
diff changeset
2007 Unlock_RTS;
kono
parents:
diff changeset
2008 end if;
kono
parents:
diff changeset
2009
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2010 -- If Self_ID.Master_Within = Self_ID.Master_Of_Task + 2 we may have
111
kono
parents:
diff changeset
2011 -- dependent tasks for which we need to wait. Otherwise we just exit.
kono
parents:
diff changeset
2012
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2013 if Self_ID.Master_Within = Self_ID.Master_Of_Task + 2 then
111
kono
parents:
diff changeset
2014 Vulnerable_Complete_Master (Self_ID);
kono
parents:
diff changeset
2015 end if;
kono
parents:
diff changeset
2016 end Vulnerable_Complete_Task;
kono
parents:
diff changeset
2017
kono
parents:
diff changeset
2018 --------------------------
kono
parents:
diff changeset
2019 -- Vulnerable_Free_Task --
kono
parents:
diff changeset
2020 --------------------------
kono
parents:
diff changeset
2021
kono
parents:
diff changeset
2022 -- Recover all runtime system storage associated with the task T. This
kono
parents:
diff changeset
2023 -- should only be called after T has terminated and will no longer be
kono
parents:
diff changeset
2024 -- referenced.
kono
parents:
diff changeset
2025
kono
parents:
diff changeset
2026 -- For tasks created by an allocator that fails, due to an exception, it
kono
parents:
diff changeset
2027 -- is called from Expunge_Unactivated_Tasks.
kono
parents:
diff changeset
2028
kono
parents:
diff changeset
2029 -- For tasks created by elaboration of task object declarations it is
kono
parents:
diff changeset
2030 -- called from the finalization code of the Task_Wrapper procedure.
kono
parents:
diff changeset
2031
kono
parents:
diff changeset
2032 procedure Vulnerable_Free_Task (T : Task_Id) is
kono
parents:
diff changeset
2033 begin
kono
parents:
diff changeset
2034 pragma Debug (Debug.Trace (Self, "Vulnerable_Free_Task", 'C', T));
kono
parents:
diff changeset
2035
kono
parents:
diff changeset
2036 if Single_Lock then
kono
parents:
diff changeset
2037 Lock_RTS;
kono
parents:
diff changeset
2038 end if;
kono
parents:
diff changeset
2039
kono
parents:
diff changeset
2040 Write_Lock (T);
kono
parents:
diff changeset
2041 Initialization.Finalize_Attributes (T);
kono
parents:
diff changeset
2042 Unlock (T);
kono
parents:
diff changeset
2043
kono
parents:
diff changeset
2044 if Single_Lock then
kono
parents:
diff changeset
2045 Unlock_RTS;
kono
parents:
diff changeset
2046 end if;
kono
parents:
diff changeset
2047
kono
parents:
diff changeset
2048 System.Task_Primitives.Operations.Finalize_TCB (T);
kono
parents:
diff changeset
2049 end Vulnerable_Free_Task;
kono
parents:
diff changeset
2050
kono
parents:
diff changeset
2051 -- Package elaboration code
kono
parents:
diff changeset
2052
kono
parents:
diff changeset
2053 begin
kono
parents:
diff changeset
2054 -- Establish the Adafinal softlink
kono
parents:
diff changeset
2055
kono
parents:
diff changeset
2056 -- This is not done inside the central RTS initialization routine
kono
parents:
diff changeset
2057 -- to avoid with'ing this package from System.Tasking.Initialization.
kono
parents:
diff changeset
2058
kono
parents:
diff changeset
2059 SSL.Adafinal := Finalize_Global_Tasks'Access;
kono
parents:
diff changeset
2060
kono
parents:
diff changeset
2061 -- Establish soft links for subprograms that manipulate master_id's.
kono
parents:
diff changeset
2062 -- This cannot be done when the RTS is initialized, because of various
kono
parents:
diff changeset
2063 -- elaboration constraints.
kono
parents:
diff changeset
2064
kono
parents:
diff changeset
2065 SSL.Current_Master := Stages.Current_Master'Access;
kono
parents:
diff changeset
2066 SSL.Enter_Master := Stages.Enter_Master'Access;
kono
parents:
diff changeset
2067 SSL.Complete_Master := Stages.Complete_Master'Access;
kono
parents:
diff changeset
2068 end System.Tasking.Stages;