/* 
 * Semaphore.java (Implements Semaphore Syhcnronization Object).
 *
 * Written by : Nitin  Motgi (nmotgi@cs.ucf.edu)
 *
 * This file implements semaphore which shared counters within blocking
 * semantics. A semaphore is a non-negative integer on which 2 operations
 * are defined:
 * 1. P() decrements the semaphore by one. If the semaphore is zero
 *    the process calling P() is blocked until semaphore is positive
 *    again.
 * 2. V() increments semaphore by one.
 *
 * The above two operations are atomic. A process sees all or none
 * P() or V(), and no P() or V() is ever lost. These processes
 * are generally invoked by processes using system calls.
 *
 * Portions copyright(c) 2000 to School of Electrical Engineering and 
 * Computer Science, UCF, Orlando.
 *
 * Use and distribution of this source code are strictly governed by 
 * terms and conditions set by the authors.
 * 
 * $Id : Semaphore.java, v1.0.1 02/07/2001. $
 *
 * Revision History:
 *
 * 1. Downloaded from Internet.       Nitin,        v1.0.0  02/07/2001.
 * 2. Final Variable Name Check.      Group,        v1.0.1  02/07/2001.
 * 3. Final Documentation Check.      Group,        v1.0.1  02/07/2001.
*/

/* Start of Semaphore.*/
public class Semaphore {
  private int nCount;
    
  /* Initialise Semaphore.*/
  public Semaphore (int nCount) {
    this.nCount = nCount;
  }

  /* Lock the data.*/
  public synchronized void P () {
    while (nCount <= 0){
      try {
	wait();
      } catch(Exception e) {
	 e.printStackTrace();
	}
    }
    nCount--;
  }/* End of P.*/
  
  /* Releases the data.*/
  public synchronized void V () {
    ++nCount;
    notify();
  }/* End of V.*/

}/* End of Semaphore.*/

