1------------------------------------------------------------------------------
2--                                                                          --
3--                 GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                 --
4--                                                                          --
5--         S Y S T E M . T A S K I N G . I N I T I A L I Z A T I O N        --
6--                                                                          --
7--                                  B o d y                                 --
8--                                                                          --
9--         Copyright (C) 1992-2021, 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 Style_Checks (All_Checks);
33--  Turn off subprogram alpha ordering check, since we group soft link bodies
34--  and dummy soft link bodies together separately in this unit.
35
36with System.Task_Primitives;
37with System.Task_Primitives.Operations;
38with System.Soft_Links;
39with System.Soft_Links.Tasking;
40with System.Tasking.Debug;
41with System.Tasking.Task_Attributes;
42
43with System.Secondary_Stack;
44pragma Elaborate_All (System.Secondary_Stack);
45pragma Unreferenced (System.Secondary_Stack);
46--  Make sure the body of Secondary_Stack is elaborated before calling
47--  Init_Tasking_Soft_Links. See comments for this routine for explanation.
48
49package body System.Tasking.Initialization is
50
51   package STPO renames System.Task_Primitives.Operations;
52   package SSL  renames System.Soft_Links;
53
54   use Parameters;
55   use Task_Primitives.Operations;
56
57   Global_Task_Lock : aliased System.Task_Primitives.RTS_Lock;
58   --  This is a global lock; it is used to execute in mutual exclusion from
59   --  all other tasks. It is only used by Task_Lock, Task_Unlock, and
60   --  Final_Task_Unlock.
61
62   ----------------------------------------------------------------------
63   -- Tasking versions of some services needed by non-tasking programs --
64   ----------------------------------------------------------------------
65
66   procedure Abort_Defer;
67   --  NON-INLINE versions without Self_ID for soft links
68
69   procedure Abort_Undefer;
70   --  NON-INLINE versions without Self_ID for soft links
71
72   procedure Task_Lock;
73   --  Locks out other tasks. Preceding a section of code by Task_Lock and
74   --  following it by Task_Unlock creates a critical region. This is used
75   --  for ensuring that a region of non-tasking code (such as code used to
76   --  allocate memory) is tasking safe. Note that it is valid for calls to
77   --  Task_Lock/Task_Unlock to be nested, and this must work properly, i.e.
78   --  only the corresponding outer level Task_Unlock will actually unlock.
79
80   procedure Task_Unlock;
81   --  Releases lock previously set by call to Task_Lock. In the nested case,
82   --  all nested locks must be released before other tasks competing for the
83   --  tasking lock are released.
84
85   function Get_Current_Excep return SSL.EOA;
86   --  Task-safe version of SSL.Get_Current_Excep
87
88   function Task_Name return String;
89   --  Returns current task's name
90
91   ------------------------
92   --  Local Subprograms --
93   ------------------------
94
95   ----------------------------
96   -- Tasking Initialization --
97   ----------------------------
98
99   procedure Init_RTS;
100   --  This procedure completes the initialization of the GNARL. The first part
101   --  of the initialization is done in the body of System.Tasking. It consists
102   --  of initializing global locks, and installing tasking versions of certain
103   --  operations used by the compiler. Init_RTS is called during elaboration.
104
105   --------------------------
106   -- Change_Base_Priority --
107   --------------------------
108
109   --  Call only with abort deferred and holding Self_ID locked
110
111   procedure Change_Base_Priority (T : Task_Id) is
112   begin
113      if T.Common.Base_Priority /= T.New_Base_Priority then
114         T.Common.Base_Priority := T.New_Base_Priority;
115         Set_Priority (T, T.Common.Base_Priority);
116      end if;
117   end Change_Base_Priority;
118
119   ------------------------
120   -- Check_Abort_Status --
121   ------------------------
122
123   function Check_Abort_Status return Integer is
124      Self_ID : constant Task_Id := Self;
125   begin
126      if Self_ID /= null
127        and then Self_ID.Deferral_Level = 0
128        and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
129        and then not Self_ID.Aborting
130      then
131         return 1;
132      else
133         return 0;
134      end if;
135   end Check_Abort_Status;
136
137   -----------------
138   -- Defer_Abort --
139   -----------------
140
141   procedure Defer_Abort (Self_ID : Task_Id) is
142   begin
143      if No_Abort then
144         return;
145      end if;
146
147      pragma Assert (Self_ID.Deferral_Level = 0);
148
149      --  pragma Assert
150      --    (Self_ID.Pending_ATC_Level >= Self_ID.ATC_Nesting_Level);
151
152      --  The above check has been useful in detecting mismatched defer/undefer
153      --  pairs. You may uncomment it when testing on systems that support
154      --  preemptive abort.
155
156      --  If the OS supports preemptive abort (e.g. pthread_kill), it should
157      --  have happened already. A problem is with systems that do not support
158      --  preemptive abort, and so rely on polling. On such systems we may get
159      --  false failures of the assertion, since polling for pending abort does
160      --  no occur until the abort undefer operation.
161
162      --  Even on systems that only poll for abort, the assertion may be useful
163      --  for catching missed abort completion polling points. The operations
164      --  that undefer abort poll for pending aborts. This covers most of the
165      --  places where the core Ada semantics require abort to be caught,
166      --  without any special attention. However, this generally happens on
167      --  exit from runtime system call, which means a pending abort will not
168      --  be noticed on the way into the runtime system. We considered adding a
169      --  check for pending aborts at this point, but chose not to, because of
170      --  the overhead. Instead, we searched for RTS calls where abort
171      --  completion is required and a task could go farther than Ada allows
172      --  before undeferring abort; we then modified the code to ensure the
173      --  abort would be detected.
174
175      Self_ID.Deferral_Level := Self_ID.Deferral_Level + 1;
176   end Defer_Abort;
177
178   --------------------------
179   -- Defer_Abort_Nestable --
180   --------------------------
181
182   procedure Defer_Abort_Nestable (Self_ID : Task_Id) is
183   begin
184      if No_Abort then
185         return;
186      end if;
187
188      --  The following assertion is by default disabled. See the comment in
189      --  Defer_Abort on the situations in which it may be useful to uncomment
190      --  this assertion and enable the test.
191
192      --  pragma Assert
193      --    (Self_ID.Pending_ATC_Level >= Self_ID.ATC_Nesting_Level or else
194      --     Self_ID.Deferral_Level > 0);
195
196      Self_ID.Deferral_Level := Self_ID.Deferral_Level + 1;
197   end Defer_Abort_Nestable;
198
199   -----------------
200   -- Abort_Defer --
201   -----------------
202
203   procedure Abort_Defer is
204      Self_ID : Task_Id;
205   begin
206      if No_Abort then
207         return;
208      end if;
209
210      Self_ID := STPO.Self;
211      Self_ID.Deferral_Level := Self_ID.Deferral_Level + 1;
212   end Abort_Defer;
213
214   -----------------------
215   -- Get_Current_Excep --
216   -----------------------
217
218   function Get_Current_Excep return SSL.EOA is
219   begin
220      return STPO.Self.Common.Compiler_Data.Current_Excep'Access;
221   end Get_Current_Excep;
222
223   -----------------------
224   -- Do_Pending_Action --
225   -----------------------
226
227   --  Call only when holding no locks
228
229   procedure Do_Pending_Action (Self_ID : Task_Id) is
230
231   begin
232      pragma Assert (Self_ID = Self and then Self_ID.Deferral_Level = 0);
233
234      --  Needs loop to recheck for pending action in case a new one occurred
235      --  while we had abort deferred below.
236
237      loop
238         --  Temporarily defer abort so that we can lock Self_ID
239
240         Self_ID.Deferral_Level := Self_ID.Deferral_Level + 1;
241
242         Write_Lock (Self_ID);
243         Self_ID.Pending_Action := False;
244         Unlock (Self_ID);
245
246         --  Restore the original Deferral value
247
248         Self_ID.Deferral_Level := Self_ID.Deferral_Level - 1;
249
250         if not Self_ID.Pending_Action then
251            if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level then
252               if not Self_ID.Aborting then
253                  Self_ID.Aborting := True;
254                  pragma Debug
255                    (Debug.Trace (Self_ID, "raise Abort_Signal", 'B'));
256                  raise Standard'Abort_Signal;
257
258                  pragma Assert (not Self_ID.ATC_Hack);
259
260               elsif Self_ID.ATC_Hack then
261
262                  --  The solution really belongs in the Abort_Signal handler
263                  --  for async. entry calls.  The present hack is very
264                  --  fragile. It relies that the very next point after
265                  --  Exit_One_ATC_Level at which the task becomes abortable
266                  --  will be the call to Undefer_Abort in the
267                  --  Abort_Signal handler.
268
269                  Self_ID.ATC_Hack := False;
270
271                  pragma Debug
272                    (Debug.Trace
273                     (Self_ID, "raise Abort_Signal (ATC hack)", 'B'));
274                  raise Standard'Abort_Signal;
275               end if;
276            end if;
277
278            return;
279         end if;
280      end loop;
281   end Do_Pending_Action;
282
283   -----------------------
284   -- Final_Task_Unlock --
285   -----------------------
286
287   --  This version is only for use in Terminate_Task, when the task is
288   --  relinquishing further rights to its own ATCB.
289
290   --  There is a very interesting potential race condition there, where the
291   --  old task may run concurrently with a new task that is allocated the old
292   --  tasks (now reused) ATCB. The critical thing here is to not make any
293   --  reference to the ATCB after the lock is released. See also comments on
294   --  Terminate_Task and Unlock.
295
296   procedure Final_Task_Unlock (Self_ID : Task_Id) is
297   begin
298      pragma Assert (Self_ID.Common.Global_Task_Lock_Nesting = 1);
299      Unlock (Global_Task_Lock'Access);
300   end Final_Task_Unlock;
301
302   --------------
303   -- Init_RTS --
304   --------------
305
306   procedure Init_RTS is
307      Self_Id : Task_Id;
308   begin
309      Tasking.Initialize;
310
311      --  Terminate run time (regular vs restricted) specific initialization
312      --  of the environment task.
313
314      Self_Id := Environment_Task;
315      Self_Id.Master_Of_Task := Environment_Task_Level;
316      Self_Id.Master_Within := Self_Id.Master_Of_Task + 1;
317
318      for L in Self_Id.Entry_Calls'Range loop
319         Self_Id.Entry_Calls (L).Self := Self_Id;
320         Self_Id.Entry_Calls (L).Level := L;
321      end loop;
322
323      Self_Id.Awake_Count := 1;
324      Self_Id.Alive_Count := 1;
325
326      --  Normally, a task starts out with internal master nesting level one
327      --  larger than external master nesting level. It is incremented to one
328      --  by Enter_Master, which is called in the task body only if the
329      --  compiler thinks the task may have dependent tasks. There is no
330      --  corresponding call to Enter_Master for the environment task, so we
331      --  would need to increment it to 2 here. Instead, we set it to 3. By
332      --  doing this we reserve the level 2 for server tasks of the runtime
333      --  system. The environment task does not need to wait for these server
334
335      Self_Id.Master_Within := Library_Task_Level;
336
337      --  Initialize lock used to implement mutual exclusion between all tasks
338
339      Initialize_Lock (Global_Task_Lock'Access, STPO.Global_Task_Level);
340
341      --  Notify that the tasking run time has been elaborated so that
342      --  the tasking version of the soft links can be used.
343
344      if not No_Abort then
345         SSL.Abort_Defer   := Abort_Defer'Access;
346         SSL.Abort_Undefer := Abort_Undefer'Access;
347      end if;
348
349      SSL.Lock_Task          := Task_Lock'Access;
350      SSL.Unlock_Task        := Task_Unlock'Access;
351      SSL.Check_Abort_Status := Check_Abort_Status'Access;
352      SSL.Task_Name          := Task_Name'Access;
353      SSL.Get_Current_Excep  := Get_Current_Excep'Access;
354
355      --  Initialize the tasking soft links (if not done yet) that are common
356      --  to the full and the restricted run times.
357
358      SSL.Tasking.Init_Tasking_Soft_Links;
359
360      --  Abort is deferred in a new ATCB, so we need to undefer abort at this
361      --  stage to make the environment task abortable.
362
363      Undefer_Abort (Environment_Task);
364   end Init_RTS;
365
366   ---------------------------
367   -- Locked_Abort_To_Level--
368   ---------------------------
369
370   --  Abort a task to the specified ATC nesting level.
371   --  Call this only with T locked.
372
373   --  An earlier version of this code contained a call to Wakeup. That should
374   --  not be necessary here, if Abort_Task is implemented correctly, since
375   --  Abort_Task should include the effect of Wakeup. However, the above call
376   --  was in earlier versions of this file, and at least for some targets
377   --  Abort_Task has not been doing Wakeup. It should not hurt to uncomment
378   --  the above call, until the error is corrected for all targets.
379
380   --  See extended comments in package body System.Tasking.Abort for the
381   --  overall design of the implementation of task abort.
382   --  ??? there is no such package ???
383
384   --  If the task is sleeping it will be in an abort-deferred region, and will
385   --  not have Abort_Signal raised by Abort_Task. Such an "abort deferral" is
386   --  just to protect the RTS internals, and not necessarily required to
387   --  enforce Ada semantics. Abort_Task should wake the task up and let it
388   --  decide if it wants to complete the aborted construct immediately.
389
390   --  Note that the effect of the low-level Abort_Task is not persistent.
391   --  If the target task is not blocked, this wakeup will be missed.
392
393   --  We don't bother calling Abort_Task if this task is aborting itself,
394   --  since we are inside the RTS and have abort deferred. Similarly, We don't
395   --  bother to call Abort_Task if T is terminated, since there is no need to
396   --  abort a terminated task, and it could be dangerous to try if the task
397   --  has stopped executing.
398
399   --  Note that an earlier version of this code had some false reasoning about
400   --  being able to reliably wake up a task that had suspended on a blocking
401   --  system call that does not atomically release the task's lock (e.g., UNIX
402   --  nanosleep, which we once thought could be used to implement delays).
403   --  That still left the possibility of missed wakeups.
404
405   --  We cannot safely call Vulnerable_Complete_Activation here, since that
406   --  requires locking Self_ID.Parent. The anti-deadlock lock ordering rules
407   --  would then require us to release the lock on Self_ID first, which would
408   --  create a timing window for other tasks to lock Self_ID. This is
409   --  significant for tasks that may be aborted before their execution can
410   --  enter the task body, and so they do not get a chance to call
411   --  Complete_Task. The actual work for this case is done in Terminate_Task.
412
413   procedure Locked_Abort_To_Level
414     (Self_ID : Task_Id;
415      T       : Task_Id;
416      L       : ATC_Level_Base)
417   is
418   begin
419      if not T.Aborting and then T /= Self_ID then
420         case T.Common.State is
421            when Terminated
422               | Unactivated
423            =>
424               pragma Assert (Standard.False);
425               null;
426
427            when Activating
428               | Runnable
429            =>
430               if T.ATC_Nesting_Level > Level_No_ATC_Occurring then
431                  --  This scenario occurs when an asynchronous protected entry
432                  --  call is canceled during a requeue with abort.
433
434                  T.Entry_Calls
435                    (T.ATC_Nesting_Level).Cancellation_Attempted := True;
436               end if;
437
438            when Interrupt_Server_Blocked_On_Event_Flag =>
439               null;
440
441            when AST_Server_Sleep
442               | Async_Select_Sleep
443               | Delay_Sleep
444               | Interrupt_Server_Blocked_Interrupt_Sleep
445               | Interrupt_Server_Idle_Sleep
446               | Timer_Server_Sleep
447            =>
448               Wakeup (T, T.Common.State);
449
450            when Acceptor_Delay_Sleep
451               | Acceptor_Sleep
452            =>
453               T.Open_Accepts := null;
454               Wakeup (T, T.Common.State);
455
456            when Entry_Caller_Sleep  =>
457               pragma Assert (T.ATC_Nesting_Level > Level_No_ATC_Occurring);
458
459               T.Entry_Calls
460                 (T.ATC_Nesting_Level).Cancellation_Attempted := True;
461               Wakeup (T, T.Common.State);
462
463            when Activator_Sleep
464               | Asynchronous_Hold
465               | Master_Completion_Sleep
466               | Master_Phase_2_Sleep
467            =>
468               null;
469         end case;
470      end if;
471
472      if T.Pending_ATC_Level > L then
473         T.Pending_ATC_Level := L;
474         T.Pending_Action := True;
475
476         if L = Level_Completed_Task then
477            T.Callable := False;
478         end if;
479
480         --  This prevents aborted task from accepting calls
481
482         if T.Aborting then
483
484            --  The test above is just a heuristic, to reduce wasteful
485            --  calls to Abort_Task.  We are holding T locked, and this
486            --  value will not be set to False except with T also locked,
487            --  inside Exit_One_ATC_Level, so we should not miss wakeups.
488
489            if T.Common.State = Acceptor_Sleep
490                 or else
491               T.Common.State = Acceptor_Delay_Sleep
492            then
493               T.Open_Accepts := null;
494            end if;
495
496         elsif T /= Self_ID and then
497           (T.Common.State = Runnable
498             or else T.Common.State = Interrupt_Server_Blocked_On_Event_Flag)
499
500            --  The task is blocked on a system call waiting for the
501            --  completion event. In this case Abort_Task may need to take
502            --  special action in order to succeed.
503
504         then
505            Abort_Task (T);
506         end if;
507      end if;
508   end Locked_Abort_To_Level;
509
510   --------------------------------
511   -- Remove_From_All_Tasks_List --
512   --------------------------------
513
514   procedure Remove_From_All_Tasks_List (T : Task_Id) is
515      C        : Task_Id;
516      Previous : Task_Id;
517
518   begin
519      pragma Debug
520        (Debug.Trace (Self, "Remove_From_All_Tasks_List", 'C'));
521
522      Previous := Null_Task;
523      C := All_Tasks_List;
524      while C /= Null_Task loop
525         if C = T then
526            if Previous = Null_Task then
527               All_Tasks_List := All_Tasks_List.Common.All_Tasks_Link;
528            else
529               Previous.Common.All_Tasks_Link := C.Common.All_Tasks_Link;
530            end if;
531
532            return;
533         end if;
534
535         Previous := C;
536         C := C.Common.All_Tasks_Link;
537      end loop;
538
539      pragma Assert (Standard.False);
540   end Remove_From_All_Tasks_List;
541
542   ---------------
543   -- Task_Lock --
544   ---------------
545
546   procedure Task_Lock (Self_ID : Task_Id) is
547   begin
548      Self_ID.Common.Global_Task_Lock_Nesting :=
549        Self_ID.Common.Global_Task_Lock_Nesting + 1;
550
551      if Self_ID.Common.Global_Task_Lock_Nesting = 1 then
552         Defer_Abort_Nestable (Self_ID);
553         Write_Lock (Global_Task_Lock'Access);
554      end if;
555   end Task_Lock;
556
557   procedure Task_Lock is
558   begin
559      Task_Lock (STPO.Self);
560   end Task_Lock;
561
562   ---------------
563   -- Task_Name --
564   ---------------
565
566   function Task_Name return String is
567      Self_Id : constant Task_Id := STPO.Self;
568   begin
569      return Self_Id.Common.Task_Image (1 .. Self_Id.Common.Task_Image_Len);
570   end Task_Name;
571
572   -----------------
573   -- Task_Unlock --
574   -----------------
575
576   procedure Task_Unlock (Self_ID : Task_Id) is
577   begin
578      pragma Assert (Self_ID.Common.Global_Task_Lock_Nesting > 0);
579      Self_ID.Common.Global_Task_Lock_Nesting :=
580        Self_ID.Common.Global_Task_Lock_Nesting - 1;
581
582      if Self_ID.Common.Global_Task_Lock_Nesting = 0 then
583         Unlock (Global_Task_Lock'Access);
584         Undefer_Abort_Nestable (Self_ID);
585      end if;
586   end Task_Unlock;
587
588   procedure Task_Unlock is
589   begin
590      Task_Unlock (STPO.Self);
591   end Task_Unlock;
592
593   -------------------
594   -- Undefer_Abort --
595   -------------------
596
597   --  Precondition : Self does not hold any locks
598
599   --  Undefer_Abort is called on any abort completion point (aka.
600   --  synchronization point). It performs the following actions if they
601   --  are pending: (1) change the base priority, (2) abort the task.
602
603   --  The priority change has to occur before abort. Otherwise, it would
604   --  take effect no earlier than the next abort completion point.
605
606   procedure Undefer_Abort (Self_ID : Task_Id) is
607   begin
608      if No_Abort then
609         return;
610      end if;
611
612      pragma Assert (Self_ID.Deferral_Level = 1);
613
614      Self_ID.Deferral_Level := Self_ID.Deferral_Level - 1;
615
616      if Self_ID.Deferral_Level = 0 then
617         pragma Assert (Check_No_Locks (Self_ID));
618
619         if Self_ID.Pending_Action then
620            Do_Pending_Action (Self_ID);
621         end if;
622      end if;
623   end Undefer_Abort;
624
625   ----------------------------
626   -- Undefer_Abort_Nestable --
627   ----------------------------
628
629   --  An earlier version would re-defer abort if an abort is in progress.
630   --  Then, we modified the effect of the raise statement so that it defers
631   --  abort until control reaches a handler. That was done to prevent
632   --  "skipping over" a handler if another asynchronous abort occurs during
633   --  the propagation of the abort to the handler.
634
635   --  There has been talk of reversing that decision, based on a newer
636   --  implementation of exception propagation. Care must be taken to evaluate
637   --  how such a change would interact with the above code and all the places
638   --  where abort-deferral is used to bridge over critical transitions, such
639   --  as entry to the scope of a region with a finalizer and entry into the
640   --  body of an accept-procedure.
641
642   procedure Undefer_Abort_Nestable (Self_ID : Task_Id) is
643   begin
644      if No_Abort then
645         return;
646      end if;
647
648      pragma Assert (Self_ID.Deferral_Level > 0);
649
650      Self_ID.Deferral_Level := Self_ID.Deferral_Level - 1;
651
652      if Self_ID.Deferral_Level = 0 then
653
654         pragma Assert (Check_No_Locks (Self_ID));
655
656         if Self_ID.Pending_Action then
657            Do_Pending_Action (Self_ID);
658         end if;
659      end if;
660   end Undefer_Abort_Nestable;
661
662   -------------------
663   -- Abort_Undefer --
664   -------------------
665
666   procedure Abort_Undefer is
667      Self_ID : Task_Id;
668   begin
669      if No_Abort then
670         return;
671      end if;
672
673      Self_ID := STPO.Self;
674
675      if Self_ID.Deferral_Level = 0 then
676
677         --  In case there are different views on whether Abort is supported
678         --  between the expander and the run time, we may end up with
679         --  Self_ID.Deferral_Level being equal to zero, when called from
680         --  the procedure created by the expander that corresponds to a
681         --  task body. In this case, there's nothing to be done.
682
683         --  See related code in System.Tasking.Stages.Create_Task resetting
684         --  Deferral_Level when System.Restrictions.Abort_Allowed is False.
685
686         return;
687      end if;
688
689      pragma Assert (Self_ID.Deferral_Level > 0);
690      Self_ID.Deferral_Level := Self_ID.Deferral_Level - 1;
691
692      if Self_ID.Deferral_Level = 0 then
693         pragma Assert (Check_No_Locks (Self_ID));
694
695         if Self_ID.Pending_Action then
696            Do_Pending_Action (Self_ID);
697         end if;
698      end if;
699   end Abort_Undefer;
700
701   --------------------------
702   -- Wakeup_Entry_Caller --
703   --------------------------
704
705   --  This is called at the end of service of an entry call, to abort the
706   --  caller if he is in an abortable part, and to wake up the caller if it
707   --  is on Entry_Caller_Sleep. It assumes that the call is already off-queue.
708
709   --  (This enforces the rule that a task must be off-queue if its state is
710   --  Done or Cancelled.) Call it holding the lock of Entry_Call.Self.
711
712   --  Timed_Call or Simple_Call:
713   --    The caller is waiting on Entry_Caller_Sleep, in
714   --    Wait_For_Completion, or Wait_For_Completion_With_Timeout.
715
716   --  Conditional_Call:
717   --    The caller might be in Wait_For_Completion,
718   --    waiting for a rendezvous (possibly requeued without abort)
719   --    to complete.
720
721   --  Asynchronous_Call:
722   --    The caller may be executing in the abortable part o
723   --    an async. select, or on a time delay,
724   --    if Entry_Call.State >= Was_Abortable.
725
726   procedure Wakeup_Entry_Caller
727     (Self_ID    : Task_Id;
728      Entry_Call : Entry_Call_Link;
729      New_State  : Entry_Call_State)
730   is
731      Caller : constant Task_Id := Entry_Call.Self;
732
733   begin
734      pragma Debug (Debug.Trace
735        (Self_ID, "Wakeup_Entry_Caller", 'E', Caller));
736      pragma Assert (New_State = Done or else New_State = Cancelled);
737
738      pragma Assert (Caller.Common.State /= Unactivated);
739
740      Entry_Call.State := New_State;
741
742      if Entry_Call.Mode = Asynchronous_Call then
743
744         --  Abort the caller in his abortable part, but do so only if call has
745         --  been queued abortably.
746
747         if Entry_Call.State >= Was_Abortable or else New_State = Done then
748            Locked_Abort_To_Level (Self_ID, Caller, Entry_Call.Level - 1);
749         end if;
750
751      elsif Caller.Common.State = Entry_Caller_Sleep then
752         Wakeup (Caller, Entry_Caller_Sleep);
753      end if;
754   end Wakeup_Entry_Caller;
755
756   -------------------------
757   -- Finalize_Attributes --
758   -------------------------
759
760   procedure Finalize_Attributes (T : Task_Id) is
761      Attr : Atomic_Address;
762
763   begin
764      for J in T.Attributes'Range loop
765         Attr := T.Attributes (J);
766
767         if Attr /= 0 and then Task_Attributes.Require_Finalization (J) then
768            Task_Attributes.To_Attribute (Attr).Free (Attr);
769            T.Attributes (J) := 0;
770         end if;
771      end loop;
772   end Finalize_Attributes;
773
774begin
775   Init_RTS;
776end System.Tasking.Initialization;
777