home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / ada_1 / Examples_ada_buffer_g_p < prev    next >
Encoding:
Text File  |  1994-08-14  |  1.2 KB  |  49 lines

  1. -- ++
  2. -- A generic protected buffer.
  3. -- Provide buffering of Max items between users. The package is implemented as
  4. -- a task to provide protection for multiple tasks calling the Get and Put
  5. -- routines simultaneously.
  6. -- --
  7.  
  8. generic
  9.    Max : Positive;
  10.    type Item is private;
  11. package Buffer_G_P is
  12.  
  13. task Protected_Buffer is
  14.    entry Put ( The_Item : in  Item );
  15.    entry Get ( The_Item : out Item );
  16. end Protected_Buffer;
  17.  
  18. end Buffer_G_P;
  19.  
  20. package body Buffer_G_P is
  21.  
  22. task body Protected_Buffer is
  23.    subtype Buffer_Range is Positive range 1 .. Max;
  24.    Buffer : array ( Buffer_Range ) of Item;
  25.    Free   : Buffer_Range := 1;
  26.    Used   : Buffer_Range := 1;
  27.    Count  : Natural range 0 .. Max := 0;
  28. begin
  29.    loop
  30.       select
  31.          when Count < Max =>
  32.             accept Put ( The_Item : in Item ) do
  33.                Buffer ( Free ) := The_Item;
  34.             end Put;
  35.          Free := Free mod Max + 1;
  36.          Count := Count + 1;
  37.       or
  38.          when Count > 0 =>
  39.             accept Get ( The_Item : out Item ) do
  40.                The_Item := Buffer ( Used );
  41.             end Get;
  42.          Used := Used mod Max + 1;
  43.          Count := Count - 1;
  44.       end select;
  45.    end loop;
  46. end Protected_Buffer;
  47.  
  48. end Buffer_G_P;
  49.