Asynchronous communication between protected types 
Author Message
 Asynchronous communication between protected types

I'm trying to connect concurrent objects via an asynchronous
message. I was hoping to do this entirely with protected objects,
however, cannot seem to do so. The difficulty I have is that even if I
put the receiver into a loop (waiting on a send entry) it seems that
*some* task has to start the receiver, so I will still need one (Ada)
task per (active) receiver.

About the best I can come up is the following code.
This is acceptable for my purposes but I was curious if I
could trick this into working using only protected types.
(and without the distribution annex's Asynchronous Pragma
either :-)

(some code snippets borrowed from _Concurrency in Ada (95)_)
-----------------------------------------------------------

with Ada.Text_IO;
procedure Test_Persistent is

   type Messages is (Run, Die);

   protected type Persistent_Signals is
      procedure Send (Message: Messages);
      entry Wait (Message: out Messages);
   private
      Message: Messages;
      Signal_Arrived: Boolean:=False;
   end Persistent_Signals;

   protected body Persistent_Signals is

      procedure Send (Message: Messages) is
      begin
         Persistent_Signals.Message:=Message;
         Signal_Arrived:=True;
      end Send;

      entry Wait (Message: out messages) when Signal_Arrived is
      begin
         Signal_Arrived:=False;
         Message:=Persistent_Signals.Message;
      end Wait;

   end Persistent_Signals;

   type Access_To_Persistent_Signals is access Persistent_Signals;

   task type Processes is
      entry Initialize (With_Signal: Access_To_Persistent_Signals);
   end Processes;

   task body Processes is
      Message: Messages;
      Signal: Access_To_Persistent_Signals;

   begin
      accept Initialize (With_Signal: Access_To_Persistent_Signals) do
         Signal:=With_Signal;
      end Initialize;
      loop
         Signal.Wait (Message);
         case Message is
            when Run =>
               Ada.Text_IO.Put_Line ("I'm running");
            when Die =>
               exit;
         end case;
      end loop;
   end Processes;

   Process: Processes;
   Signal: Access_To_Persistent_Signals;

begin
   Signal:=new Persistent_Signals;
   Process.Initialize (Signal);
   for I in 1 .. 4 loop
      Signal.Send (Run);
      delay 0.5;
   end loop;
   Signal.Send (Die);
end Test_Persistent;

--



Sat, 06 Jan 2001 03:00:00 GMT  
 Asynchronous communication between protected types


Quote:
> I'm trying to connect concurrent objects via an asynchronous
> message. I was hoping to do this entirely with protected objects,
> however, cannot seem to do so. The difficulty I have is that even if I
> put the receiver into a loop (waiting on a send entry) it seems that
> *some* task has to start the receiver, so I will still need one (Ada)
> task per (active) receiver.
> ...

I assume that by "start the receiver", you mean call the initialize
entry. Ada allows you to avoid that:

with Ada.Text_IO;
procedure Test_Persistent is
   type Messages is (Run, Die);

   protected type Persistent_Signals is
      procedure Send (Message: in Messages);
      entry Wait (Message: out Messages);
   private
      Message: Messages;
      Signal_Arrived: Boolean:=False;
   end Persistent_Signals;

   protected body Persistent_Signals is
      procedure Send (Message: in Messages) is
      begin
         Persistent_Signals.Message:=Message;
         Signal_Arrived:=True;
      end Send;

      entry Wait (Message: out messages) when Signal_Arrived is
      begin
         Signal_Arrived:=False;
         Message:=Persistent_Signals.Message;
      end Wait;
   end Persistent_Signals;

   task type Processes (Signal : access Persistent_Signals);

   task body Processes is
      Message: Messages;
   begin
      loop
         Signal.Wait (Message => Message);
         case Message is
            when Run =>
               Ada.Text_IO.Put_Line ("I'm running");
            when Die =>
               exit;
         end case;
      end loop;
   end Processes;

   Signal : aliased Persistent_Signals;
   Process: Processes (Signal => Signal'access);
begin
   for I in 1 .. 4 loop
      Signal.Send (Message => Run);
      delay 0.5;
   end loop;

   Signal.Send (Message => Die);
end Test_Persistent;

This compiles and runs fine on GNAT 3.10p1/Win95.

Jeff Carter  PGP:1024/440FBE21

"I fart in your general direction."
Monty python & the Holy Grail

Posted with Spam Hater - see
http://www.compulink.co.uk/~net-services/spam/



Sun, 07 Jan 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Asynchronous Serial Communications

2. Asynchronous Communication Events

3. Asynchronous communication

4. Asynchronous Communications (RS232) under Unix

5. Asynchronous device communications

6. Asynchronous Inter Process Communication in a Prolog debugger

7. ILPS 91 Post Conference Workshop on Semantics of Asynchronous Communication

8. Corrections, Semantics of Asynchronous Communication, ILPS 91

9. Workshop on Semantics of Asynchronous Communication

10. Two-Way Task Communication In Ada (w/o Protected Objects)

11. Unsupported record type - Sona Communications

12. JGNAT and Tasking/Protected Types Problem

 

 
Powered by phpBB® Forum Software