OSDN Git Service

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