comp.lang.ada
 help / color / mirror / Atom feed
From: stefan-lucks@see-the.signature
Subject: Re: [Ravenscar] run tasks on events
Date: Fri, 30 May 2008 13:59:58 +0200
Date: 2008-05-30T13:59:58+02:00	[thread overview]
Message-ID: <Pine.LNX.4.64.0805301334420.31551@medsec1.medien.uni-weimar.de> (raw)
In-Reply-To: <483fdadf$0$2672$4f793bc4@news.tdc.fi>

On Fri, 30 May 2008, Niklas Holsti wrote:

> In unrestricted Ada, if an exception occurs in a task, and the task does not
> handle the exception, the task by default terminates silently when the
> exception propagates out of the task. If you want to know about exceptions in
> a task, you should put a last-chance, catch-all exception handler in the task
> body ("exception when others => ...").

You don't need this in Ada 05 any more. You can define some callback 
procedures, to do something in the case of unhandled exceptions (and 
normal and abnormal termination as well, if you want to). 


The following is similar to an example in Barnes' book, except that the 
usage avoids race conditions:

--------------------------------------------------------------------------

Package defined in 
<http://www.adaic.org/standards/05rm/html/RM-C-7-3.html>:

with Ada.Task_Identification; with Ada.Exceptions;

package Ada.Task_Termination is
  ...
  type Cause_Of_Termination is (Normal, Abnormal, Unhandled_Exception);
  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);

  procedure Set_Dependents_Fallback_Handler(Handler: in Termination_Handler);
  ...
end Ada.Task_Termination;

--------------------------------------------------------------------------

Specification of Callback:

protected End_Of is
  
  procedure World(C: Cause_Of_Termination;
                  T: Task_ID;
                  X: Exception_Occurrence);

end End_Of;

--------------------------------------------------------------------------

Implementation of Callback:

  procedure World(C: Cause_Of_Termination;
                  T: Task_ID;
                  X: Exception_Occurrence);

  begin
    case C is
      when Normal => 
        null;
      when Abnormal => 
        Put_Log("abnormal termination of Task" & Image(T));
      when Unhandled_Exception =>
        Put_Log("unhandled exception in Task" & Image(T));
        Put_Log(Exception_Information(X));
    end case;
  end World;

--------------------------------------------------------------------------

Usage of callback (typically in main program / environment task):

begin 
  Set_Dependents_Fallback_Handler(End_Of.World);
  declare -- any task you want
    A_Bunch_Of_Tasks: array (0 .. 1023) of Some_Task_Type;
    Alice, Bob: Some_Other_Task_Type;
  begin
    ...
    -- Whenever any of our tasks terminates normally or abnormally or 
    -- propagates an exception, End_Of.World is called. 
  end;
end;




So long

Stefan

-- 
------ Stefan Lucks   --  Bauhaus-University Weimar  --   Germany  ------
               Stefan dot Lucks at uni minus weimar dot de
------  I  love  the  taste  of  Cryptanalysis  in  the  morning!  ------




  parent reply	other threads:[~2008-05-30 11:59 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-28 19:14 [Ravenscar] run tasks on events Sebastian Hanigk
2008-05-28 23:47 ` jimmaureenrogers
2008-05-29 10:35   ` Sebastian Hanigk
2008-05-29  3:16 ` ahab
2008-05-29 10:31   ` Sebastian Hanigk
2008-05-29 18:56     ` Anh Vo
2008-05-30  6:56       ` Sebastian Hanigk
2008-05-30  7:26 ` [Ravenscar] " Niklas Holsti
2008-05-30  8:57   ` Sebastian Hanigk
2008-05-30 10:47     ` Niklas Holsti
2008-05-30 11:17       ` Sebastian Hanigk
2008-05-30 15:24         ` Alex R. Mosteo
2008-05-30 15:35           ` Ed Falis
2008-05-30 18:02             ` Sebastian Hanigk
2008-05-30 18:11               ` Ed Falis
2008-05-30 11:59       ` stefan-lucks [this message]
2008-05-30 12:17 ` jimmaureenrogers
2008-05-30 18:59   ` Sebastian Hanigk
2008-06-02 10:32     ` Alex R. Mosteo
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox