OSDN Git Service

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