-- test_prot_83.adb with Text_IO; procedure Test_Prot_83 is -- like Ada 95 protected Buffer -- FIFO on floating point numbers F1, F2, F3 : Float; -- just temporaries Size : constant := 100; -- buffer size subtype Indices is natural range 0..Size-1; -- modular type in Ada 95 type Pool_Type is array(Indices) of Float; task Buffer is entry Read(C : out Float); -- mutually exclusive because of select entry Write(C : in Float); end Buffer; task body Buffer is Pool : Pool_Type; -- the protected data items Count : Natural := 0; In_Index, Out_Index : Indices := 0; begin loop select when Count < Pool'Length => -- guard holds up writer when full accept Write(C : in Float) do Pool(In_Index) := C; In_Index := In_Index mod Pool'Length; Count := Count + 1; end; or when Count > 0 => -- guard holds up reader when empty accept Read(C : out Float) do C := Pool(Out_Index); Out_Index := Out_Index mod Pool'Length; Count := Count - 1; end; or terminate; end select; end loop; end Buffer; begin Buffer.Write(1.5); -- these calls would normally be in tasks Buffer.Read(F1); -- here in main program, must avoid deadlock Buffer.Write(2.6); Buffer.Write(3.7); Buffer.Read(F2); Buffer.Read(F3); Text_io.Put_line("Write and Read gives " & Integer'Image(Integer(F1)) & " " & Integer'Image(Integer(F2)) & " " & Integer'Image(Integer(F3))); for I in 1..250 loop Buffer.Write(Float(I)); -- loop through buffer several times Buffer.Read(F1); end loop; Buffer.Write(4.8); Buffer.Read(F1); Text_io.Put_line("Write and Read gives " & Integer'Image(Integer(F1))); end Test_Prot_83;