Yurttas/PL/IL/Ada-95/F/06/p-c/producer consumer 02.adb

From ZCubes Wiki
Jump to navigation Jump to search
  1--
  2-- Copyright(C) 1998
  3-- All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
  4--
  5-- Permission to use, copy, modify, and distribute this
  6-- software and its documentation for EDUCATIONAL purposes
  7-- and without fee is hereby granted provided that this
  8-- copyright notice appears in all copies.
  9--
 10 
 11-- date   : January 1, 1998.
 12-- author : Salih Yurttas.
 13
 14-- producer_consumer_02.adb
 15
 16
 17with Ada.Text_IO;
 18use Ada.Text_IO;
 19
 20procedure Producer_Consumer_02 is
 21
 22  type Unit is new Integer;
 23
 24  task Ring_Buffer is
 25    entry G(Item : out Unit);
 26    entry P(Item : in Unit);
 27  end Ring_Buffer;
 28
 29  task body Ring_Buffer is
 30
 31    No_of_Units : constant := 16;
 32
 33    Buffers : array(1..No_of_Units) of Unit;
 34
 35    Full_Slots : Integer range 0..No_of_Units := 0;
 36
 37    Get_Pointer, 
 38    Put_Pointer  : Integer range 1..No_of_Units := 1;
 39
 40  begin
 41
 42    loop
 43
 44      if Full_Slots<No_of_Units then
 45        accept P(Item : in Unit) do
 46          Buffers(Put_Pointer) := Item;
 47        end P;
 48
 49        Put_Pointer := Put_Pointer mod No_of_Units+1;
 50
 51        Full_Slots := Full_Slots+1;
 52
 53	Put_Line("P");
 54      end if;
 55
 56      if Full_Slots>0 then
 57        accept G(Item : out Unit) do
 58          Item := Buffers(Get_Pointer);
 59        end G;
 60
 61        Get_Pointer := Get_Pointer mod No_of_Units+1;
 62
 63        Full_Slots := Full_Slots-1;
 64
 65	Put_Line("C");
 66      end if;
 67
 68    end loop;
 69
 70  end Ring_Buffer;
 71
 72  task Producer;
 73
 74  task body Producer is
 75    Item : Unit;
 76  begin
 77
 78    loop
 79      Item := 9;
 80      Ring_Buffer.P(Item);
 81    end loop;
 82
 83  end Producer;
 84
 85  task Consumer;
 86
 87  task body Consumer is
 88    Item : Unit;
 89  begin
 90
 91    loop
 92      Ring_Buffer.G(Item);
 93    end loop;
 94
 95  end Consumer;
 96
 97begin
 98
 99  null;
100
101end Producer_Consumer_02;