Permalink
Browse files

Got rid of the Chew procedure and replaced Child_Id with Task_Id.

  • Loading branch information...
1 parent 65890d2 commit 80399dd58207977e698620a80ca5b00ee59b57ed Thomas Løcke committed Jul 14, 2012
Showing with 25 additions and 60 deletions.
  1. +17 −34 concurrent/concurrent.adb
  2. +8 −26 sequential/sequential.adb
@@ -10,41 +10,26 @@ procedure Concurrent is
protected Pez_Dispenser is
procedure Pop
- (Result : out Reward);
+ (Result : out Reward;
+ Task_Id : in String);
-- Get one of those delicious candies!
private
Available_Candies : Natural := 20;
end Pez_Dispenser;
- procedure Chew;
- -- Hiding away the fact that we just need to waste some time to get the
- -- point of this program across.
-
- ------------
- -- Chew --
- ------------
-
- procedure Chew
- is
- Child_Id : constant String := Image (Current_Task);
- begin
- Put_Line (Child_Id & " is chewing on a candy.");
- delay 1.0; -- This is some fast chewing indeed!
- end Chew;
-
---------------------
-- Pez_Dispenser --
---------------------
protected body Pez_Dispenser is
- -----------
- -- Pop --
- -----------
+ -----------
+ -- Pop --
+ -----------
procedure Pop
- (Result : out Reward)
+ (Result : out Reward;
+ Task_Id : in String)
is
- Child_Id : constant String := Image (Current_Task);
begin
case Available_Candies is
when 0 =>
@@ -54,9 +39,9 @@ procedure Concurrent is
Available_Candies := Available_Candies - 1;
- Put_Line (Child_Id & " pops a candy from the dispenser.");
- Put_Line (Natural'Image (Available_Candies) &
- " left in the dispenser");
+ Put_Line (Task_Id & " pops a candy from the dispenser.");
+ Put_Line (Natural'Image (Available_Candies)
+ & " left in the dispenser");
end case;
end Pop;
end Pez_Dispenser;
@@ -72,21 +57,19 @@ procedure Concurrent is
task body Child
is
- Child_Id : constant String := Image (Current_Task);
- Result : Reward;
+ Task_Id : constant String := Image (Current_Task);
+ Result : Reward;
begin
- A_Child_Popping_Pez :
- loop
- Pez_Dispenser.Pop (Result);
+ A_Child_Popping_Pez : loop
+ Pez_Dispenser.Pop (Result, Task_Id);
exit A_Child_Popping_Pez when Result = No_Candy;
- Chew;
+ delay 1.0;
+ -- This simulates chewing. Yes, a 1 second chew is pretty darn fast!
end loop A_Child_Popping_Pez;
- Put_Line ("No more candy! "
- & Child_Id
- & " runs out to play.");
+ Put_Line ("No more candy! " & Task_Id & " runs out to play.");
end Child;
Alice : Child;
@@ -6,7 +6,7 @@ procedure Sequential is
use Ada.Task_Identification;
use Ada.Text_IO;
- Child_Id : constant String := Image (Current_Task);
+ Task_Id : constant String := Image (Current_Task);
type Reward is (Candy, No_Candy);
@@ -15,26 +15,11 @@ procedure Sequential is
Available_Candies : Natural := 20;
end record;
- procedure Chew;
- -- Hiding away the fact that we just need to waste some time to get the
- -- point of this program across.
-
function Pop
(D : in out Dispenser)
return Reward;
-- Get one of those delicious candies!
- ------------
- -- Chew --
- ------------
-
- procedure Chew
- is
- begin
- Put_Line (Child_Id & " is chewing on a candy.");
- delay 1.0; -- This is some fast chewing indeed!
- end Chew;
-
-----------
-- Pop --
-----------
@@ -49,10 +34,9 @@ procedure Sequential is
return No_Candy;
when others =>
D.Available_Candies := D.Available_Candies - 1;
- Put_Line (Child_Id &
- " pops a candy from the dispenser.");
- Put_Line (Natural'Image (D.Available_Candies) &
- " left in the dispenser");
+ Put_Line (Task_Id & " pops a candy from the dispenser.");
+ Put_Line (Natural'Image (D.Available_Candies)
+ & " left in the dispenser");
return Candy;
end case;
end Pop;
@@ -62,17 +46,15 @@ procedure Sequential is
begin
- A_Child_Popping_Pez :
- loop
+ A_Child_Popping_Pez : loop
Result := Pop (Pez_Dispenser);
exit A_Child_Popping_Pez when Result = No_Candy;
- Chew;
+ delay 1.0;
+ -- This simulates chewing. Yes, a 1 second chew is pretty darn fast!
end loop A_Child_Popping_Pez;
- Put_Line ("No more candy! "
- & Child_Id
- & " runs out to play.");
+ Put_Line ("No more candy! " & Task_Id & " runs out to play.");
end Sequential;

0 comments on commit 80399dd

Please sign in to comment.