home *** CD-ROM | disk | FTP | other *** search
- -- ++
- -- A generic protected buffer.
- -- Provide buffering of Max items between users. The package is implemented as
- -- a task to provide protection for multiple tasks calling the Get and Put
- -- routines simultaneously.
- -- --
-
- generic
- Max : Positive;
- type Item is private;
- package Buffer_G_P is
-
- task Protected_Buffer is
- entry Put ( The_Item : in Item );
- entry Get ( The_Item : out Item );
- end Protected_Buffer;
-
- end Buffer_G_P;
-
- package body Buffer_G_P is
-
- task body Protected_Buffer is
- subtype Buffer_Range is Positive range 1 .. Max;
- Buffer : array ( Buffer_Range ) of Item;
- Free : Buffer_Range := 1;
- Used : Buffer_Range := 1;
- Count : Natural range 0 .. Max := 0;
- begin
- loop
- select
- when Count < Max =>
- accept Put ( The_Item : in Item ) do
- Buffer ( Free ) := The_Item;
- end Put;
- Free := Free mod Max + 1;
- Count := Count + 1;
- or
- when Count > 0 =>
- accept Get ( The_Item : out Item ) do
- The_Item := Buffer ( Used );
- end Get;
- Used := Used mod Max + 1;
- Count := Count - 1;
- end select;
- end loop;
- end Protected_Buffer;
-
- end Buffer_G_P;
-