comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Task with access to itself?
Date: Wed, 5 Dec 2012 15:18:34 +0100
Date: 2012-12-05T15:18:34+01:00	[thread overview]
Message-ID: <dn1z5m7g6fb4$.wfyfnvnvqvph.dlg@40tude.net> (raw)
In-Reply-To: 8738zk7gge.fsf@adaheads.sparre-andersen.dk

On Wed, 05 Dec 2012 14:53:53 +0100, Jacob Sparre Andersen wrote:

> I would like to maintain a collection of tasks, where the tasks
> themselves can register their availability.
[...]
> Any suggestions?

Active object?

   type Active_Object is
      abstract new Ada.Finalization.Limited_Controlled with private;
   overriding procedure Initialize (Object : in out Active_Object);
   overriding procedure Finalize (Object : in out Active_Object);
   procedure Do_Stuff (Object : in out Active_Object) is abstract;
private
   task type Worker (Object : not null access Active_Object'Class) is
       entry Stop;
   end Worker;
   type Active_Object is
      abstract new Ada.Finalization.Limited_Controlled with
   record
       Handler : not null access Worker :=
          new Worker (Active_Object'Access);
   end record;
-------------------------------------------------------------------------------------------
   procedure Initialize (Object : in out Active_Object) is
   begin
        -- Register Object or else its task
   end Initialize;

   procedure Finalize (Object : in out Active_Object) is
       type Worker_Ptr is access all Worker;
       Ptr : Worker_Ptr;
       procedure Free is
           new Ada.Unchecked_Deallocation (Worker, Worker_Ptr)
   begin
       -- Unregister

       Object.Handler.Stop;
       while not Object.Handler'Terminated loop
           delay 0.001;
       end loop;
       Ptr := Object.Handler.all'Unchecked_Access;
       Free (Ptr);
   end Finalize;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2012-12-05 14:19 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-05 13:53 Task with access to itself? Jacob Sparre Andersen
2012-12-05 14:18 ` Dmitry A. Kazakov [this message]
2012-12-05 16:57 ` Jeffrey Carter
2012-12-11 11:21   ` Jacob Sparre Andersen
2012-12-11 20:39     ` Jeffrey Carter
2012-12-12 20:25       ` Jacob Sparre Andersen
2012-12-12 21:11         ` Jeffrey Carter
2012-12-13  7:20           ` Jacob Sparre Andersen
2012-12-06  9:38 ` Egil Høvik
2012-12-06 19:53   ` Adam Beneschan
replies disabled

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