org.castor.util.concurrent

Class WriterPreferenceReadWriteLock

Implemented Interfaces:
ReadWriteLock
Known Direct Subclasses:
ReaderPreferenceReadWriteLock, ReentrantWriterPreferenceReadWriteLock

public class WriterPreferenceReadWriteLock
extends java.lang.Object
implements ReadWriteLock

A ReadWriteLock that prefers waiting writers over waiting readers when there is contention. This class is adapted from the versions described in CPJ, improving on the ones there a bit by segregating reader and writer wait queues, which is typically more efficient.

The locks are NOT reentrant. In particular, even though it may appear to usually work OK, a thread holding a read lock should not attempt to re-acquire it. Doing so risks lockouts when there are also waiting writers.

[ Introduction to this package. ]

Nested Class Summary

protected class
WriterPreferenceReadWriteLock.ReaderLock
protected abstract class
WriterPreferenceReadWriteLock.Signaller
Reader and Writer requests are maintained in two different wait sets, by two different objects.
protected class
WriterPreferenceReadWriteLock.WriterLock

Field Summary

protected long
activeReaders_
protected Thread
activeWriter_
protected WriterPreferenceReadWriteLock.ReaderLock
readerLock_
protected long
waitingReaders_
protected long
waitingWriters_
protected WriterPreferenceReadWriteLock.WriterLock
writerLock_

Method Summary

protected boolean
allowReader()
Override this method to change to reader preference *
protected void
cancelledWaitingReader()
protected void
cancelledWaitingWriter()
protected WriterPreferenceReadWriteLock.Signaller
endRead()
Called upon termination of a read.
protected WriterPreferenceReadWriteLock.Signaller
endWrite()
Called upon termination of a write.
Sync
readLock()
get the readLock *
protected boolean
startRead()
protected boolean
startReadFromNewReader()
protected boolean
startReadFromWaitingReader()
protected boolean
startWrite()
protected boolean
startWriteFromNewWriter()
protected boolean
startWriteFromWaitingWriter()
Sync
writeLock()
get the writeLock *

Field Details

activeReaders_

protected long activeReaders_

activeWriter_

protected Thread activeWriter_

readerLock_

protected final WriterPreferenceReadWriteLock.ReaderLock readerLock_

waitingReaders_

protected long waitingReaders_

waitingWriters_

protected long waitingWriters_

writerLock_

protected final WriterPreferenceReadWriteLock.WriterLock writerLock_

Method Details

allowReader

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

cancelledWaitingReader

protected void cancelledWaitingReader()

cancelledWaitingWriter

protected void cancelledWaitingWriter()

endRead

protected WriterPreferenceReadWriteLock.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 WriterPreferenceReadWriteLock.Signaller endWrite()
Called upon termination of a write. Returns the object to signal to wake up a waiter, or null if no such

readLock

public Sync readLock()
get the readLock *
Specified by:
readLock in interface ReadWriteLock

startRead

protected boolean startRead()

startReadFromNewReader

protected boolean startReadFromNewReader()

startReadFromWaitingReader

protected boolean startReadFromWaitingReader()

startWrite

protected boolean startWrite()

startWriteFromNewWriter

protected boolean startWriteFromNewWriter()

startWriteFromWaitingWriter

protected boolean startWriteFromWaitingWriter()

writeLock

public Sync writeLock()
get the writeLock *
Specified by:
writeLock in interface ReadWriteLock

Intalio Inc. (C) 1999-2008. All rights reserved http://www.intalio.com