resource bounded_buffer
  import MPDanimator
  op deposit(int pid, real item ), fetch(int cid) returns real  item
body bounded_buffer(int size )
  real buf[0:size-1]
  int count = 0, take_out = 0, put_in = 0
  write("bounded buffer resource with", size, "slots is alive")
  reply

  while (true) {
    in deposit(pid, item) st count < size ->

# Move the producer to the slot.
      call A_stepjumpto(2000+pid, 7000+put_in, 5, 100)
# Change a full buffer slot's symbol to be solid black.
      call A_fill(put_in, "solid")

      buf[put_in] = item; reply; put_in = (put_in + 1) % size; count++
      write("                               count++ =", count)

# Move the producer back to its original position.
      call A_stepjumpto(2000+pid, 4000+pid, 5, 100)
# Change producer's symbol to an outline black circle.
      call A_fill(2000+pid, "outline")
      call A_color(2000+pid, "black")

    [] fetch(cid) returns item st count > 0 ->

# Move the consumer to the slot.
      call A_stepjumpto(3000+cid, 7000+take_out, 5, 100)
# Change an empty buffer slot's symbol to an outline black circle.
      call A_fill(take_out, "outline")

      item = buf[take_out]; reply; take_out = (take_out + 1) % size; count--
      write("                               count-- =", count)

# Move the consumer back to its original position.
      call A_stepjumpto(3000+cid, 5000+cid, 5, 100)
# Change consumers's symbol to an outline black circle.
      call A_fill(3000+cid, "outline")
      call A_color(3000+cid, "black")

    ni
  }
end bounded_buffer
