com.sun.slamd.common
Class RefCountMutex

java.lang.Object
  extended by com.sun.slamd.common.RefCountMutex

public class RefCountMutex
extends java.lang.Object

This class implements a mutex based on reference counts so that multiple readers may hold the lock at the same time, but a writer must have exclusive access to the lock (that is, no read locks may be in use while a write lock is held).

Author:
Neil A. Wilson

Constructor Summary
RefCountMutex()
          Creates a new reference count mutex.
RefCountMutex(boolean debugMode)
          Creates a new reference count mutex, optionally operating in debug mode.
 
Method Summary
 void debugPrint(java.lang.String message)
          Prints the specified message to standard error if debug mode is enabled.
 void getReadLock()
          Obtains a read lock.
 void getWriteLock()
          Obtains a write lock.
 void getWriteLock(long timeout)
          Obtains a write lock.
 void releaseReadLock()
          Releases a previously obtained read lock.
 void releaseWriteLock()
          Releases a previously obtained write lock.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RefCountMutex

public RefCountMutex()
Creates a new reference count mutex.


RefCountMutex

public RefCountMutex(boolean debugMode)
Creates a new reference count mutex, optionally operating in debug mode.

Parameters:
debugMode - Indicates debug logging should be performed.
Method Detail

getReadLock

public void getReadLock()
Obtains a read lock. This method will not return until the read lock has been granted.


releaseReadLock

public void releaseReadLock()
Releases a previously obtained read lock.


getWriteLock

public void getWriteLock()
Obtains a write lock. This method will not return until the write lock has been granted.


getWriteLock

public void getWriteLock(long timeout)
                  throws java.lang.InterruptedException
Obtains a write lock. This method will not return until the write lock has been granted, or until the specified timeout occurs.

Parameters:
timeout - The maximum length of time (in milliseconds) to wait on the write lock.
Throws:
java.lang.InterruptedException - If the write lock could not be obtained before the timeout occurred.

releaseWriteLock

public void releaseWriteLock()
Releases a previously obtained write lock.


debugPrint

public void debugPrint(java.lang.String message)
Prints the specified message to standard error if debug mode is enabled. If debug mode is not enabled, then nothing will be printed.

Parameters:
message - The message to be printed.