/********************************************************************
 * Uses Java monitor primitives to implement semaphore.
 *
 * $RCSfile: Semaphore.java,v $   
 * @version $Revision: 1.1 $ $Date: 2001-02-07 09:38:23-03:30 $
 * @author $Author: dpeters $
 *
 * $State: Exp $ 
 */
public class Semaphore {

  /** Current value of the semaphore */
  private int val; 

  /**
   * Constructor with initialization.
   *
   * @param i Initial value for semaphore (>= 0)
   */
  public Semaphore(int i)
  {
    val = i;
  }

  /**
   * Default constructor, initializes to 0.
   */
  public Semaphore()
  {
    val = 0;
  }

  /**
   * Releases the semaphore: &lt; <code>val = val + 1</code> &gt;
   */
  public synchronized void V()
  {
    val++;
    notifyAll();
  }

  /**
   * Acquires the semaphore: 
   * &lt; <code>await(val > 0) val = val - 1</code> &gt;
   * @throws InterruptedException
   */
  public synchronized void P()
    throws InterruptedException
  {
    while (val <= 0) wait();
    val--;
  }
  
}

/******************************************************************
 *                     REVISION HISTORY
 *
 * $Log: Semaphore.java,v $
 * Revision 1.1  2001-02-07 09:38:23-03:30  dpeters
 * Initial revision
 *
 *
 ******************************************************************/

