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