* configure.ac: Don't test for [build] __cxa_atexit when building a
[official-gcc.git] / gcc / ada / s-tassta.adb
blob784dade88d81c5d4ff8c00c0a425d0eefa3f2a66
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . T A S K I N G . S T A G E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2004, Free Software Foundation, Inc. --
10 -- --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 pragma Polling (Off);
35 -- Turn off polling, we do not want ATC polling to take place during
36 -- tasking operations. It causes infinite loops and other problems.
38 with Ada.Exceptions;
39 -- used for Raise_Exception
41 with System.Tasking.Debug;
42 -- used for enabling tasking facilities with gdb
44 with System.Address_Image;
45 -- used for the function itself.
47 with System.Parameters;
48 -- used for Size_Type
49 -- Single_Lock
50 -- Runtime_Traces
52 with System.Task_Info;
53 -- used for Task_Info_Type
55 with System.Task_Primitives.Operations;
56 -- used for Finalize_Lock
57 -- Enter_Task
58 -- Write_Lock
59 -- Unlock
60 -- Sleep
61 -- Wakeup
62 -- Get_Priority
63 -- Lock/Unlock_RTS
64 -- New_ATCB
66 with System.Soft_Links;
67 -- These are procedure pointers to non-tasking routines that use
68 -- task specific data. In the absence of tasking, these routines
69 -- refer to global data. In the presense of tasking, they must be
70 -- replaced with pointers to task-specific versions.
71 -- Also used for Create_TSD, Destroy_TSD, Get_Current_Excep
73 with System.Tasking.Initialization;
74 -- Used for Remove_From_All_Tasks_List
75 -- Defer_Abort
76 -- Undefer_Abort
77 -- Initialization.Poll_Base_Priority_Change
78 -- Finalize_Attributes_Link
79 -- Initialize_Attributes_Link
81 pragma Elaborate_All (System.Tasking.Initialization);
82 -- This insures that tasking is initialized if any tasks are created.
84 with System.Tasking.Utilities;
85 -- Used for Make_Passive
86 -- Abort_One_Task
88 with System.Tasking.Queuing;
89 -- Used for Dequeue_Head
91 with System.Tasking.Rendezvous;
92 -- Used for Call_Simple
94 with System.OS_Primitives;
95 -- Used for Delay_Modes
97 with System.Finalization_Implementation;
98 -- Used for System.Finalization_Implementation.Finalize_Global_List
100 with System.Secondary_Stack;
101 -- used for SS_Init
103 with System.Storage_Elements;
104 -- used for Storage_Array
106 with System.Standard_Library;
107 -- used for Exception_Trace
109 with System.Traces.Tasking;
110 -- used for Send_Trace_Info
112 with Unchecked_Deallocation;
113 -- To recover from failure of ATCB initialization.
115 package body System.Tasking.Stages is
117 package STPO renames System.Task_Primitives.Operations;
118 package SSL renames System.Soft_Links;
119 package SSE renames System.Storage_Elements;
120 package SST renames System.Secondary_Stack;
122 use Ada.Exceptions;
124 use Parameters;
125 use Task_Primitives;
126 use Task_Primitives.Operations;
127 use Task_Info;
129 use System.Traces;
130 use System.Traces.Tasking;
132 -----------------------
133 -- Local Subprograms --
134 -----------------------
136 procedure Free is new
137 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
139 procedure Trace_Unhandled_Exception_In_Task (Self_Id : Task_Id);
140 -- This procedure outputs the task specific message for exception
141 -- tracing purposes.
143 procedure Task_Wrapper (Self_ID : Task_Id);
144 pragma Convention (C, Task_Wrapper);
145 -- This is the procedure that is called by the GNULL from the new context
146 -- when a task is created. It waits for activation and then calls the task
147 -- body procedure. When the task body procedure completes, it terminates
148 -- the task.
150 -- The Task_Wrapper's address will be provided to the underlying threads
151 -- library as the task entry point. Convention C is what makes most sense
152 -- for that purpose (Export C would make the function globally visible,
153 -- and affect the link name on which GDB depends). This will in addition
154 -- trigger an automatic stack alignment suitable for GCC's assumptions if
155 -- need be.
157 procedure Vulnerable_Complete_Task (Self_ID : Task_Id);
158 -- Complete the calling task. This procedure must be called with
159 -- abort deferred. It should only be called by Complete_Task and
160 -- Finalizate_Global_Tasks (for the environment task).
162 procedure Vulnerable_Complete_Master (Self_ID : Task_Id);
163 -- Complete the current master of the calling task. This procedure
164 -- must be called with abort deferred. It should only be called by
165 -- Vulnerable_Complete_Task and Complete_Master.
167 procedure Vulnerable_Complete_Activation (Self_ID : Task_Id);
168 -- Signal to Self_ID's activator that Self_ID has completed activation.
169 -- This procedure must be called with abort deferred.
171 procedure Abort_Dependents (Self_ID : Task_Id);
172 -- Abort all the direct dependents of Self at its current master
173 -- nesting level, plus all of their dependents, transitively.
174 -- RTS_Lock should be locked by the caller.
176 procedure Vulnerable_Free_Task (T : Task_Id);
177 -- Recover all runtime system storage associated with the task T.
178 -- This should only be called after T has terminated and will no
179 -- longer be referenced.
181 -- For tasks created by an allocator that fails, due to an exception,
182 -- it is called from Expunge_Unactivated_Tasks.
184 -- It is also called from Unchecked_Deallocation, for objects that
185 -- are or contain tasks.
187 -- Different code is used at master completion, in Terminate_Dependents,
188 -- due to a need for tighter synchronization with the master.
190 ----------------------
191 -- Abort_Dependents --
192 ----------------------
194 procedure Abort_Dependents (Self_ID : Task_Id) is
195 C : Task_Id;
196 P : Task_Id;
198 begin
199 C := All_Tasks_List;
200 while C /= null loop
201 P := C.Common.Parent;
202 while P /= null loop
203 if P = Self_ID then
205 -- ??? C is supposed to take care of its own dependents, so
206 -- there should be no need to worry about them. Need to double
207 -- check this.
209 if C.Master_of_Task = Self_ID.Master_Within then
210 Utilities.Abort_One_Task (Self_ID, C);
211 C.Dependents_Aborted := True;
212 end if;
214 exit;
215 end if;
217 P := P.Common.Parent;
218 end loop;
220 C := C.Common.All_Tasks_Link;
221 end loop;
223 Self_ID.Dependents_Aborted := True;
224 end Abort_Dependents;
226 -----------------
227 -- Abort_Tasks --
228 -----------------
230 procedure Abort_Tasks (Tasks : Task_List) is
231 begin
232 -- If pragma Detect_Blocking is active then Program_Error must be
233 -- raised if this potentially blocking operation is called from a
234 -- protected action.
236 if System.Tasking.Detect_Blocking
237 and then STPO.Self.Common.Protected_Action_Nesting > 0
238 then
239 Ada.Exceptions.Raise_Exception
240 (Program_Error'Identity, "potentially blocking operation");
241 end if;
243 Utilities.Abort_Tasks (Tasks);
244 end Abort_Tasks;
246 --------------------
247 -- Activate_Tasks --
248 --------------------
250 -- Note that locks of activator and activated task are both locked
251 -- here. This is necessary because C.Common.State and
252 -- Self.Common.Wait_Count have to be synchronized. This is safe from
253 -- deadlock because the activator is always created before the activated
254 -- task. That satisfies our in-order-of-creation ATCB locking policy.
256 -- At one point, we may also lock the parent, if the parent is
257 -- different from the activator. That is also consistent with the
258 -- lock ordering policy, since the activator cannot be created
259 -- before the parent.
261 -- Since we are holding both the activator's lock, and Task_Wrapper
262 -- locks that before it does anything more than initialize the
263 -- low-level ATCB components, it should be safe to wait to update
264 -- the counts until we see that the thread creation is successful.
266 -- If the thread creation fails, we do need to close the entries
267 -- of the task. The first phase, of dequeuing calls, only requires
268 -- locking the acceptor's ATCB, but the waking up of the callers
269 -- requires locking the caller's ATCB. We cannot safely do this
270 -- while we are holding other locks. Therefore, the queue-clearing
271 -- operation is done in a separate pass over the activation chain.
273 procedure Activate_Tasks (Chain_Access : Activation_Chain_Access) is
274 Self_ID : constant Task_Id := STPO.Self;
275 P : Task_Id;
276 C : Task_Id;
277 Next_C, Last_C : Task_Id;
278 Activate_Prio : System.Any_Priority;
279 Success : Boolean;
280 All_Elaborated : Boolean := True;
282 begin
283 -- If pragma Detect_Blocking is active, then we must check whether this
284 -- potentially blocking operation is called from a protected action.
286 if System.Tasking.Detect_Blocking
287 and then Self_ID.Common.Protected_Action_Nesting > 0
288 then
289 Ada.Exceptions.Raise_Exception
290 (Program_Error'Identity, "potentially blocking operation");
291 end if;
293 pragma Debug
294 (Debug.Trace (Self_ID, "Activate_Tasks", 'C'));
296 Initialization.Defer_Abort_Nestable (Self_ID);
298 pragma Assert (Self_ID.Common.Wait_Count = 0);
300 -- Lock RTS_Lock, to prevent activated tasks from racing ahead before
301 -- we finish activating the chain.
303 Lock_RTS;
305 -- Check that all task bodies have been elaborated
307 C := Chain_Access.T_ID;
308 Last_C := null;
309 while C /= null loop
310 if C.Common.Elaborated /= null
311 and then not C.Common.Elaborated.all
312 then
313 All_Elaborated := False;
314 end if;
316 -- Reverse the activation chain so that tasks are
317 -- activated in the same order they're declared.
319 Next_C := C.Common.Activation_Link;
320 C.Common.Activation_Link := Last_C;
321 Last_C := C;
322 C := Next_C;
323 end loop;
325 Chain_Access.T_ID := Last_C;
327 if not All_Elaborated then
328 Unlock_RTS;
329 Initialization.Undefer_Abort_Nestable (Self_ID);
330 Raise_Exception
331 (Program_Error'Identity, "Some tasks have not been elaborated");
332 end if;
334 -- Activate all the tasks in the chain. Creation of the thread of
335 -- control was deferred until activation. So create it now.
337 C := Chain_Access.T_ID;
338 while C /= null loop
339 if C.Common.State /= Terminated then
340 pragma Assert (C.Common.State = Unactivated);
342 P := C.Common.Parent;
343 Write_Lock (P);
344 Write_Lock (C);
346 if C.Common.Base_Priority < Get_Priority (Self_ID) then
347 Activate_Prio := Get_Priority (Self_ID);
348 else
349 Activate_Prio := C.Common.Base_Priority;
350 end if;
352 System.Task_Primitives.Operations.Create_Task
353 (C, Task_Wrapper'Address,
354 Parameters.Size_Type
355 (C.Common.Compiler_Data.Pri_Stack_Info.Size),
356 Activate_Prio, Success);
358 -- There would be a race between the created task and the
359 -- creator to do the following initialization, if we did not
360 -- have a Lock/Unlock_RTS pair in the task wrapper to prevent
361 -- it from racing ahead.
363 if Success then
364 C.Common.State := Runnable;
365 C.Awake_Count := 1;
366 C.Alive_Count := 1;
367 P.Awake_Count := P.Awake_Count + 1;
368 P.Alive_Count := P.Alive_Count + 1;
370 if P.Common.State = Master_Completion_Sleep and then
371 C.Master_of_Task = P.Master_Within
372 then
373 pragma Assert (Self_ID /= P);
374 P.Common.Wait_Count := P.Common.Wait_Count + 1;
375 end if;
377 Unlock (C);
378 Unlock (P);
380 else
381 -- No need to set Awake_Count, State, etc. here since the loop
382 -- below will do that for any Unactivated tasks.
384 Unlock (C);
385 Unlock (P);
386 Self_ID.Common.Activation_Failed := True;
387 end if;
388 end if;
390 C := C.Common.Activation_Link;
391 end loop;
393 if not Single_Lock then
394 Unlock_RTS;
395 end if;
397 -- Close the entries of any tasks that failed thread creation,
398 -- and count those that have not finished activation.
400 Write_Lock (Self_ID);
401 Self_ID.Common.State := Activator_Sleep;
403 C := Chain_Access.T_ID;
404 while C /= null loop
405 Write_Lock (C);
407 if C.Common.State = Unactivated then
408 C.Common.Activator := null;
409 C.Common.State := Terminated;
410 C.Callable := False;
411 Utilities.Cancel_Queued_Entry_Calls (C);
413 elsif C.Common.Activator /= null then
414 Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1;
415 end if;
417 Unlock (C);
418 P := C.Common.Activation_Link;
419 C.Common.Activation_Link := null;
420 C := P;
421 end loop;
423 -- Wait for the activated tasks to complete activation. It is
424 -- unsafe to abort any of these tasks until the count goes to zero.
426 loop
427 Initialization.Poll_Base_Priority_Change (Self_ID);
428 exit when Self_ID.Common.Wait_Count = 0;
429 Sleep (Self_ID, Activator_Sleep);
430 end loop;
432 Self_ID.Common.State := Runnable;
433 Unlock (Self_ID);
435 if Single_Lock then
436 Unlock_RTS;
437 end if;
439 -- Remove the tasks from the chain
441 Chain_Access.T_ID := null;
442 Initialization.Undefer_Abort_Nestable (Self_ID);
444 if Self_ID.Common.Activation_Failed then
445 Self_ID.Common.Activation_Failed := False;
446 Raise_Exception (Tasking_Error'Identity,
447 "Failure during activation");
448 end if;
449 end Activate_Tasks;
451 -------------------------
452 -- Complete_Activation --
453 -------------------------
455 procedure Complete_Activation is
456 Self_ID : constant Task_Id := STPO.Self;
458 begin
459 Initialization.Defer_Abort_Nestable (Self_ID);
461 if Single_Lock then
462 Lock_RTS;
463 end if;
465 Vulnerable_Complete_Activation (Self_ID);
467 if Single_Lock then
468 Unlock_RTS;
469 end if;
471 Initialization.Undefer_Abort_Nestable (Self_ID);
473 -- ???
474 -- Why do we need to allow for nested deferral here?
476 if Runtime_Traces then
477 Send_Trace_Info (T_Activate);
478 end if;
479 end Complete_Activation;
481 ---------------------
482 -- Complete_Master --
483 ---------------------
485 procedure Complete_Master is
486 Self_ID : constant Task_Id := STPO.Self;
487 begin
488 pragma Assert (Self_ID.Deferral_Level > 0);
489 Vulnerable_Complete_Master (Self_ID);
490 end Complete_Master;
492 -------------------
493 -- Complete_Task --
494 -------------------
496 -- See comments on Vulnerable_Complete_Task for details
498 procedure Complete_Task is
499 Self_ID : constant Task_Id := STPO.Self;
501 begin
502 pragma Assert (Self_ID.Deferral_Level > 0);
504 Vulnerable_Complete_Task (Self_ID);
506 -- All of our dependents have terminated. Never undefer abort again!
508 end Complete_Task;
510 -----------------
511 -- Create_Task --
512 -----------------
514 -- Compiler interface only. Do not call from within the RTS.
515 -- This must be called to create a new task.
517 procedure Create_Task
518 (Priority : Integer;
519 Size : System.Parameters.Size_Type;
520 Task_Info : System.Task_Info.Task_Info_Type;
521 Num_Entries : Task_Entry_Index;
522 Master : Master_Level;
523 State : Task_Procedure_Access;
524 Discriminants : System.Address;
525 Elaborated : Access_Boolean;
526 Chain : in out Activation_Chain;
527 Task_Image : String;
528 Created_Task : out Task_Id)
530 T, P : Task_Id;
531 Self_ID : constant Task_Id := STPO.Self;
532 Success : Boolean;
533 Base_Priority : System.Any_Priority;
534 Len : Natural;
536 begin
537 -- If pragma Detect_Blocking is active must be checked whether
538 -- this potentially blocking operation is called from a
539 -- protected action.
541 if System.Tasking.Detect_Blocking
542 and then Self_ID.Common.Protected_Action_Nesting > 0
543 then
544 Ada.Exceptions.Raise_Exception
545 (Program_Error'Identity, "potentially blocking operation");
546 end if;
548 pragma Debug
549 (Debug.Trace (Self_ID, "Create_Task", 'C'));
551 if Priority = Unspecified_Priority then
552 Base_Priority := Self_ID.Common.Base_Priority;
553 else
554 Base_Priority := System.Any_Priority (Priority);
555 end if;
557 -- Find parent P of new Task, via master level number
559 P := Self_ID;
561 if P /= null then
562 while P.Master_of_Task >= Master loop
563 P := P.Common.Parent;
564 exit when P = null;
565 end loop;
566 end if;
568 Initialization.Defer_Abort_Nestable (Self_ID);
570 begin
571 T := New_ATCB (Num_Entries);
572 exception
573 when others =>
574 Initialization.Undefer_Abort_Nestable (Self_ID);
575 Raise_Exception (Storage_Error'Identity, "Cannot allocate task");
576 end;
578 -- RTS_Lock is used by Abort_Dependents and Abort_Tasks.
579 -- Up to this point, it is possible that we may be part of
580 -- a family of tasks that is being aborted.
582 Lock_RTS;
583 Write_Lock (Self_ID);
585 -- Now, we must check that we have not been aborted.
586 -- If so, we should give up on creating this task,
587 -- and simply return.
589 if not Self_ID.Callable then
590 pragma Assert (Self_ID.Pending_ATC_Level = 0);
591 pragma Assert (Self_ID.Pending_Action);
592 pragma Assert
593 (Chain.T_ID = null or else Chain.T_ID.Common.State = Unactivated);
595 Unlock (Self_ID);
596 Unlock_RTS;
597 Initialization.Undefer_Abort_Nestable (Self_ID);
599 -- ??? Should never get here
601 pragma Assert (False);
602 raise Standard'Abort_Signal;
603 end if;
605 Initialize_ATCB (Self_ID, State, Discriminants, P, Elaborated,
606 Base_Priority, Task_Info, Size, T, Success);
608 if not Success then
609 Free (T);
610 Unlock (Self_ID);
611 Unlock_RTS;
612 Initialization.Undefer_Abort_Nestable (Self_ID);
613 Raise_Exception
614 (Storage_Error'Identity, "Failed to initialize task");
615 end if;
617 T.Master_of_Task := Master;
618 T.Master_Within := T.Master_of_Task + 1;
620 for L in T.Entry_Calls'Range loop
621 T.Entry_Calls (L).Self := T;
622 T.Entry_Calls (L).Level := L;
623 end loop;
625 if Task_Image'Length = 0 then
626 T.Common.Task_Image_Len := 0;
627 else
628 Len := 1;
629 T.Common.Task_Image (1) := Task_Image (Task_Image'First);
631 -- Remove unwanted blank space generated by 'Image
633 for J in Task_Image'First + 1 .. Task_Image'Last loop
634 if Task_Image (J) /= ' '
635 or else Task_Image (J - 1) /= '('
636 then
637 Len := Len + 1;
638 T.Common.Task_Image (Len) := Task_Image (J);
639 exit when Len = T.Common.Task_Image'Last;
640 end if;
641 end loop;
643 T.Common.Task_Image_Len := Len;
644 end if;
646 Unlock (Self_ID);
647 Unlock_RTS;
649 -- Create TSD as early as possible in the creation of a task, since it
650 -- may be used by the operation of Ada code within the task.
652 SSL.Create_TSD (T.Common.Compiler_Data);
653 T.Common.Activation_Link := Chain.T_ID;
654 Chain.T_ID := T;
655 Initialization.Initialize_Attributes_Link.all (T);
656 Created_Task := T;
657 Initialization.Undefer_Abort_Nestable (Self_ID);
659 if Runtime_Traces then
660 Send_Trace_Info (T_Create, T);
661 end if;
662 end Create_Task;
664 --------------------
665 -- Current_Master --
666 --------------------
668 function Current_Master return Master_Level is
669 begin
670 return STPO.Self.Master_Within;
671 end Current_Master;
673 ------------------
674 -- Enter_Master --
675 ------------------
677 procedure Enter_Master is
678 Self_ID : constant Task_Id := STPO.Self;
679 begin
680 Self_ID.Master_Within := Self_ID.Master_Within + 1;
681 end Enter_Master;
683 -------------------------------
684 -- Expunge_Unactivated_Tasks --
685 -------------------------------
687 -- See procedure Close_Entries for the general case
689 procedure Expunge_Unactivated_Tasks (Chain : in out Activation_Chain) is
690 Self_ID : constant Task_Id := STPO.Self;
691 C : Task_Id;
692 Call : Entry_Call_Link;
693 Temp : Task_Id;
695 begin
696 pragma Debug
697 (Debug.Trace (Self_ID, "Expunge_Unactivated_Tasks", 'C'));
699 Initialization.Defer_Abort_Nestable (Self_ID);
701 -- ???
702 -- Experimentation has shown that abort is sometimes (but not
703 -- always) already deferred when this is called.
705 -- That may indicate an error. Find out what is going on
707 C := Chain.T_ID;
708 while C /= null loop
709 pragma Assert (C.Common.State = Unactivated);
711 Temp := C.Common.Activation_Link;
713 if C.Common.State = Unactivated then
714 Lock_RTS;
715 Write_Lock (C);
717 for J in 1 .. C.Entry_Num loop
718 Queuing.Dequeue_Head (C.Entry_Queues (J), Call);
719 pragma Assert (Call = null);
720 end loop;
722 Unlock (C);
724 Initialization.Remove_From_All_Tasks_List (C);
725 Unlock_RTS;
727 Vulnerable_Free_Task (C);
728 C := Temp;
729 end if;
730 end loop;
732 Chain.T_ID := null;
733 Initialization.Undefer_Abort_Nestable (Self_ID);
734 end Expunge_Unactivated_Tasks;
736 ---------------------------
737 -- Finalize_Global_Tasks --
738 ---------------------------
740 -- ???
741 -- We have a potential problem here if finalization of global
742 -- objects does anything with signals or the timer server, since
743 -- by that time those servers have terminated.
745 -- It is hard to see how that would occur
747 -- However, a better solution might be to do all this finalization
748 -- using the global finalization chain.
750 procedure Finalize_Global_Tasks is
751 Self_ID : constant Task_Id := STPO.Self;
752 Ignore : Boolean;
754 begin
755 if Self_ID.Deferral_Level = 0 then
756 -- ???
757 -- In principle, we should be able to predict whether
758 -- abort is already deferred here (and it should not be deferred
759 -- yet but in practice it seems Finalize_Global_Tasks is being
760 -- called sometimes, from RTS code for exceptions, with abort already
761 -- deferred.
763 Initialization.Defer_Abort_Nestable (Self_ID);
765 -- Never undefer again!!!
766 end if;
768 -- This code is only executed by the environment task
770 pragma Assert (Self_ID = Environment_Task);
772 -- Set Environment_Task'Callable to false to notify library-level tasks
773 -- that it is waiting for them (cf 5619-003).
775 Self_ID.Callable := False;
777 -- Exit level 2 master, for normal tasks in library-level packages.
779 Complete_Master;
781 -- Force termination of "independent" library-level server tasks.
783 Lock_RTS;
785 Abort_Dependents (Self_ID);
787 if not Single_Lock then
788 Unlock_RTS;
789 end if;
791 -- We need to explicitely wait for the task to be terminated here
792 -- because on true concurrent system, we may end this procedure
793 -- before the tasks are really terminated.
795 Write_Lock (Self_ID);
797 loop
798 exit when Utilities.Independent_Task_Count = 0;
800 -- We used to yield here, but this did not take into account
801 -- low priority tasks that would cause dead lock in some cases.
802 -- See 8126-020.
804 Timed_Sleep
805 (Self_ID, 0.01, System.OS_Primitives.Relative,
806 Self_ID.Common.State, Ignore, Ignore);
807 end loop;
809 -- ??? On multi-processor environments, it seems that the above loop
810 -- isn't sufficient, so we need to add an additional delay.
812 Timed_Sleep
813 (Self_ID, 0.01, System.OS_Primitives.Relative,
814 Self_ID.Common.State, Ignore, Ignore);
816 Unlock (Self_ID);
818 if Single_Lock then
819 Unlock_RTS;
820 end if;
822 -- Complete the environment task
824 Vulnerable_Complete_Task (Self_ID);
826 System.Finalization_Implementation.Finalize_Global_List;
828 SSL.Abort_Defer := SSL.Abort_Defer_NT'Access;
829 SSL.Abort_Undefer := SSL.Abort_Undefer_NT'Access;
830 SSL.Lock_Task := SSL.Task_Lock_NT'Access;
831 SSL.Unlock_Task := SSL.Task_Unlock_NT'Access;
832 SSL.Get_Jmpbuf_Address := SSL.Get_Jmpbuf_Address_NT'Access;
833 SSL.Set_Jmpbuf_Address := SSL.Set_Jmpbuf_Address_NT'Access;
834 SSL.Get_Sec_Stack_Addr := SSL.Get_Sec_Stack_Addr_NT'Access;
835 SSL.Set_Sec_Stack_Addr := SSL.Set_Sec_Stack_Addr_NT'Access;
836 SSL.Get_Exc_Stack_Addr := SSL.Get_Exc_Stack_Addr_NT'Access;
837 SSL.Set_Exc_Stack_Addr := SSL.Set_Exc_Stack_Addr_NT'Access;
838 SSL.Check_Abort_Status := SSL.Check_Abort_Status_NT'Access;
839 SSL.Get_Stack_Info := SSL.Get_Stack_Info_NT'Access;
841 -- Don't bother trying to finalize Initialization.Global_Task_Lock
842 -- and System.Task_Primitives.RTS_Lock.
844 end Finalize_Global_Tasks;
846 ---------------
847 -- Free_Task --
848 ---------------
850 procedure Free_Task (T : Task_Id) is
851 Self_Id : constant Task_Id := Self;
853 begin
854 if T.Common.State = Terminated then
856 -- It is not safe to call Abort_Defer or Write_Lock at this stage
858 Initialization.Task_Lock (Self_Id);
860 Lock_RTS;
861 Initialization.Remove_From_All_Tasks_List (T);
862 Unlock_RTS;
864 Initialization.Task_Unlock (Self_Id);
866 System.Task_Primitives.Operations.Finalize_TCB (T);
868 -- If the task is not terminated, then we simply ignore the call. This
869 -- happens when a user program attempts an unchecked deallocation on
870 -- a non-terminated task.
872 else
873 null;
874 end if;
875 end Free_Task;
877 ------------------
878 -- Task_Wrapper --
879 ------------------
881 -- The task wrapper is a procedure that is called first for each task
882 -- task body, and which in turn calls the compiler-generated task body
883 -- procedure. The wrapper's main job is to do initialization for the task.
884 -- It also has some locally declared objects that server as per-task local
885 -- data. Task finalization is done by Complete_Task, which is called from
886 -- an at-end handler that the compiler generates.
888 procedure Task_Wrapper (Self_ID : Task_Id) is
889 use type System.Parameters.Size_Type;
890 use type SSE.Storage_Offset;
891 use System.Standard_Library;
893 Secondary_Stack :
894 aliased SSE.Storage_Array
895 (1 .. Self_ID.Common.Compiler_Data.Pri_Stack_Info.Size *
896 SSE.Storage_Offset (Parameters.Sec_Stack_Ratio) / 100);
898 Secondary_Stack_Address : System.Address := Secondary_Stack'Address;
900 begin
901 pragma Assert (Self_ID.Deferral_Level = 1);
903 if not Parameters.Sec_Stack_Dynamic then
904 Self_ID.Common.Compiler_Data.Sec_Stack_Addr :=
905 Secondary_Stack'Address;
906 SST.SS_Init (Secondary_Stack_Address, Integer (Secondary_Stack'Last));
907 end if;
909 -- Set the guard page at the bottom of the stack. The call to
910 -- unprotect the page is done in Terminate_Task
912 Stack_Guard (Self_ID, True);
914 -- Initialize low-level TCB components, that cannot be initialized
915 -- by the creator. Enter_Task sets Self_ID.Known_Tasks_Index and
916 -- also Self_ID.LL.Thread
918 Enter_Task (Self_ID);
920 -- We lock RTS_Lock to wait for activator to finish activating
921 -- the rest of the chain, so that everyone in the chain comes out
922 -- in priority order.
924 -- This also protects the value of
925 -- Self_ID.Common.Activator.Common.Wait_Count.
927 Lock_RTS;
928 Unlock_RTS;
930 begin
931 -- We are separating the following portion of the code in order to
932 -- place the exception handlers in a different block. In this way,
933 -- we do not call Set_Jmpbuf_Address (which needs Self) before we
934 -- set Self in Enter_Task
936 -- Call the task body procedure
938 -- The task body is called with abort still deferred. That
939 -- eliminates a dangerous window, for which we had to patch-up in
940 -- Terminate_Task.
942 -- During the expansion of the task body, we insert an RTS-call
943 -- to Abort_Undefer, at the first point where abort should be
944 -- allowed.
946 Self_ID.Common.Task_Entry_Point (Self_ID.Common.Task_Arg);
947 Initialization.Defer_Abort_Nestable (Self_ID);
949 exception
950 -- We can't call Terminate_Task in the exception handlers below,
951 -- since there may be (e.g. in the case of GCC exception handling)
952 -- clean ups associated with the exception handler that need to
953 -- access task specific data.
955 -- Defer abortion so that this task can't be aborted while exiting
957 when Standard'Abort_Signal =>
958 Initialization.Defer_Abort_Nestable (Self_ID);
960 when others =>
961 -- ??? Using an E : others here causes CD2C11A to fail on
962 -- DEC Unix, see 7925-005.
964 Initialization.Defer_Abort_Nestable (Self_ID);
966 -- Perform the task specific exception tracing duty. We handle
967 -- these outputs here and not in the common notification routine
968 -- because we need access to tasking related data and we don't
969 -- want to drag dependencies against tasking related units in the
970 -- the common notification units. Additionally, no trace is ever
971 -- triggered from the common routine for the Unhandled_Raise case
972 -- in tasks, since an exception never appears unhandled in this
973 -- context because of this handler.
975 if Exception_Trace = Unhandled_Raise then
976 Trace_Unhandled_Exception_In_Task (Self_ID);
977 end if;
978 end;
980 Terminate_Task (Self_ID);
981 end Task_Wrapper;
983 --------------------
984 -- Terminate_Task --
985 --------------------
987 -- Before we allow the thread to exit, we must clean up. This is a
988 -- a delicate job. We must wake up the task's master, who may immediately
989 -- try to deallocate the ATCB out from under the current task WHILE IT IS
990 -- STILL EXECUTING.
992 -- To avoid this, the parent task must be blocked up to the latest
993 -- statement executed. The trouble is that we have another step that we
994 -- also want to postpone to the very end, i.e., calling SSL.Destroy_TSD.
995 -- We have to postpone that until the end because compiler-generated code
996 -- is likely to try to access that data at just about any point.
998 -- We can't call Destroy_TSD while we are holding any other locks, because
999 -- it locks Global_Task_Lock, and our deadlock prevention rules require
1000 -- that to be the outermost lock. Our first "solution" was to just lock
1001 -- Global_Task_Lock in addition to the other locks, and force the parent
1002 -- to also lock this lock between its wakeup and its freeing of the ATCB.
1003 -- See Complete_Task for the parent-side of the code that has the matching
1004 -- calls to Task_Lock and Task_Unlock. That was not really a solution,
1005 -- since the operation Task_Unlock continued to access the ATCB after
1006 -- unlocking, after which the parent was observed to race ahead,
1007 -- deallocate the ATCB, and then reallocate it to another task. The
1008 -- call to Undefer_Abortion in Task_Unlock by the "terminated" task was
1009 -- overwriting the data of the new task that reused the ATCB! To solve
1010 -- this problem, we introduced the new operation Final_Task_Unlock.
1012 procedure Terminate_Task (Self_ID : Task_Id) is
1013 Environment_Task : constant Task_Id := STPO.Environment_Task;
1014 Master_of_Task : Integer;
1016 begin
1017 Debug.Task_Termination_Hook;
1019 if Runtime_Traces then
1020 Send_Trace_Info (T_Terminate);
1021 end if;
1023 -- Since GCC cannot allocate stack chunks efficiently without reordering
1024 -- some of the allocations, we have to handle this unexpected situation
1025 -- here. We should normally never have to call Vulnerable_Complete_Task
1026 -- here. See 6602-003 for more details.
1028 if Self_ID.Common.Activator /= null then
1029 Vulnerable_Complete_Task (Self_ID);
1030 end if;
1032 Initialization.Task_Lock (Self_ID);
1034 if Single_Lock then
1035 Lock_RTS;
1036 end if;
1038 Master_of_Task := Self_ID.Master_of_Task;
1040 -- Check if the current task is an independent task If so, decrement
1041 -- the Independent_Task_Count value.
1043 if Master_of_Task = 2 then
1044 if Single_Lock then
1045 Utilities.Independent_Task_Count :=
1046 Utilities.Independent_Task_Count - 1;
1047 else
1048 Write_Lock (Environment_Task);
1049 Utilities.Independent_Task_Count :=
1050 Utilities.Independent_Task_Count - 1;
1051 Unlock (Environment_Task);
1052 end if;
1053 end if;
1055 -- Unprotect the guard page if needed
1057 Stack_Guard (Self_ID, False);
1059 Utilities.Make_Passive (Self_ID, Task_Completed => True);
1061 if Single_Lock then
1062 Unlock_RTS;
1063 end if;
1065 pragma Assert (Check_Exit (Self_ID));
1067 SSL.Destroy_TSD (Self_ID.Common.Compiler_Data);
1068 Initialization.Final_Task_Unlock (Self_ID);
1070 -- WARNING: past this point, this thread must assume that the ATCB
1071 -- has been deallocated. It should not be accessed again.
1073 if Master_of_Task > 0 then
1074 STPO.Exit_Task;
1075 end if;
1076 end Terminate_Task;
1078 ----------------
1079 -- Terminated --
1080 ----------------
1082 function Terminated (T : Task_Id) return Boolean is
1083 Self_ID : constant Task_Id := STPO.Self;
1084 Result : Boolean;
1086 begin
1087 Initialization.Defer_Abort_Nestable (Self_ID);
1089 if Single_Lock then
1090 Lock_RTS;
1091 end if;
1093 Write_Lock (T);
1094 Result := T.Common.State = Terminated;
1095 Unlock (T);
1097 if Single_Lock then
1098 Unlock_RTS;
1099 end if;
1101 Initialization.Undefer_Abort_Nestable (Self_ID);
1102 return Result;
1103 end Terminated;
1105 ----------------------------------------
1106 -- Trace_Unhandled_Exception_In_Task --
1107 ----------------------------------------
1109 procedure Trace_Unhandled_Exception_In_Task (Self_Id : Task_Id) is
1110 procedure To_Stderr (S : String);
1111 pragma Import (Ada, To_Stderr, "__gnat_to_stderr");
1113 use System.Task_Info;
1114 use System.Soft_Links;
1115 use System.Standard_Library;
1117 function To_Address is new
1118 Unchecked_Conversion (Task_Id, System.Address);
1120 function Tailored_Exception_Information
1121 (E : Exception_Occurrence) return String;
1122 pragma Import
1123 (Ada, Tailored_Exception_Information,
1124 "__gnat_tailored_exception_information");
1126 Excep : constant Exception_Occurrence_Access :=
1127 SSL.Get_Current_Excep.all;
1129 begin
1130 -- This procedure is called by the task outermost handler in
1131 -- Task_Wrapper below, so only once the task stack has been fully
1132 -- unwound. The common notification routine has been called at the
1133 -- raise point already.
1135 To_Stderr ("task ");
1137 if Self_Id.Common.Task_Image_Len /= 0 then
1138 To_Stderr
1139 (Self_Id.Common.Task_Image (1 .. Self_Id.Common.Task_Image_Len));
1140 To_Stderr ("_");
1141 end if;
1143 To_Stderr (System.Address_Image (To_Address (Self_Id)));
1144 To_Stderr (" terminated by unhandled exception");
1145 To_Stderr ((1 => ASCII.LF));
1146 To_Stderr (Tailored_Exception_Information (Excep.all));
1147 end Trace_Unhandled_Exception_In_Task;
1149 ------------------------------------
1150 -- Vulnerable_Complete_Activation --
1151 ------------------------------------
1153 -- As in several other places, the locks of the activator and activated
1154 -- task are both locked here. This follows our deadlock prevention lock
1155 -- ordering policy, since the activated task must be created after the
1156 -- activator.
1158 procedure Vulnerable_Complete_Activation (Self_ID : Task_Id) is
1159 Activator : constant Task_Id := Self_ID.Common.Activator;
1161 begin
1162 pragma Debug (Debug.Trace (Self_ID, "V_Complete_Activation", 'C'));
1164 Write_Lock (Activator);
1165 Write_Lock (Self_ID);
1167 pragma Assert (Self_ID.Common.Activator /= null);
1169 -- Remove dangling reference to Activator, since a task may
1170 -- outlive its activator.
1172 Self_ID.Common.Activator := null;
1174 -- Wake up the activator, if it is waiting for a chain of tasks to
1175 -- activate, and we are the last in the chain to complete activation.
1177 if Activator.Common.State = Activator_Sleep then
1178 Activator.Common.Wait_Count := Activator.Common.Wait_Count - 1;
1180 if Activator.Common.Wait_Count = 0 then
1181 Wakeup (Activator, Activator_Sleep);
1182 end if;
1183 end if;
1185 -- The activator raises a Tasking_Error if any task it is activating
1186 -- is completed before the activation is done. However, if the reason
1187 -- for the task completion is an abortion, we do not raise an exception.
1188 -- See RM 9.2(5).
1190 if not Self_ID.Callable and then Self_ID.Pending_ATC_Level /= 0 then
1191 Activator.Common.Activation_Failed := True;
1192 end if;
1194 Unlock (Self_ID);
1195 Unlock (Activator);
1197 -- After the activation, active priority should be the same
1198 -- as base priority. We must unlock the Activator first,
1199 -- though, since it should not wait if we have lower priority.
1201 if Get_Priority (Self_ID) /= Self_ID.Common.Base_Priority then
1202 Write_Lock (Self_ID);
1203 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
1204 Unlock (Self_ID);
1205 end if;
1206 end Vulnerable_Complete_Activation;
1208 --------------------------------
1209 -- Vulnerable_Complete_Master --
1210 --------------------------------
1212 procedure Vulnerable_Complete_Master (Self_ID : Task_Id) is
1213 C : Task_Id;
1214 P : Task_Id;
1215 CM : constant Master_Level := Self_ID.Master_Within;
1216 T : aliased Task_Id;
1218 To_Be_Freed : Task_Id;
1219 -- This is a list of ATCBs to be freed, after we have released
1220 -- all RTS locks. This is necessary because of the locking order
1221 -- rules, since the storage manager uses Global_Task_Lock.
1223 pragma Warnings (Off);
1224 function Check_Unactivated_Tasks return Boolean;
1225 pragma Warnings (On);
1226 -- Temporary error-checking code below. This is part of the checks
1227 -- added in the new run time. Call it only inside a pragma Assert.
1229 -----------------------------
1230 -- Check_Unactivated_Tasks --
1231 -----------------------------
1233 function Check_Unactivated_Tasks return Boolean is
1234 begin
1235 if not Single_Lock then
1236 Lock_RTS;
1237 end if;
1239 Write_Lock (Self_ID);
1241 C := All_Tasks_List;
1242 while C /= null loop
1243 if C.Common.Activator = Self_ID then
1244 return False;
1245 end if;
1247 if C.Common.Parent = Self_ID and then C.Master_of_Task = CM then
1248 Write_Lock (C);
1250 if C.Common.State = Unactivated then
1251 return False;
1252 end if;
1254 Unlock (C);
1255 end if;
1257 C := C.Common.All_Tasks_Link;
1258 end loop;
1260 Unlock (Self_ID);
1262 if not Single_Lock then
1263 Unlock_RTS;
1264 end if;
1266 return True;
1267 end Check_Unactivated_Tasks;
1269 -- Start of processing for Vulnerable_Complete_Master
1271 begin
1272 pragma Debug
1273 (Debug.Trace (Self_ID, "V_Complete_Master", 'C'));
1275 pragma Assert (Self_ID.Common.Wait_Count = 0);
1276 pragma Assert (Self_ID.Deferral_Level > 0);
1278 -- Count how many active dependent tasks this master currently
1279 -- has, and record this in Wait_Count.
1281 -- This count should start at zero, since it is initialized to
1282 -- zero for new tasks, and the task should not exit the
1283 -- sleep-loops that use this count until the count reaches zero.
1285 Lock_RTS;
1286 Write_Lock (Self_ID);
1288 C := All_Tasks_List;
1289 while C /= null loop
1290 if C.Common.Activator = Self_ID then
1291 pragma Assert (C.Common.State = Unactivated);
1293 Write_Lock (C);
1294 C.Common.Activator := null;
1295 C.Common.State := Terminated;
1296 C.Callable := False;
1297 Utilities.Cancel_Queued_Entry_Calls (C);
1298 Unlock (C);
1299 end if;
1301 if C.Common.Parent = Self_ID and then C.Master_of_Task = CM then
1302 Write_Lock (C);
1304 if C.Awake_Count /= 0 then
1305 Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1;
1306 end if;
1308 Unlock (C);
1309 end if;
1311 C := C.Common.All_Tasks_Link;
1312 end loop;
1314 Self_ID.Common.State := Master_Completion_Sleep;
1315 Unlock (Self_ID);
1317 if not Single_Lock then
1318 Unlock_RTS;
1319 end if;
1321 -- Wait until dependent tasks are all terminated or ready to terminate.
1322 -- While waiting, the task may be awakened if the task's priority needs
1323 -- changing, or this master is aborted. In the latter case, we want
1324 -- to abort the dependents, and resume waiting until Wait_Count goes
1325 -- to zero.
1327 Write_Lock (Self_ID);
1329 loop
1330 Initialization.Poll_Base_Priority_Change (Self_ID);
1331 exit when Self_ID.Common.Wait_Count = 0;
1333 -- Here is a difference as compared to Complete_Master
1335 if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
1336 and then not Self_ID.Dependents_Aborted
1337 then
1338 if Single_Lock then
1339 Abort_Dependents (Self_ID);
1340 else
1341 Unlock (Self_ID);
1342 Lock_RTS;
1343 Abort_Dependents (Self_ID);
1344 Unlock_RTS;
1345 Write_Lock (Self_ID);
1346 end if;
1347 else
1348 Sleep (Self_ID, Master_Completion_Sleep);
1349 end if;
1350 end loop;
1352 Self_ID.Common.State := Runnable;
1353 Unlock (Self_ID);
1355 -- Dependents are all terminated or on terminate alternatives.
1356 -- Now, force those on terminate alternatives to terminate, by
1357 -- aborting them.
1359 pragma Assert (Check_Unactivated_Tasks);
1361 if Self_ID.Alive_Count > 1 then
1362 -- ???
1363 -- Consider finding a way to skip the following extra steps if there
1364 -- are no dependents with terminate alternatives. This could be done
1365 -- by adding another count to the ATCB, similar to Awake_Count, but
1366 -- keeping track of tasks that are on terminate alternatives.
1368 pragma Assert (Self_ID.Common.Wait_Count = 0);
1370 -- Force any remaining dependents to terminate, by aborting them.
1372 if not Single_Lock then
1373 Lock_RTS;
1374 end if;
1376 Abort_Dependents (Self_ID);
1378 -- Above, when we "abort" the dependents we are simply using this
1379 -- operation for convenience. We are not required to support the full
1380 -- abort-statement semantics; in particular, we are not required to
1381 -- immediately cancel any queued or in-service entry calls. That is
1382 -- good, because if we tried to cancel a call we would need to lock
1383 -- the caller, in order to wake the caller up. Our anti-deadlock
1384 -- rules prevent us from doing that without releasing the locks on C
1385 -- and Self_ID. Releasing and retaking those locks would be wasteful
1386 -- at best, and should not be considered further without more
1387 -- detailed analysis of potential concurrent accesses to the
1388 -- ATCBs of C and Self_ID.
1390 -- Count how many "alive" dependent tasks this master currently
1391 -- has, and record this in Wait_Count. This count should start at
1392 -- zero, since it is initialized to zero for new tasks, and the
1393 -- task should not exit the sleep-loops that use this count until
1394 -- the count reaches zero.
1396 pragma Assert (Self_ID.Common.Wait_Count = 0);
1398 Write_Lock (Self_ID);
1400 C := All_Tasks_List;
1401 while C /= null loop
1402 if C.Common.Parent = Self_ID and then C.Master_of_Task = CM then
1403 Write_Lock (C);
1405 pragma Assert (C.Awake_Count = 0);
1407 if C.Alive_Count > 0 then
1408 pragma Assert (C.Terminate_Alternative);
1409 Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1;
1410 end if;
1412 Unlock (C);
1413 end if;
1415 C := C.Common.All_Tasks_Link;
1416 end loop;
1418 Self_ID.Common.State := Master_Phase_2_Sleep;
1419 Unlock (Self_ID);
1421 if not Single_Lock then
1422 Unlock_RTS;
1423 end if;
1425 -- Wait for all counted tasks to finish terminating themselves
1427 Write_Lock (Self_ID);
1429 loop
1430 Initialization.Poll_Base_Priority_Change (Self_ID);
1431 exit when Self_ID.Common.Wait_Count = 0;
1432 Sleep (Self_ID, Master_Phase_2_Sleep);
1433 end loop;
1435 Self_ID.Common.State := Runnable;
1436 Unlock (Self_ID);
1437 end if;
1439 -- We don't wake up for abortion here. We are already terminating
1440 -- just as fast as we can, so there is no point.
1442 -- Remove terminated tasks from the list of Self_ID's dependents, but
1443 -- don't free their ATCBs yet, because of lock order restrictions,
1444 -- which don't allow us to call "free" or "malloc" while holding any
1445 -- other locks. Instead, we put those ATCBs to be freed onto a
1446 -- temporary list, called To_Be_Freed.
1448 if not Single_Lock then
1449 Lock_RTS;
1450 end if;
1452 C := All_Tasks_List;
1453 P := null;
1454 while C /= null loop
1455 if C.Common.Parent = Self_ID and then C.Master_of_Task >= CM then
1456 if P /= null then
1457 P.Common.All_Tasks_Link := C.Common.All_Tasks_Link;
1458 else
1459 All_Tasks_List := C.Common.All_Tasks_Link;
1460 end if;
1462 T := C.Common.All_Tasks_Link;
1463 C.Common.All_Tasks_Link := To_Be_Freed;
1464 To_Be_Freed := C;
1465 C := T;
1467 else
1468 P := C;
1469 C := C.Common.All_Tasks_Link;
1470 end if;
1471 end loop;
1473 Unlock_RTS;
1475 -- Free all the ATCBs on the list To_Be_Freed
1477 -- The ATCBs in the list are no longer in All_Tasks_List, and after
1478 -- any interrupt entries are detached from them they should no longer
1479 -- be referenced.
1481 -- Global_Task_Lock (Task_Lock/Unlock) is locked in the loop below to
1482 -- avoid a race between a terminating task and its parent. The parent
1483 -- might try to deallocate the ACTB out from underneath the exiting
1484 -- task. Note that Free will also lock Global_Task_Lock, but that is
1485 -- OK, since this is the *one* lock for which we have a mechanism to
1486 -- support nested locking. See Task_Wrapper and its finalizer for more
1487 -- explanation.
1489 -- ???
1490 -- The check "T.Common.Parent /= null ..." below is to prevent dangling
1491 -- references to terminated library-level tasks, which could
1492 -- otherwise occur during finalization of library-level objects.
1493 -- A better solution might be to hook task objects into the
1494 -- finalization chain and deallocate the ATCB when the task
1495 -- object is deallocated. However, this change is not likely
1496 -- to gain anything significant, since all this storage should
1497 -- be recovered en-masse when the process exits.
1499 while To_Be_Freed /= null loop
1500 T := To_Be_Freed;
1501 To_Be_Freed := T.Common.All_Tasks_Link;
1503 -- ??? On SGI there is currently no Interrupt_Manager, that's
1504 -- why we need to check if the Interrupt_Manager_ID is null
1506 if T.Interrupt_Entry and Interrupt_Manager_ID /= null then
1507 declare
1508 Detach_Interrupt_Entries_Index : constant Task_Entry_Index := 1;
1509 -- Corresponds to the entry index of System.Interrupts.
1510 -- Interrupt_Manager.Detach_Interrupt_Entries.
1511 -- Be sure to update this value when changing
1512 -- Interrupt_Manager specs.
1514 type Param_Type is access all Task_Id;
1516 Param : aliased Param_Type := T'Access;
1518 begin
1519 System.Tasking.Rendezvous.Call_Simple
1520 (Interrupt_Manager_ID, Detach_Interrupt_Entries_Index,
1521 Param'Address);
1522 end;
1523 end if;
1525 if (T.Common.Parent /= null
1526 and then T.Common.Parent.Common.Parent /= null)
1527 or else T.Master_of_Task > 3
1528 then
1529 Initialization.Task_Lock (Self_ID);
1531 -- If Sec_Stack_Addr is not null, it means that Destroy_TSD
1532 -- has not been called yet (case of an unactivated task).
1534 if T.Common.Compiler_Data.Sec_Stack_Addr /= Null_Address then
1535 SSL.Destroy_TSD (T.Common.Compiler_Data);
1536 end if;
1538 Vulnerable_Free_Task (T);
1539 Initialization.Task_Unlock (Self_ID);
1540 end if;
1541 end loop;
1543 -- It might seem nice to let the terminated task deallocate its own
1544 -- ATCB. That would not cover the case of unactivated tasks. It also
1545 -- would force us to keep the underlying thread around past termination,
1546 -- since references to the ATCB are possible past termination.
1547 -- Currently, we get rid of the thread as soon as the task terminates,
1548 -- and let the parent recover the ATCB later.
1550 -- Some day, if we want to recover the ATCB earlier, at task
1551 -- termination, we could consider using "fat task IDs", that include the
1552 -- serial number with the ATCB pointer, to catch references to tasks
1553 -- that no longer have ATCBs. It is not clear how much this would gain,
1554 -- since the user-level task object would still be occupying storage.
1556 -- Make next master level up active.
1557 -- We don't need to lock the ATCB, since the value is only updated by
1558 -- each task for itself.
1560 Self_ID.Master_Within := CM - 1;
1561 end Vulnerable_Complete_Master;
1563 ------------------------------
1564 -- Vulnerable_Complete_Task --
1565 ------------------------------
1567 -- Complete the calling task
1569 -- This procedure must be called with abort deferred. (That's why the
1570 -- name has "Vulnerable" in it.) It should only be called by Complete_Task
1571 -- and Finalize_Global_Tasks (for the environment task).
1573 -- The effect is similar to that of Complete_Master. Differences include
1574 -- the closing of entries here, and computation of the number of active
1575 -- dependent tasks in Complete_Master.
1577 -- We don't lock Self_ID before the call to Vulnerable_Complete_Activation,
1578 -- because that does its own locking, and because we do not need the lock
1579 -- to test Self_ID.Common.Activator. That value should only be read and
1580 -- modified by Self.
1582 procedure Vulnerable_Complete_Task (Self_ID : Task_Id) is
1583 begin
1584 pragma Assert (Self_ID.Deferral_Level > 0);
1585 pragma Assert (Self_ID = Self);
1586 pragma Assert (Self_ID.Master_Within = Self_ID.Master_of_Task + 1
1587 or else
1588 Self_ID.Master_Within = Self_ID.Master_of_Task + 2);
1589 pragma Assert (Self_ID.Common.Wait_Count = 0);
1590 pragma Assert (Self_ID.Open_Accepts = null);
1591 pragma Assert (Self_ID.ATC_Nesting_Level = 1);
1593 pragma Debug (Debug.Trace (Self_ID, "V_Complete_Task", 'C'));
1595 if Single_Lock then
1596 Lock_RTS;
1597 end if;
1599 Write_Lock (Self_ID);
1600 Self_ID.Callable := False;
1602 -- In theory, Self should have no pending entry calls left on its
1603 -- call-stack. Each async. select statement should clean its own call,
1604 -- and blocking entry calls should defer abort until the calls are
1605 -- cancelled, then clean up.
1607 Utilities.Cancel_Queued_Entry_Calls (Self_ID);
1608 Unlock (Self_ID);
1610 if Self_ID.Common.Activator /= null then
1611 Vulnerable_Complete_Activation (Self_ID);
1612 end if;
1614 if Single_Lock then
1615 Unlock_RTS;
1616 end if;
1618 -- If Self_ID.Master_Within = Self_ID.Master_of_Task + 2
1619 -- we may have dependent tasks for which we need to wait.
1620 -- Otherwise, we can just exit.
1622 if Self_ID.Master_Within = Self_ID.Master_of_Task + 2 then
1623 Vulnerable_Complete_Master (Self_ID);
1624 end if;
1625 end Vulnerable_Complete_Task;
1627 --------------------------
1628 -- Vulnerable_Free_Task --
1629 --------------------------
1631 -- Recover all runtime system storage associated with the task T.
1632 -- This should only be called after T has terminated and will no
1633 -- longer be referenced.
1635 -- For tasks created by an allocator that fails, due to an exception,
1636 -- it is called from Expunge_Unactivated_Tasks.
1638 -- For tasks created by elaboration of task object declarations it
1639 -- is called from the finalization code of the Task_Wrapper procedure.
1640 -- It is also called from Unchecked_Deallocation, for objects that
1641 -- are or contain tasks.
1643 procedure Vulnerable_Free_Task (T : Task_Id) is
1644 begin
1645 pragma Debug (Debug.Trace (Self, "Vulnerable_Free_Task", 'C', T));
1647 if Single_Lock then
1648 Lock_RTS;
1649 end if;
1651 Write_Lock (T);
1652 Initialization.Finalize_Attributes_Link.all (T);
1653 Unlock (T);
1655 if Single_Lock then
1656 Unlock_RTS;
1657 end if;
1659 System.Task_Primitives.Operations.Finalize_TCB (T);
1660 end Vulnerable_Free_Task;
1662 -- Package elaboration code
1664 begin
1665 -- Establish the Adafinal softlink.
1667 -- This is not done inside the central RTS initialization routine
1668 -- to avoid with-ing this package from System.Tasking.Initialization.
1670 SSL.Adafinal := Finalize_Global_Tasks'Access;
1672 -- Establish soft links for subprograms that manipulate master_id's.
1673 -- This cannot be done when the RTS is initialized, because of various
1674 -- elaboration constraints.
1676 SSL.Current_Master := Stages.Current_Master'Access;
1677 SSL.Enter_Master := Stages.Enter_Master'Access;
1678 SSL.Complete_Master := Stages.Complete_Master'Access;
1679 end System.Tasking.Stages;