org.jboss.cache.lock
Class ReadWriteLockWithUpgrade

java.lang.Object
  extended by org.jboss.cache.lock.ReadWriteLockWithUpgrade
All Implemented Interfaces:
ReadWriteLock
Direct Known Subclasses:
NonBlockingWriterLock

public class ReadWriteLockWithUpgrade
extends Object
implements ReadWriteLock

This class is similar to PreferredWriterReadWriteLock except that the read lock is upgradable to write lock. I.e., when a user calls upgradeLock(), it will release the read lock, wait for all the read locks to clear and obtain a write lock afterwards. In particular, the write lock is obtained with priority to prevent deadlock situation. The current design is based in part from Doug Lea's PreferredWriterReadWriteLock.

Note that the pre-requisite to use upgrade lock is the pre-existing of a read lock. Otherwise, a RuntimeException will be thrown.

Also note that currently lock can only be obtained through attempt api with specified timeout instead acquire is not supported.

Internally, the upgrade is done through a Semaphore where a thread with a higher priority will obtain the write lock first. The following scenarios then can happen:

Author:
Ben Wang

Nested Class Summary
protected  class ReadWriteLockWithUpgrade.ReaderLock
           
protected  class ReadWriteLockWithUpgrade.WriterLock
           
 
Field Summary
protected  Thread activeWriter_
           
protected static Object dummy_
           
protected static org.apache.commons.logging.Log log_
           
protected  ReadWriteLockWithUpgrade.ReaderLock readerLock_
           
protected static Map<ReadWriteLock,Object> upgraderLocal_
           
protected  ReadWriteLockWithUpgrade.WriterLock writerLock_
           
 
Constructor Summary
ReadWriteLockWithUpgrade()
           
 
Method Summary
protected  boolean allowReader()
          Override this method to change to reader preference *
protected  void cancelledWaitingReader()
           
protected  void cancelledWaitingWriter()
           
protected  Lock changeLock()
           
protected  org.jboss.cache.lock.ReadWriteLockWithUpgrade.Signaller endRead()
          Called upon termination of a read.
protected  org.jboss.cache.lock.ReadWriteLockWithUpgrade.Signaller endWrite()
          Called upon termination of a write.
 Lock readLock()
           
protected  boolean startRead()
           
protected  boolean startReadFromNewReader()
           
protected  boolean startReadFromWaitingReader()
           
protected  boolean startWrite()
           
protected  boolean startWriteFromNewWriter()
           
protected  boolean startWriteFromWaitingWriter()
           
 String toString()
           
 Lock upgradeLockAttempt(long msecs)
          Attempt to obtain an upgrade to writer lock.
 Lock writeLock()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

activeWriter_

protected Thread activeWriter_

upgraderLocal_

protected static final Map<ReadWriteLock,Object> upgraderLocal_

dummy_

protected static final Object dummy_

readerLock_

protected final ReadWriteLockWithUpgrade.ReaderLock readerLock_

writerLock_

protected final ReadWriteLockWithUpgrade.WriterLock writerLock_

log_

protected static final org.apache.commons.logging.Log log_
Constructor Detail

ReadWriteLockWithUpgrade

public ReadWriteLockWithUpgrade()
Method Detail

toString

public String toString()
Overrides:
toString in class Object

writeLock

public Lock writeLock()
Specified by:
writeLock in interface ReadWriteLock

readLock

public Lock readLock()
Specified by:
readLock in interface ReadWriteLock

upgradeLockAttempt

public Lock upgradeLockAttempt(long msecs)
                        throws UpgradeException
Attempt to obtain an upgrade to writer lock. If successful, the read lock is upgraded to write lock. If fails, the owner retains the read lock.

Parameters:
msecs - Time to wait in millisecons.
Returns:
Sync object. Null if not successful or timeout.
Throws:
UpgradeException

changeLock

protected Lock changeLock()

cancelledWaitingReader

protected void cancelledWaitingReader()

cancelledWaitingWriter

protected void cancelledWaitingWriter()

allowReader

protected boolean allowReader()
Override this method to change to reader preference *


startRead

protected boolean startRead()

startWrite

protected boolean startWrite()

startReadFromNewReader

protected boolean startReadFromNewReader()

startWriteFromNewWriter

protected boolean startWriteFromNewWriter()

startReadFromWaitingReader

protected boolean startReadFromWaitingReader()

startWriteFromWaitingWriter

protected boolean startWriteFromWaitingWriter()

endRead

protected org.jboss.cache.lock.ReadWriteLockWithUpgrade.Signaller endRead()
Called upon termination of a read. Returns the object to signal to wake up a waiter, or null if no such


endWrite

protected org.jboss.cache.lock.ReadWriteLockWithUpgrade.Signaller endWrite()
Called upon termination of a write. Returns the object to signal to wake up a waiter, or null if no such



Copyright © 2009 JBoss, a division of Red Hat. All Rights Reserved.