
package org.eclipse.birt.test.performance;

/**
 * Semaphore - utility class that allows threads to use "signal" to inform
 * thread monitor of their status
 */
public class Semaphore
{

	private int count;

	public Semaphore( int count )
	{
		this.count = count;
	}

	/**
	 * re-set semaphore counter
	 */
	public synchronized void reset( )
	{
		this.count = 0;
	}

	/**
	 * Acquire the semaphore, only one object can access it at one time ( i.e.
	 * mutual exclusion )
	 */

	/**
	 * increase semaphore counter
	 */
	public synchronized void acquire( )
	{
		count++;
		System.out.println( "Semephore acquired, count turn to: " + count );
	}

	/**
	 * holdup semaphore, i.e. "sleep signal"
	 */
	public synchronized void holdup( )
	{
		try
		{
			while ( count > 0 )
			{
				System.out.println( "Sleeping.... on count " + count  );
				this.wait( );
			}
		}
		catch ( InterruptedException e )
		{
			e.printStackTrace( );
		}
	}

	/**
	 * release semaphore, i.e. "wakeup signal"
	 */
	public synchronized void release( )
	{
		--count;
		System.out.println( "Semephore release, count turn to: " + count );
		this.notify( );
	}
}