comp.lang.ada
 help / color / mirror / Atom feed
From: "Egil Høvik" <egilhovik@hotmail.com>
Subject: Re: Task with access to itself?
Date: Thu, 6 Dec 2012 01:38:41 -0800 (PST)
Date: 2012-12-06T01:38:41-08:00	[thread overview]
Message-ID: <93da19a9-acaf-4395-98d0-33baf5a04bd0@googlegroups.com> (raw)
In-Reply-To: <8738zk7gge.fsf@adaheads.sparre-andersen.dk>

On Wednesday, December 5, 2012 2:53:53 PM UTC+1, Jacob Sparre Andersen wrote:
> I would like to maintain a collection of tasks, where the tasks
> 
> themselves can register their availability.
> 
> 
> 
> My immediate thought was to let the tasks know their own address
> 
> ('Access), such that they can simply pass that to the collection of
> 
> available tasks, when they are ready.  But I haven't found a nice way to
> 
> implement that pattern, so may be I should do it a different way.
> 

Having the tasks grabbing jobs from a protected objects is probably a better idea, but this seems to work on GNAT Pro 7.0.2:

package foo is

   task type Handler is
      entry Do_Stuff;
   end Handler;
   
   type Reference is access all Handler;
   
   procedure Register_As_Available(P : access Handler);
   function Get_Next return Reference;

end foo;


with Ada.Task_Identification;
with Ada.Text_IO;

package body foo is

   task body Handler is
   begin
      Register_As_Available(Handler'Access); -- Handler here refers to the current instance of the type

      loop
         select 
            accept Do_Stuff do
               Ada.Text_IO.Put_Line("foo" & Ada.Task_Identification.Image(Handler'Identity));
            end Do_Stuff;
         or 
            terminate;
         end select;
      end loop;
   end Handler;


   List : array(1..2) of Reference;
   Last : Natural := 0;
   
   procedure Register_As_Available(P : access Handler)
   is
   begin
      Last := Last + 1;
      List(Last) := Reference(P);
   end Register_As_Available;


   Next : Natural := 0;
   function Get_Next return Reference is
   begin
      Next := Next + 1;
      if Next > List'Last then
         Next := 1;
      end if;
      return List(Next);
   end Get_Next;
end foo;


with foo;

procedure bar is

   t1 : foo.handler;
   t2 : foo.handler;

   Ref : foo.Reference;
begin

   for i in 1..10 loop
      Ref := foo.Get_Next;
      Ref.Do_Stuff;
   end loop;

end bar;


(If you're not careful, you may end up with a bunch of accessibility issues, though)

-- 
~egilhh



  parent reply	other threads:[~2012-12-06  9:38 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
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 [this message]
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