home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / misc / ada1110b.lha / Examples / Dine / philbody.ada < prev    next >
Encoding:
Text File  |  1992-03-02  |  1.5 KB  |  59 lines

  1. WITH Room;
  2. WITH Random;
  3. PACKAGE BODY Phil IS
  4.   
  5. -- Body of philosopher task. 
  6.  
  7. -- The philosopher needs to "with" package Room in order to report its states.
  8. -- Package Random is used to generate a (pseudo-)random-length meal and
  9. -- thinking time. This produces very useful nondeterminism.
  10.  
  11. -- Michael B. Feldman, The George Washington University, November 1990.
  12.  
  13.   TASK BODY Philosopher IS
  14.  
  15.     Who_Am_I   : Positive;
  16.     First_Grab : Positive;
  17.     Second_Grab: Positive;
  18.     Meal_Time  : Natural;
  19.     Think_Time : Natural;
  20.     
  21.   BEGIN
  22.     ACCEPT Come_To_Life (My_ID :     Positive; 
  23.                         Chopstick1 : Positive;
  24.                         Chopstick2 : Positive) DO
  25.       Who_Am_I    := My_ID;
  26.       First_Grab  := Chopstick1;
  27.       Second_Grab := Chopstick2;
  28.  
  29.     END Come_To_Life;
  30.  
  31.     Room.Head_Waiter.Report_State(Who_Am_I, Breathing);
  32.  
  33.     LOOP
  34.  
  35.       Room.Sticks(First_Grab).Pick_Up;
  36.       Room.Head_Waiter.Report_State(Who_Am_I, Got_One_Stick, First_Grab);
  37.  
  38.       Room.Sticks(Second_Grab).Pick_Up;
  39.       Room.Head_Waiter.Report_State(Who_Am_I, Got_Other_Stick, Second_Grab);
  40.  
  41.       Meal_Time := Random.Random_Int(10);
  42.       Room.Head_Waiter.Report_State(Who_Am_I, Eating, Meal_Time);
  43.  
  44.       DELAY Duration(Meal_Time);
  45.       Room.Head_Waiter.Report_State(Who_Am_I, Done_Eating);
  46.  
  47.       Room.Sticks(First_Grab).Put_Down;
  48.       Room.Sticks(Second_Grab).Put_Down;
  49.  
  50.       Think_Time := Random.Random_Int(10);
  51.       Room.Head_Waiter.Report_State(Who_Am_I, Thinking, Think_Time);
  52.       DELAY Duration(Think_Time);
  53.  
  54.     END LOOP;
  55.  
  56.   END Philosopher;
  57.  
  58. END Phil;
  59.