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