home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnat / examples / room.adb < prev    next >
Text File  |  2000-07-19  |  5KB  |  148 lines

  1. --::::::::::
  2. --room.adb
  3. --::::::::::
  4. with Windows;
  5. with Chop;
  6. with Phil;
  7. with Society;
  8. with Calendar;
  9. pragma Elaborate (Phil);
  10. package body Room is
  11.  
  12.   -- Dining Philosophers, Ada 95 edition
  13.   -- A line-oriented version of the Room package
  14.   -- Michael B. Feldman, The George Washington University, 
  15.   -- July, 1995.
  16.  
  17.   -- philosophers sign into dining room, giving Maitre_D their DNA code
  18.  
  19.   Dijkstra  : aliased Phil.Philosopher (My_ID => 1);
  20.   Stroustrup: aliased Phil.Philosopher (My_ID => 2);
  21.   Anderson  : aliased Phil.Philosopher (My_ID => 3);
  22.   Ichbiah   : aliased Phil.Philosopher (My_ID => 4);
  23.   Taft      : aliased Phil.Philosopher (My_ID => 5);
  24.  
  25.   type Philosopher_Ptr is access all Phil.Philosopher;
  26.  
  27.   Phils : array (Table_Type) of Philosopher_Ptr;
  28.   Phil_Windows : array(Table_Type) of Windows.Window;
  29.   Phil_Seats : array (Society.Unique_DNA_Codes) of Table_Type;
  30.  
  31.   task body Maitre_D is
  32.  
  33.     T          : Natural;
  34.     Start_Time : Calendar.Time;
  35.     Blanks : constant String := "     ";
  36.  
  37.   begin
  38.  
  39.     accept Start_Serving;
  40.  
  41.     Start_Time := Calendar.Clock;
  42.  
  43.     -- now Maitre_D assigns phils to seats at the table
  44.  
  45.     Phils :=
  46.       (Dijkstra'Access,
  47.        Anderson'Access,
  48.        Taft'Access,
  49.        Ichbiah'Access,
  50.        Stroustrup'Access);
  51.   
  52.     Phil_Seats   := (1, 3, 5, 4, 2);   -- which seat each phil occupies
  53.  
  54.     Phil_Windows :=
  55.       (Windows.Open (( 1, 24), 7, 30),
  56.        Windows.Open (( 9,  2), 7, 30),
  57.        Windows.Open (( 9, 46), 7, 30),
  58.        Windows.Open ((17,  7), 7, 30),
  59.        Windows.Open ((17, 41), 7, 30));
  60.  
  61.  
  62.     for Which_Window in Phil_Windows'range loop
  63.       Windows.Borders (Phil_Windows(Which_Window), '+', '|', '-');
  64.     end loop;
  65.  
  66.     -- and assigns them their chopsticks.
  67.  
  68.     Phils (1).Start_Eating (1, 2);
  69.     Phils (3).Start_Eating (3, 4);
  70.     Phils (2).Start_Eating (2, 3);
  71.     Phils (5).Start_Eating (1, 5);
  72.     Phils (4).Start_Eating (4, 5);
  73.  
  74.     loop
  75.       select
  76.         accept Report_State (Which_Phil : in Society.Unique_DNA_Codes;
  77.                              State      : in Phil.States;
  78.                              How_Long   : in Natural := 0;
  79.                              Which_Meal : in Natural := 0) do
  80.  
  81.           T := Natural (Calendar."-" (Calendar.Clock, Start_Time));
  82.  
  83.           case State is
  84.  
  85.             when Phil.Breathing =>
  86.               Windows.Title(Phil_Windows(Phil_Seats(Which_Phil)),
  87.                             Society.Name_Register(Which_Phil), '-');
  88.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  89.                      "T =" & Integer'Image (T) & " " 
  90.                       & "Breathing...");
  91.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  92.  
  93.             when Phil.Thinking =>
  94.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  95.                      "T =" & Integer'Image (T) & " " 
  96.                       & "Thinking" 
  97.                       & Integer'Image (How_Long) & " seconds.");
  98.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  99.  
  100.             when Phil.Eating =>
  101.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  102.                      "T =" & Integer'Image (T) & " " 
  103.                       & "Meal"  
  104.                       & Integer'Image (Which_Meal)
  105.                       & ","  
  106.                       & Integer'Image (How_Long) & " seconds.");
  107.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  108.  
  109.             when Phil.Done_Eating =>
  110.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  111.                      "T =" & Integer'Image (T) & " " 
  112.                       & "Yum-yum (burp)");
  113.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  114.  
  115.             when Phil.Got_One_Stick =>
  116.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  117.                      "T =" & Integer'Image (T) & " " 
  118.                       & "First chopstick" 
  119.                       & Integer'Image (How_Long));
  120.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  121.  
  122.             when Phil.Got_Other_Stick =>
  123.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  124.                      "T =" & Integer'Image (T) & " " 
  125.                       & "Second chopstick" 
  126.                       & Integer'Image (How_Long));
  127.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  128.  
  129.             when Phil.Dying =>
  130.               Windows.Put (Phil_Windows(Phil_Seats(Which_Phil)),
  131.                      "T =" & Integer'Image (T) & " " 
  132.                       & "Croak");
  133.               Windows.New_Line (Phil_Windows(Phil_Seats(Which_Phil)));
  134.  
  135.           end case; -- State
  136.           
  137.         end Report_State;
  138.  
  139.       or
  140.         terminate;
  141.       end select;
  142.  
  143.     end loop;
  144.  
  145.   end Maitre_D;
  146.  
  147. end Room;
  148.