1. with Ada.Exceptions;             use Ada.Exceptions; 
  2. with Ada.Task_Termination;       use Ada.Task_Termination; 
  3. with Ada.Text_IO;                use Ada.Text_IO; 
  4. with Ada.Unchecked_Deallocation; use Ada; 
  5.  
  6. package body Dynamic_Tasks_Finalizer is 
  7.  
  8.    procedure Free_Task_w_Discriminant is new Unchecked_Deallocation (Dynamic_Task, Dynamic_Task_Ptr); 
  9.  
  10.    protected body Finalizer_Deallocator is 
  11.  
  12.       procedure Last_Wish (Cause : Cause_Of_Termination; 
  13.                            Id    : Task_Id; 
  14.                            X     : Exception_Occurrence) is 
  15.  
  16.          Ptr : Dynamic_Task_Ptr := Running_Tasks.Element (Key => Id); 
  17.  
  18.       begin 
  19.          Running_Tasks.Delete (Key => Id); 
  20.          Free_Task_w_Discriminant (Ptr); 
  21.          case Cause is 
  22.             when Normal              => Put_Line ("Task " & Image (Id) & " came to a peaceful end."); 
  23.             when Abnormal            => Put_Line ("Somethings really nasty happend to task " & Image (Id)); 
  24.             when Unhandled_Exception => Put_Line ("Unhandled exception " & Exception_Name (X) & " in task " & Image (Id)); 
  25.          end case; 
  26.          Put_Line ("Cleaned up dynamic task " & Image (Id)); 
  27.       end Last_Wish; 
  28.  
  29.       procedure Register (Ptr : Dynamic_Task_Ptr) is 
  30.  
  31.          Task_Key : constant Task_Id := Ptr'Identity; 
  32.  
  33.       begin 
  34.          Running_Tasks.Insert (Key => Task_Key, New_Item => Ptr); 
  35.          Set_Specific_Handler (T   => Task_Key, Handler  => Last_Wish'Access); 
  36.       end Register; 
  37.  
  38.    end Finalizer_Deallocator; 
  39.  
  40. end Dynamic_Tasks_Finalizer;