comp.lang.ada
 help / color / mirror / Atom feed
* Protected type defined in the procedure
@ 2005-09-28  8:10 Maciej Sobczak
  2005-09-28  8:53 ` Egil H. H�vik
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Maciej Sobczak @ 2005-09-28  8:10 UTC (permalink / raw)


Hi,

While learning Ada I have found the following problem.
It is possible to define a task within some procedure, like here:

procedure Hello is

     task SomeTask is
         -- ... some entries
     end SomeTask;

     task body SomeTask is
     begin
         -- ... (2)
     end SomeTask;

begin
     -- ... (1)
end Hello;

At some point I decided to use a protected object to act as a shared 
data between (1) and (2). I was unable to find any way to define 
appropriate protected type within the Hello procedure. Separate package 
helped with this, but the question remains: is it at all possible to 
define a protected type (*all* of it, including implementation of its 
procedures, functions and entries) within the procedure where it is 
supposed to be used? I find it to be reasonable, especially when 
compared with the possibility of defining the whole task this way. In 
fact, the protected object is not going to be used outside of the 
Hello+SomeTask combo, so there is no need to pollute the project with 
separate packages just for things like this.


-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Protected type defined in the procedure
  2005-09-28  8:10 Protected type defined in the procedure Maciej Sobczak
@ 2005-09-28  8:53 ` Egil H. H�vik
  2005-09-28  9:18 ` Martin Dowie
  2005-09-28  9:35 ` Maciej Sobczak
  2 siblings, 0 replies; 4+ messages in thread
From: Egil H. H�vik @ 2005-09-28  8:53 UTC (permalink / raw)


Will this do:

with Ada.Text_IO;

procedure protected_type is

   protected foo is
      entry wait;
      procedure release;
   private
      continue : boolean := false;
   end foo;

   protected body foo is

      entry wait when continue is
      begin
         continue := false;
      end wait;

      procedure release is
      begin
         continue := true;
      end release;
   end foo;


   task bar;

   task body bar is
   begin
      loop
         foo.release;
         delay 1.0;
      end loop;
   end bar;

begin

   loop
      select
         foo.wait;
         Ada.Text_IO.Put_Line("released");
      or
         delay 0.5;
         Ada.Text_IO.Put_Line("waiting...");
      end select;
   end loop;
end protected_type;


~egilhh



"Maciej Sobczak" <no.spam@no.spam.com> wrote in message
news:dhdj5p$o6r$1@sunnews.cern.ch...
> Hi,
>
> While learning Ada I have found the following problem.
> It is possible to define a task within some procedure, like here:
>
> procedure Hello is
>
>      task SomeTask is
>          -- ... some entries
>      end SomeTask;
>
>      task body SomeTask is
>      begin
>          -- ... (2)
>      end SomeTask;
>
> begin
>      -- ... (1)
> end Hello;
>
> At some point I decided to use a protected object to act as a shared
> data between (1) and (2). I was unable to find any way to define
> appropriate protected type within the Hello procedure. Separate package
> helped with this, but the question remains: is it at all possible to
> define a protected type (*all* of it, including implementation of its
> procedures, functions and entries) within the procedure where it is
> supposed to be used? I find it to be reasonable, especially when
> compared with the possibility of defining the whole task this way. In
> fact, the protected object is not going to be used outside of the
> Hello+SomeTask combo, so there is no need to pollute the project with
> separate packages just for things like this.
>
>
> --
> Maciej Sobczak : http://www.msobczak.com/
> Programming    : http://www.msobczak.com/prog/





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Protected type defined in the procedure
  2005-09-28  8:10 Protected type defined in the procedure Maciej Sobczak
  2005-09-28  8:53 ` Egil H. H�vik
@ 2005-09-28  9:18 ` Martin Dowie
  2005-09-28  9:35 ` Maciej Sobczak
  2 siblings, 0 replies; 4+ messages in thread
From: Martin Dowie @ 2005-09-28  9:18 UTC (permalink / raw)


Yes, you can, e.g.:

with Ada.Text_IO; use Ada.Text_IO;

procedure Test_Prot_Task_Comms is
   protected type A_Resource is
      entry Seize;
      procedure Release;
   private
      Busy : Boolean := False;
   end A_Resource;
   protected body A_Resource is
      entry Seize when not Busy is
      begin
         Busy := True;
      end Seize;
      procedure Release is
      begin
         Busy := False;
      end Release;
   end A_Resource;
   This_Resource : A_Resource;
   task Foo;
   task body Foo is
   begin
      loop
         This_Resource.Seize;
         Put_Line ("Task");
         This_Resource.Release;
         delay 1.0;
      end loop;
   end Foo;
begin
   delay 1.0;
   loop
      This_Resource.Seize;
      Put_Line ("Main");
      This_Resource.Release;
      delay 3.0;
   end loop;
end Test_Prot_Task_Comms;





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Protected type defined in the procedure
  2005-09-28  8:10 Protected type defined in the procedure Maciej Sobczak
  2005-09-28  8:53 ` Egil H. H�vik
  2005-09-28  9:18 ` Martin Dowie
@ 2005-09-28  9:35 ` Maciej Sobczak
  2 siblings, 0 replies; 4+ messages in thread
From: Maciej Sobczak @ 2005-09-28  9:35 UTC (permalink / raw)



Egil, Martin,

Indeed that works.
Now when I look at it I wonder how I managed to miss this obvious 
solution. :)

Thanks,


-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-09-28  9:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-28  8:10 Protected type defined in the procedure Maciej Sobczak
2005-09-28  8:53 ` Egil H. H�vik
2005-09-28  9:18 ` Martin Dowie
2005-09-28  9:35 ` Maciej Sobczak

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