Annotated Ada Reference ManualLegal Information
Contents   Index   References   Search   Previous   Next 

C.7.3 The Package Task_Termination

Static Semantics

1/2
{AI95-00266-02} The following language-defined library package exists: 
2/2
with Ada.Task_Identification;
with Ada.Exceptions;
package Ada.Task_Termination is
   pragma Preelaborate(Task_Termination);
3/2
   type Cause_Of_Termination is (Normal, Abnormal, Unhandled_Exception);
4/2
   type Termination_Handler is access protected procedure
     (Cause : in Cause_Of_Termination;
      T     : in Ada.Task_Identification.Task_Id;
      X     : in Ada.Exceptions.Exception_Occurrence);
5/2
   procedure Set_Dependents_Fallback_Handler
     (Handler: in Termination_Handler);
   function Current_Task_Fallback_Handler return Termination_Handler;
6/2
   procedure Set_Specific_Handler
     (T       : in Ada.Task_Identification.Task_Id;
      Handler : in Termination_Handler);
   function Specific_Handler (T : Ada.Task_Identification.Task_Id)
      return Termination_Handler;
7/2
end Ada.Task_Termination;

Dynamic Semantics

8/3
{AI95-00266-02} {AI05-0202-1} The type Termination_Handler identifies a protected procedure to be executed by the implementation when a task terminates. Such a protected procedure is called a handler. In all cases T identifies the task that is terminating. If the task terminates due to completing the last statement of its body, or as a result of waiting on a terminate alternative, and the finalization of the task completes normally, then Cause is set to Normal and X is set to Null_Occurrence. If the task terminates because it is being aborted, then Cause is set to Abnormal; X is set to Null_Occurrence if the finalization of the task completes normally. If the task terminates because of an exception raised by the execution of its task_body, then Cause is set to Unhandled_Exception; X is set to the associated exception occurrence if the finalization of the task completes normally. Independent of how the task completes, if finalization of the task propagates an exception, then Cause is either Unhandled_Exception or Abnormal, and X is an exception occurrence that identifies the Program_Error exception.
9/2
{AI95-00266-02} Each task has two termination handlers, a fall-back handler and a specific handler. The specific handler applies only to the task itself, while the fall-back handler applies only to the dependent tasks of the task. A handler is said to be set if it is associated with a nonnull value of type Termination_Handler, and cleared otherwise. When a task is created, its specific handler and fall-back handler are cleared.
10/3
{AI95-00266-02} {AI05-0264-1} The procedure Set_Dependents_Fallback_Handler changes the fall-back handler for the calling task: if Handler is null, that fall-back handler is cleared; otherwise, it is set to be Handler.all. If a fall-back handler had previously been set it is replaced.
11/3
{AI95-00266-02} {AI05-0264-1} The function Current_Task_Fallback_Handler returns the fall-back handler that is currently set for the calling task, if one is set; otherwise, it returns null.
12/3
{AI95-00266-02} {AI05-0264-1} The procedure Set_Specific_Handler changes the specific handler for the task identified by T: if Handler is null, that specific handler is cleared; otherwise, it is set to be Handler.all. If a specific handler had previously been set it is replaced.
12.a/3
Ramification: {AI05-0005-1} This package cannot portably be used to set a handler on the program as a whole. It is possible to call Set_Specific_Handler with the environment task's ID. But any call to the handler would necessarily be a Bounded (Run-Time) Error, as the handler is called after the task's finalization has completed. In the case of the environment task, that includes any possible protected objects, and calling a protected object after it is finalized is a Bounded (Run-Time) Error (see 9.4). This might work in a particular implementation, but it cannot be depended upon. 
13/3
{AI95-00266-02} {AI05-0264-1} The function Specific_Handler returns the specific handler that is currently set for the task identified by T, if one is set; otherwise, it returns null.
14/2
{AI95-00266-02} As part of the finalization of a task_body, after performing the actions specified in 7.6 for finalization of a master, the specific handler for the task, if one is set, is executed. If the specific handler is cleared, a search for a fall-back handler proceeds by recursively following the master relationship for the task. If a task is found whose fall-back handler is set, that handler is executed; otherwise, no handler is executed.
15/2
{AI95-00266-02} For Set_Specific_Handler or Specific_Handler, Tasking_Error is raised if the task identified by T has already terminated. Program_Error is raised if the value of T is Ada.Task_Identification.Null_Task_Id.
16/2
{AI95-00266-02} An exception propagated from a handler that is invoked as part of the termination of a task has no effect.

Erroneous Execution

17/2
{AI95-00266-02} For a call of Set_Specific_Handler or Specific_Handler, if the task identified by T no longer exists, the execution of the program is erroneous. 

Extensions to Ada 95

17.a/2
{AI95-00266-02} Package Task_Termination is new. 

Wording Changes from Ada 2005

17.b/3
{AI05-0202-1} Correction: Specified what is passed to the handler if the finalization of the task fails after it is completed. This was not specified at all in Ada 2005, so there is a possibility that some program depended on some other behavior of an implementation. But as this case is very unlikely (and only occurs when there is already a significant bug in the program - so should not occur in fielded systems), we're not listing this as an inconsistency. 

Contents   Index   References   Search   Previous   Next 
Ada-Europe Ada 2005 and 2012 Editions sponsored in part by Ada-Europe