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