Dining Philosophers with Dictionary (3)



package dpd is

task phil_1 is
   entry dictionary;
end phil_1;

task phil_2 is
   entry dictionary;
end phil_2;

task phil_3 is
   entry dictionary;
end phil_3;

task fork_1 is
   entry up;
   entry down;
end fork_1;

task fork_2 is
   entry up;
   entry down;
end fork_2;

task fork_3 is
   entry up;
   entry down;
end fork_3;

end dpd;


package body dpd is

task body phil_1 is
   done : Boolean;
begin

   -- pass the dictionary to phil_2
   phil_2.dictionary;
   loop
      -- either eat or accept the dictionary
      -- this is a little tricky, but we can do it with an else in the select
      select
         fork_2.up;  
         fork_1.up;     --event[{phil_1_eating}]
         fork_2.down;   --eventb[{phil_1_not_eating}]
         fork_1.down;
      else
         accept dictionary;
         phil_2.dictionary;
      end select;

      exit when done;
   end loop;
end phil_1;

task body phil_2 is
   done : Boolean;
begin
   loop
      -- either eat or accept the dictionary
      -- this is a little tricky, but we can do it with an else in the accept
      select
         fork_3.up;
         fork_2.up;     --event[{phil_2_eating}]
         fork_3.down;   --eventb[{phil_2_not_eating}]
         fork_2.down;
      else
         accept dictionary;
         phil_3.dictionary;
      end select;

      exit when done;
   end loop;
end phil_2;

task body phil_3 is
   done : Boolean;
begin
   loop
      -- either eat or accept the dictionary
      -- this is a little tricky, but we can do it with an else in the accept
      select
         fork_1.up;
         fork_3.up;
         fork_1.down;
         fork_3.down;
      else
         accept dictionary;
         phil_1.dictionary;
      end select;

      exit when done;
   end loop;
end phil_3;

task body fork_1 is
   done : Boolean;
begin
   loop
      accept up;
      accept down;
      exit when done;
   end loop;
end fork_1;

task body fork_2 is
   done : Boolean;
begin
   loop
      accept up;
      accept down;
      exit when done;
   end loop;
end fork_2;

task body fork_3 is
   done : Boolean;
begin
   loop
      accept up;
      accept down;
      exit when done;
   end loop;
end fork_3;

end dpd;