org.apache.jcs.utils.locking
Class ReadWriteLock

java.lang.Object
  extended byorg.apache.jcs.utils.locking.ReadWriteLock

public class ReadWriteLock
extends java.lang.Object

This class coordinates concurrent calls to an object's get and set methods so that calls to the object set methods do not interfere with each other or with calls to the object's get methods.

Only a single instance of this class should be created per specific resource that requires Read/Write lock protection.

The invariant required by this class is that the method done must be called, and only be called, after a previous call to either the method readLock or writeLock.

Author:
asmuts

Field Summary
private static org.apache.commons.logging.Log log
           
private  int outstandingReadLocks
          Number of threads reading.
private  int outstandingWriteLocks
          The number of (nested) write locks that have been requested from writeLockedThread.
private  int waitingForReadLock
          Number of threads waiting to read.
private  java.util.ArrayList waitingForWriteLock
          Threads waiting to get a write lock are tracked in this ArrayList to ensure that write locks are issued in the same order they are requested.
private  java.lang.Thread writeLockedThread
          The thread that has the write lock or null.
 
Constructor Summary
ReadWriteLock()
          Default constructor.
 
Method Summary
 void done()
          Threads call this method to relinquish a lock that they previously got from this object.
 void readLock()
          Issue a read lock if there is no outstanding write lock or threads waiting to get a write lock.
 void writeLock()
          Issue a write lock if there are no outstanding read or write locks.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static final org.apache.commons.logging.Log log

waitingForReadLock

private int waitingForReadLock
Number of threads waiting to read.


outstandingReadLocks

private int outstandingReadLocks
Number of threads reading.


writeLockedThread

private java.lang.Thread writeLockedThread
The thread that has the write lock or null.


outstandingWriteLocks

private int outstandingWriteLocks
The number of (nested) write locks that have been requested from writeLockedThread.


waitingForWriteLock

private java.util.ArrayList waitingForWriteLock
Threads waiting to get a write lock are tracked in this ArrayList to ensure that write locks are issued in the same order they are requested.

Constructor Detail

ReadWriteLock

public ReadWriteLock()
Default constructor.

Method Detail

readLock

public void readLock()
              throws java.lang.InterruptedException
Issue a read lock if there is no outstanding write lock or threads waiting to get a write lock. Caller of this method must be careful to avoid synchronizing the calling code so as to avoid deadlock.

Throws:
java.lang.InterruptedException

writeLock

public void writeLock()
               throws java.lang.InterruptedException
Issue a write lock if there are no outstanding read or write locks. Caller of this method must be careful to avoid synchronizing the calling code so as to avoid deadlock.

Throws:
java.lang.InterruptedException

done

public void done()
Threads call this method to relinquish a lock that they previously got from this object.

Throws:
java.lang.IllegalStateException - if called when there are no outstanding locks or there is a write lock issued to a different thread.