FileDocCategorySizeDatePackage
AESemaphore.javaAPI DocAzureus 3.0.3.45052Wed Aug 02 09:10:46 BST 2006org.gudy.azureus2.core3.util

AESemaphore

public class AESemaphore extends AEMonSem
author
parg

Fields Summary
private int
dont_wait
private int
total_reserve
private int
total_release
private boolean
released_forever
protected Thread
latest_waiter
Constructors Summary
public AESemaphore(String _name)

	
	
	
				 
	
		this( _name, 0 );
	
public AESemaphore(String _name, int count)

		super( _name, false );
		
		dont_wait		= count;
		total_release	= count;
	
Methods Summary
public java.lang.StringgetString()

		synchronized(this){

			return( "value=" + dont_wait + ",waiting=" + waiting + ",res=" + total_reserve + ",rel=" + total_release );
		}
	
public intgetValue()

		synchronized(this){

			return( dont_wait - waiting );
		}
	
public booleanisReleasedForever()

		synchronized(this){

			return( released_forever );
		}
	
public voidrelease()

		try{
			synchronized(this){
				
				//System.out.println( name + "::release");
	
				total_release++;
	
				if ( waiting != 0 ){
	
					waiting--;
	
					notify();
	
				}else{
					dont_wait++;
				}
			}
		}finally{
			
			if ( DEBUG ){
				
				debugExit();
			}
		}
	
public voidreleaseAllWaiters()

		synchronized(this){

			int	x	= waiting;

			for ( int i=0;i<x;i++ ){
				release();
			}
		}
	
public voidreleaseForever()

		synchronized(this){

			releaseAllWaiters();

			released_forever	= true;
		}
	
public voidreserve()

		if ( !reserve(0)){
			
			Debug.out( "AESemaphore: reserve completed without acquire [" + getString() + "]" );
		}
	
public booleanreserve(long millis)

		return( reserveSupport( millis, 1 ) == 1 );
	
public booleanreserveIfAvailable()

		synchronized(this){

			if ( released_forever || dont_wait > 0 ){

				reserve();
				
				return( true );
				
			}else{
				
				return( false );
			}
		}
	
public intreserveSet(int max_to_reserve, long millis)

		return( reserveSupport( millis, max_to_reserve));
	
public intreserveSet(int max_to_reserve)

		return( reserveSupport( 0, max_to_reserve));
	
protected intreserveSupport(long millis, int max_to_reserve)

		if ( DEBUG ){
			
			super.debugEntry();
		}
		
		synchronized(this){

			entry_count++;
			
			//System.out.println( name + "::reserve");
			
			if ( released_forever ){

				return(1);
			}

			if ( dont_wait == 0 ){

				try{
					waiting++;

					latest_waiter	= Thread.currentThread();
					
					if ( waiting > 1 ){
						
						// System.out.println( "AESemaphore: " + name + " contended" );
					}

					if ( millis == 0 ){
						
							// we can get spurious wakeups (see Object javadoc) so we need to guard against
							// their possibility
						
						int	spurious_count	= 0;
						
						while( true ){
							
							wait();
							
							if ( total_reserve == total_release ){
								
								spurious_count++;

								if ( spurious_count > 1024 ){
								
									Debug.out( "AESemaphore: spurious wakeup limit exceeded" );
									
									throw( new Throwable( "die die die" ));
									
								}else{
								
									Debug.out("AESemaphore: spurious wakeup, ignoring" );
								}					
							}else{
								
								break;
							}
						}
					}else{
						
							// we don't hugely care about spurious wakeups here, it'll just appear
							// as a failed reservation a bit early
						
						wait(millis);
					}
					
					if ( total_reserve == total_release ){
							
							// here we have timed out on the wait without acquiring
						
						waiting--;
						
						return( 0 );
					}
						
					total_reserve++;

					return( 1 );

				}catch( Throwable e ){

					waiting--;

					Debug.out( "**** semaphore operation interrupted ****" );

					throw( new RuntimeException("Semaphore: operation interrupted", e ));
					
				}finally{
					
					latest_waiter = null;
				}
			}else{
				int	num_to_get = max_to_reserve>dont_wait?dont_wait:max_to_reserve;
				
				dont_wait -= num_to_get;

				total_reserve += num_to_get;
				
				return( num_to_get );
			}
		}