|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.sape.carbon.core.util.thread.WriterPreferenceReadWriteLock | +--org.sape.carbon.core.util.thread.ReentrantWriterPreferenceReadWriteLock
A writer-preference ReadWriteLock that allows both readers and writers to reacquire read or write locks in the style of a ReentrantLock. Readers are not allowed until all write locks held by the writing thread have been released. Among other applications, reentrancy can be useful when write locks are held during calls or callbacks to methods that perform reads under read locks.
Sample usage. Here is a code sketch showing how to exploit reentrancy to perform lock downgrading after updating a cache:
class CachedData { Object data; volatile boolean cacheValid; ReentrantWriterPreferenceReadWriteLock rwl = ... void processCachedData() { rwl.readLock().acquire(); if (!cacheValid) { // upgrade lock: rwl.readLock().release(); // must release first to obtain writelock rwl.writeLock().acquire(); if (!cacheValid) { // recheck data = ... cacheValid = true; } // downgrade lock rwl.readLock().acquire(); // reacquire read without giving up lock rwl.writeLock().release(); // release write, still hold read } use(data); rwl.readLock().release(); } }
[ Introduction to this package. ]
Nested Class Summary |
Nested classes inherited from class org.sape.carbon.core.util.thread.WriterPreferenceReadWriteLock |
WriterPreferenceReadWriteLock.ReaderLock, WriterPreferenceReadWriteLock.Signaller, WriterPreferenceReadWriteLock.WriterLock |
Field Summary | |
protected static Integer |
IONE
cache/reuse the special Integer value one to speed up readlocks |
protected HashMap |
readers_
Number of acquires on read lock by any reader thread |
protected long |
writeHolds_
Number of acquires on write lock by activeWriter_ thread |
Fields inherited from class org.sape.carbon.core.util.thread.WriterPreferenceReadWriteLock |
activeReaders_, activeWriter_, readerLock_, waitingReaders_, waitingWriters_, writerLock_ |
Constructor Summary | |
ReentrantWriterPreferenceReadWriteLock()
|
Method Summary | |
protected boolean |
allowReader()
Override this method to change to reader preference |
protected WriterPreferenceReadWriteLock.Signaller |
endRead()
Called upon termination of a read. |
protected WriterPreferenceReadWriteLock.Signaller |
endWrite()
Called upon termination of a write. |
protected boolean |
startRead()
|
protected boolean |
startWrite()
|
Methods inherited from class org.sape.carbon.core.util.thread.WriterPreferenceReadWriteLock |
cancelledWaitingReader, cancelledWaitingWriter, readLock, startReadFromNewReader, startReadFromWaitingReader, startWriteFromNewWriter, startWriteFromWaitingWriter, writeLock |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected long writeHolds_
protected HashMap readers_
protected static final Integer IONE
Constructor Detail |
public ReentrantWriterPreferenceReadWriteLock()
Method Detail |
protected boolean allowReader()
WriterPreferenceReadWriteLock
allowReader
in class WriterPreferenceReadWriteLock
protected boolean startRead()
startRead
in class WriterPreferenceReadWriteLock
protected boolean startWrite()
startWrite
in class WriterPreferenceReadWriteLock
protected WriterPreferenceReadWriteLock.Signaller endRead()
WriterPreferenceReadWriteLock
endRead
in class WriterPreferenceReadWriteLock
protected WriterPreferenceReadWriteLock.Signaller endWrite()
WriterPreferenceReadWriteLock
endWrite
in class WriterPreferenceReadWriteLock
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |