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