with Ada.Exceptions; use Ada.Exceptions;
with Ada.Task_Termination; use Ada.Task_Termination;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Deallocation; use Ada;
package body Dynamic_Tasks_Finalizer is
procedure Free_Task_w_Discriminant is new Unchecked_Deallocation (Dynamic_Task, Dynamic_Task_Ptr);
protected body Finalizer_Deallocator is
procedure Last_Wish (Cause : Cause_Of_Termination;
Id : Task_Id;
X : Exception_Occurrence) is
Ptr : Dynamic_Task_Ptr := Running_Tasks.Element (Key => Id);
begin
Running_Tasks.Delete (Key => Id);
Free_Task_w_Discriminant (Ptr);
case Cause is
when Normal => Put_Line ("Task " & Image (Id) & " came to a peaceful end.");
when Abnormal => Put_Line ("Somethings really nasty happend to task " & Image (Id));
when Unhandled_Exception => Put_Line ("Unhandled exception " & Exception_Name (X) & " in task " & Image (Id));
end case;
Put_Line ("Cleaned up dynamic task " & Image (Id));
end Last_Wish;
procedure Register (Ptr : Dynamic_Task_Ptr) is
Task_Key : constant Task_Id := Ptr'Identity;
begin
Running_Tasks.Insert (Key => Task_Key, New_Item => Ptr);
Set_Specific_Handler (T => Task_Key, Handler => Last_Wish'Access);
end Register;
end Finalizer_Deallocator;
end Dynamic_Tasks_Finalizer;