OSDN Git Service

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