1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
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 --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
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. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
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.
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.
44 -- Used for Exception_Occurrence_Access
46 with System.Task_Primitives;
49 with System.Task_Primitives.Operations;
50 -- Used for Set_Priority
55 with System.Soft_Links;
56 -- Used for the non-tasking routines (*_NT) that refer to global data.
57 -- They are needed here before the tasking run time has been elaborated.
59 with System.Soft_Links.Tasking;
60 -- Used for Init_Tasking_Soft_Links
62 with System.Tasking.Debug;
65 with System.Parameters;
66 -- used for Single_Lock
68 package body System.Tasking.Initialization is
70 package STPO renames System.Task_Primitives.Operations;
71 package SSL renames System.Soft_Links;
72 package AE renames Ada.Exceptions;
75 use Task_Primitives.Operations;
77 Global_Task_Lock : aliased System.Task_Primitives.RTS_Lock;
78 -- This is a global lock; it is used to execute in mutual exclusion
79 -- from all other tasks. It is only used by Task_Lock,
80 -- Task_Unlock, and Final_Task_Unlock.
82 ----------------------------------------------------------------------
83 -- Tasking versions of some services needed by non-tasking programs --
84 ----------------------------------------------------------------------
86 procedure Abort_Defer;
87 -- NON-INLINE versions without Self_ID for soft links
89 procedure Abort_Undefer;
90 -- NON-INLINE versions without Self_ID for soft links
93 -- Locks out other tasks. Preceding a section of code by Task_Lock and
94 -- following it by Task_Unlock creates a critical region. This is used
95 -- for ensuring that a region of non-tasking code (such as code used to
96 -- allocate memory) is tasking safe. Note that it is valid for calls to
97 -- Task_Lock/Task_Unlock to be nested, and this must work properly, i.e.
98 -- only the corresponding outer level Task_Unlock will actually unlock.
100 procedure Task_Unlock;
101 -- Releases lock previously set by call to Task_Lock. In the nested case,
102 -- all nested locks must be released before other tasks competing for the
103 -- tasking lock are released.
105 function Get_Current_Excep return SSL.EOA;
106 -- Task-safe version of SSL.Get_Current_Excep
108 procedure Update_Exception
109 (X : AE.Exception_Occurrence := SSL.Current_Target_Exception);
110 -- Handle exception setting and check for pending actions
112 function Task_Name return String;
113 -- Returns current task's name
115 ------------------------
116 -- Local Subprograms --
117 ------------------------
119 ----------------------------
120 -- Tasking Initialization --
121 ----------------------------
124 -- This procedure completes the initialization of the GNARL. The first
125 -- part of the initialization is done in the body of System.Tasking.
126 -- It consists of initializing global locks, and installing tasking
127 -- versions of certain operations used by the compiler. Init_RTS is called
128 -- during elaboration.
130 --------------------------
131 -- Change_Base_Priority --
132 --------------------------
134 -- Call only with abort deferred and holding Self_ID locked
136 procedure Change_Base_Priority (T : Task_Id) is
138 if T.Common.Base_Priority /= T.New_Base_Priority then
139 T.Common.Base_Priority := T.New_Base_Priority;
140 Set_Priority (T, T.Common.Base_Priority);
142 end Change_Base_Priority;
144 ------------------------
145 -- Check_Abort_Status --
146 ------------------------
148 function Check_Abort_Status return Integer is
149 Self_ID : constant Task_Id := Self;
151 if Self_ID /= null and then Self_ID.Deferral_Level = 0
152 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
158 end Check_Abort_Status;
164 procedure Defer_Abort (Self_ID : Task_Id) is
170 pragma Assert (Self_ID.Deferral_Level = 0);
173 -- (Self_ID.Pending_ATC_Level >= Self_ID.ATC_Nesting_Level);
175 -- The above check has been useful in detecting mismatched defer/undefer
176 -- pairs. You may uncomment it when testing on systems that support
179 -- If the OS supports preemptive abort (e.g. pthread_kill), it should
180 -- have happened already. A problem is with systems that do not support
181 -- preemptive abort, and so rely on polling. On such systems we may get
182 -- false failures of the assertion, since polling for pending abort does
183 -- no occur until the abort undefer operation.
185 -- Even on systems that only poll for abort, the assertion may be useful
186 -- for catching missed abort completion polling points. The operations
187 -- that undefer abort poll for pending aborts. This covers most of the
188 -- places where the core Ada semantics require abort to be caught,
189 -- without any special attention. However, this generally happens on
190 -- exit from runtime system call, which means a pending abort will not
191 -- be noticed on the way into the runtime system. We considered adding a
192 -- check for pending aborts at this point, but chose not to, because of
193 -- the overhead. Instead, we searched for RTS calls where abort
194 -- completion is required and a task could go farther than Ada allows
195 -- before undeferring abort; we then modified the code to ensure the
196 -- abort would be detected.
198 Self_ID.Deferral_Level := Self_ID.Deferral_Level + 1;
201 --------------------------
202 -- Defer_Abort_Nestable --
203 --------------------------
205 procedure Defer_Abort_Nestable (Self_ID : Task_Id) is
212 -- ((Self_ID.Pending_ATC_Level >= Self_ID.ATC_Nesting_Level or else
213 -- Self_ID.Deferral_Level > 0));
215 -- See comment in Defer_Abort on the situations in which it may be
216 -- useful to uncomment the above assertion.
218 Self_ID.Deferral_Level := Self_ID.Deferral_Level + 1;
219 end Defer_Abort_Nestable;
225 procedure Abort_Defer is
232 Self_ID := STPO.Self;
233 Self_ID.Deferral_Level := Self_ID.Deferral_Level + 1;
236 -----------------------
237 -- Get_Current_Excep --
238 -----------------------
240 function Get_Current_Excep return SSL.EOA is
242 return STPO.Self.Common.Compiler_Data.Current_Excep'Access;
243 end Get_Current_Excep;
245 -----------------------
246 -- Do_Pending_Action --
247 -----------------------
249 -- Call only when holding no locks
251 procedure Do_Pending_Action (Self_ID : Task_Id) is
252 use type Ada.Exceptions.Exception_Id;
255 pragma Assert (Self_ID = Self and then Self_ID.Deferral_Level = 0);
257 -- Needs loop to recheck for pending action in case a new one occurred
258 -- while we had abort deferred below.
261 -- Temporarily defer abort so that we can lock Self_ID
263 Self_ID.Deferral_Level := Self_ID.Deferral_Level + 1;
269 Write_Lock (Self_ID);
270 Self_ID.Pending_Action := False;
277 -- Restore the original Deferral value
279 Self_ID.Deferral_Level := Self_ID.Deferral_Level - 1;
281 if not Self_ID.Pending_Action then
282 if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level then
283 if not Self_ID.Aborting then
284 Self_ID.Aborting := True;
286 (Debug.Trace (Self_ID, "raise Abort_Signal", 'B'));
287 raise Standard'Abort_Signal;
289 pragma Assert (not Self_ID.ATC_Hack);
291 elsif Self_ID.ATC_Hack then
292 -- The solution really belongs in the Abort_Signal handler
293 -- for async. entry calls. The present hack is very
294 -- fragile. It relies that the very next point after
295 -- Exit_One_ATC_Level at which the task becomes abortable
296 -- will be the call to Undefer_Abort in the
297 -- Abort_Signal handler.
299 Self_ID.ATC_Hack := False;
303 (Self_ID, "raise Abort_Signal (ATC hack)", 'B'));
304 raise Standard'Abort_Signal;
311 end Do_Pending_Action;
313 -----------------------
314 -- Final_Task_Unlock --
315 -----------------------
317 -- This version is only for use in Terminate_Task, when the task
318 -- is relinquishing further rights to its own ATCB.
319 -- There is a very interesting potential race condition there, where
320 -- the old task may run concurrently with a new task that is allocated
321 -- the old tasks (now reused) ATCB. The critical thing here is to
322 -- not make any reference to the ATCB after the lock is released.
323 -- See also comments on Terminate_Task and Unlock.
325 procedure Final_Task_Unlock (Self_ID : Task_Id) is
327 pragma Assert (Self_ID.Common.Global_Task_Lock_Nesting = 1);
328 Unlock (Global_Task_Lock'Access, Global_Lock => True);
329 end Final_Task_Unlock;
335 procedure Init_RTS is
340 -- Terminate run time (regular vs restricted) specific initialization
341 -- of the environment task.
343 Self_Id := Environment_Task;
344 Self_Id.Master_of_Task := Environment_Task_Level;
345 Self_Id.Master_Within := Self_Id.Master_of_Task + 1;
347 for L in Self_Id.Entry_Calls'Range loop
348 Self_Id.Entry_Calls (L).Self := Self_Id;
349 Self_Id.Entry_Calls (L).Level := L;
352 Self_Id.Awake_Count := 1;
353 Self_Id.Alive_Count := 1;
355 Self_Id.Master_Within := Library_Task_Level;
356 -- Normally, a task starts out with internal master nesting level
357 -- one larger than external master nesting level. It is incremented
358 -- to one by Enter_Master, which is called in the task body only if
359 -- the compiler thinks the task may have dependent tasks. There is no
360 -- corresponding call to Enter_Master for the environment task, so we
361 -- would need to increment it to 2 here. Instead, we set it to 3.
362 -- By doing this we reserve the level 2 for server tasks of the runtime
363 -- system. The environment task does not need to wait for these server
365 -- Initialize lock used to implement mutual exclusion between all tasks
367 Initialize_Lock (Global_Task_Lock'Access, STPO.Global_Task_Level);
369 -- Notify that the tasking run time has been elaborated so that
370 -- the tasking version of the soft links can be used.
373 SSL.Abort_Defer := Abort_Defer'Access;
374 SSL.Abort_Undefer := Abort_Undefer'Access;
377 SSL.Lock_Task := Task_Lock'Access;
378 SSL.Unlock_Task := Task_Unlock'Access;
379 SSL.Check_Abort_Status := Check_Abort_Status'Access;
380 SSL.Task_Name := Task_Name'Access;
381 SSL.Update_Exception := Update_Exception'Access;
382 SSL.Get_Current_Excep := Get_Current_Excep'Access;
384 -- Initialize the tasking soft links (if not done yet) that are common
385 -- to the full and the restricted run times.
387 SSL.Tasking.Init_Tasking_Soft_Links;
389 -- Abort is deferred in a new ATCB, so we need to undefer abort
390 -- at this stage to make the environment task abortable.
392 Undefer_Abort (Environment_Task);
395 ---------------------------
396 -- Locked_Abort_To_Level--
397 ---------------------------
399 -- Abort a task to the specified ATC nesting level.
400 -- Call this only with T locked.
402 -- An earlier version of this code contained a call to Wakeup. That
403 -- should not be necessary here, if Abort_Task is implemented correctly,
404 -- since Abort_Task should include the effect of Wakeup. However, the
405 -- above call was in earlier versions of this file, and at least for
406 -- some targets Abort_Task has not beek doing Wakeup. It should not
407 -- hurt to uncomment the above call, until the error is corrected for
410 -- See extended comments in package body System.Tasking.Abort for the
411 -- overall design of the implementation of task abort.
412 -- ??? there is no such package ???
414 -- If the task is sleeping it will be in an abort-deferred region, and
415 -- will not have Abort_Signal raised by Abort_Task. Such an "abort
416 -- deferral" is just to protect the RTS internals, and not necessarily
417 -- required to enforce Ada semantics. Abort_Task should wake the task up
418 -- and let it decide if it wants to complete the aborted construct
421 -- Note that the effect of the lowl-level Abort_Task is not persistent.
422 -- If the target task is not blocked, this wakeup will be missed.
424 -- We don't bother calling Abort_Task if this task is aborting itself,
425 -- since we are inside the RTS and have abort deferred. Similarly, We
426 -- don't bother to call Abort_Task if T is terminated, since there is
427 -- no need to abort a terminated task, and it could be dangerous to try
428 -- if the task has stopped executing.
430 -- Note that an earlier version of this code had some false reasoning
431 -- about being able to reliably wake up a task that had suspended on
432 -- a blocking system call that does not atomically relase the task's
433 -- lock (e.g., UNIX nanosleep, which we once thought could be used to
434 -- implement delays). That still left the possibility of missed
437 -- We cannot safely call Vulnerable_Complete_Activation here, since that
438 -- requires locking Self_ID.Parent. The anti-deadlock lock ordering rules
439 -- would then require us to release the lock on Self_ID first, which would
440 -- create a timing window for other tasks to lock Self_ID. This is
441 -- significant for tasks that may be aborted before their execution can
442 -- enter the task body, and so they do not get a chance to call
443 -- Complete_Task. The actual work for this case is done in Terminate_Task.
445 procedure Locked_Abort_To_Level
451 if not T.Aborting and then T /= Self_ID then
452 case T.Common.State is
453 when Unactivated | Terminated =>
454 pragma Assert (False);
458 -- This is needed to cancel an asynchronous protected entry
459 -- call during a requeue with abort.
462 (T.ATC_Nesting_Level).Cancellation_Attempted := True;
464 when Interrupt_Server_Blocked_On_Event_Flag =>
469 Interrupt_Server_Idle_Sleep |
470 Interrupt_Server_Blocked_Interrupt_Sleep |
473 Wakeup (T, T.Common.State);
475 when Acceptor_Sleep =>
476 T.Open_Accepts := null;
477 Wakeup (T, T.Common.State);
479 when Entry_Caller_Sleep =>
481 (T.ATC_Nesting_Level).Cancellation_Attempted := True;
482 Wakeup (T, T.Common.State);
484 when Activator_Sleep |
485 Master_Completion_Sleep |
486 Master_Phase_2_Sleep |
492 if T.Pending_ATC_Level > L then
493 T.Pending_ATC_Level := L;
494 T.Pending_Action := True;
500 -- This prevents aborted task from accepting calls
504 -- The test above is just a heuristic, to reduce wasteful
505 -- calls to Abort_Task. We are holding T locked, and this
506 -- value will not be set to False except with T also locked,
507 -- inside Exit_One_ATC_Level, so we should not miss wakeups.
509 if T.Common.State = Acceptor_Sleep then
510 T.Open_Accepts := null;
513 elsif T /= Self_ID and then
514 (T.Common.State = Runnable
515 or else T.Common.State = Interrupt_Server_Blocked_On_Event_Flag)
516 -- The task is blocked on a system call waiting for the
517 -- completion event. In this case Abort_Task may need to take
518 -- special action in order to succeed. Example system: VMS.
524 end Locked_Abort_To_Level;
526 --------------------------------
527 -- Remove_From_All_Tasks_List --
528 --------------------------------
530 procedure Remove_From_All_Tasks_List (T : Task_Id) is
536 (Debug.Trace (Self, "Remove_From_All_Tasks_List", 'C'));
538 Previous := Null_Task;
541 while C /= Null_Task loop
543 if Previous = Null_Task then
545 All_Tasks_List.Common.All_Tasks_Link;
547 Previous.Common.All_Tasks_Link := C.Common.All_Tasks_Link;
554 C := C.Common.All_Tasks_Link;
557 pragma Assert (False);
558 end Remove_From_All_Tasks_List;
564 procedure Task_Lock (Self_ID : Task_Id) is
566 Self_ID.Common.Global_Task_Lock_Nesting :=
567 Self_ID.Common.Global_Task_Lock_Nesting + 1;
569 if Self_ID.Common.Global_Task_Lock_Nesting = 1 then
570 Defer_Abort_Nestable (Self_ID);
571 Write_Lock (Global_Task_Lock'Access, Global_Lock => True);
575 procedure Task_Lock is
577 Task_Lock (STPO.Self);
584 function Task_Name return String is
585 Self_Id : constant Task_Id := STPO.Self;
588 return Self_Id.Common.Task_Image (1 .. Self_Id.Common.Task_Image_Len);
595 procedure Task_Unlock (Self_ID : Task_Id) is
597 pragma Assert (Self_ID.Common.Global_Task_Lock_Nesting > 0);
598 Self_ID.Common.Global_Task_Lock_Nesting :=
599 Self_ID.Common.Global_Task_Lock_Nesting - 1;
601 if Self_ID.Common.Global_Task_Lock_Nesting = 0 then
602 Unlock (Global_Task_Lock'Access, Global_Lock => True);
603 Undefer_Abort_Nestable (Self_ID);
607 procedure Task_Unlock is
609 Task_Unlock (STPO.Self);
616 -- Precondition : Self does not hold any locks!
618 -- Undefer_Abort is called on any abort completion point (aka.
619 -- synchronization point). It performs the following actions if they
620 -- are pending: (1) change the base priority, (2) abort the task.
622 -- The priority change has to occur before abort. Otherwise, it would
623 -- take effect no earlier than the next abort completion point.
625 procedure Undefer_Abort (Self_ID : Task_Id) is
631 pragma Assert (Self_ID.Deferral_Level = 1);
633 Self_ID.Deferral_Level := Self_ID.Deferral_Level - 1;
635 if Self_ID.Deferral_Level = 0 then
636 pragma Assert (Check_No_Locks (Self_ID));
638 if Self_ID.Pending_Action then
639 Do_Pending_Action (Self_ID);
644 ----------------------------
645 -- Undefer_Abort_Nestable --
646 ----------------------------
648 -- An earlier version would re-defer abort if an abort is in progress.
649 -- Then, we modified the effect of the raise statement so that it defers
650 -- abort until control reaches a handler. That was done to prevent
651 -- "skipping over" a handler if another asynchronous abort occurs during
652 -- the propagation of the abort to the handler.
654 -- There has been talk of reversing that decision, based on a newer
655 -- implementation of exception propagation. Care must be taken to evaluate
656 -- how such a change would interact with the above code and all the places
657 -- where abort-deferral is used to bridge over critical transitions, such
658 -- as entry to the scope of a region with a finalizer and entry into the
659 -- body of an accept-procedure.
661 procedure Undefer_Abort_Nestable (Self_ID : Task_Id) is
667 pragma Assert (Self_ID.Deferral_Level > 0);
669 Self_ID.Deferral_Level := Self_ID.Deferral_Level - 1;
671 if Self_ID.Deferral_Level = 0 then
673 pragma Assert (Check_No_Locks (Self_ID));
675 if Self_ID.Pending_Action then
676 Do_Pending_Action (Self_ID);
679 end Undefer_Abort_Nestable;
685 procedure Abort_Undefer is
692 Self_ID := STPO.Self;
694 if Self_ID.Deferral_Level = 0 then
696 -- In case there are different views on whether Abort is supported
697 -- between the expander and the run time, we may end up with
698 -- Self_ID.Deferral_Level being equal to zero, when called from
699 -- the procedure created by the expander that corresponds to a
702 -- In this case, there's nothing to be done
704 -- See related code in System.Tasking.Stages.Create_Task resetting
705 -- Deferral_Level when System.Restrictions.Abort_Allowed is False.
710 pragma Assert (Self_ID.Deferral_Level > 0);
711 Self_ID.Deferral_Level := Self_ID.Deferral_Level - 1;
713 if Self_ID.Deferral_Level = 0 then
714 pragma Assert (Check_No_Locks (Self_ID));
716 if Self_ID.Pending_Action then
717 Do_Pending_Action (Self_ID);
722 ----------------------
723 -- Update_Exception --
724 ----------------------
726 -- Call only when holding no locks
728 procedure Update_Exception
729 (X : AE.Exception_Occurrence := SSL.Current_Target_Exception)
731 Self_Id : constant Task_Id := Self;
735 Save_Occurrence (Self_Id.Common.Compiler_Data.Current_Excep, X);
737 if Self_Id.Deferral_Level = 0 then
738 if Self_Id.Pending_Action then
739 Self_Id.Pending_Action := False;
740 Self_Id.Deferral_Level := Self_Id.Deferral_Level + 1;
746 Write_Lock (Self_Id);
747 Self_Id.Pending_Action := False;
754 Self_Id.Deferral_Level := Self_Id.Deferral_Level - 1;
756 if Self_Id.Pending_ATC_Level < Self_Id.ATC_Nesting_Level then
757 if not Self_Id.Aborting then
758 Self_Id.Aborting := True;
759 raise Standard'Abort_Signal;
764 end Update_Exception;
766 --------------------------
767 -- Wakeup_Entry_Caller --
768 --------------------------
770 -- This is called at the end of service of an entry call, to abort the
771 -- caller if he is in an abortable part, and to wake up the caller if it
772 -- is on Entry_Caller_Sleep. It assumes that the call is already off-queue.
774 -- (This enforces the rule that a task must be off-queue if its state is
775 -- Done or Cancelled.) Call it holding the lock of Entry_Call.Self.
777 -- Timed_Call or Simple_Call:
778 -- The caller is waiting on Entry_Caller_Sleep, in
779 -- Wait_For_Completion, or Wait_For_Completion_With_Timeout.
782 -- The caller might be in Wait_For_Completion,
783 -- waiting for a rendezvous (possibly requeued without abort)
786 -- Asynchronous_Call:
787 -- The caller may be executing in the abortable part o
788 -- an async. select, or on a time delay,
789 -- if Entry_Call.State >= Was_Abortable.
791 procedure Wakeup_Entry_Caller
793 Entry_Call : Entry_Call_Link;
794 New_State : Entry_Call_State)
796 Caller : constant Task_Id := Entry_Call.Self;
798 pragma Debug (Debug.Trace
799 (Self_ID, "Wakeup_Entry_Caller", 'E', Caller));
800 pragma Assert (New_State = Done or else New_State = Cancelled);
802 pragma Assert (Caller.Common.State /= Unactivated);
804 Entry_Call.State := New_State;
806 if Entry_Call.Mode = Asynchronous_Call then
808 -- Abort the caller in his abortable part,
809 -- but do so only if call has been queued abortably
811 if Entry_Call.State >= Was_Abortable or else New_State = Done then
812 Locked_Abort_To_Level (Self_ID, Caller, Entry_Call.Level - 1);
815 elsif Caller.Common.State = Entry_Caller_Sleep then
816 Wakeup (Caller, Entry_Caller_Sleep);
818 end Wakeup_Entry_Caller;
820 -----------------------
821 -- Soft-Link Dummies --
822 -----------------------
824 -- These are dummies for subprograms that are only needed by certain
825 -- optional run-time system packages. If they are needed, the soft
826 -- links will be redirected to the real subprogram by elaboration of
827 -- the subprogram body where the real subprogram is declared.
829 procedure Finalize_Attributes (T : Task_Id) is
830 pragma Unreferenced (T);
833 end Finalize_Attributes;
835 procedure Initialize_Attributes (T : Task_Id) is
836 pragma Unreferenced (T);
839 end Initialize_Attributes;
843 end System.Tasking.Initialization;