Package com.intellij.util.concurrency
Class ReentrantWriterPreferenceReadWriteLock
- java.lang.Object
-
- com.intellij.util.concurrency.WriterPreferenceReadWriteLock
-
- com.intellij.util.concurrency.ReentrantWriterPreferenceReadWriteLock
-
- All Implemented Interfaces:
ReadWriteLock
public class ReentrantWriterPreferenceReadWriteLock extends WriterPreferenceReadWriteLock
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(); } }- See Also:
ReentrantLock
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.intellij.util.concurrency.WriterPreferenceReadWriteLock
WriterPreferenceReadWriteLock.ReaderLock, WriterPreferenceReadWriteLock.Signaller, WriterPreferenceReadWriteLock.WriterLock
-
-
Field Summary
-
Fields inherited from class com.intellij.util.concurrency.WriterPreferenceReadWriteLock
activeReaders_, activeWriter_, readerLock_, waitingReaders_, waitingWriters_, writerLock_
-
-
Constructor Summary
Constructors Constructor Description ReentrantWriterPreferenceReadWriteLock()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleanallowReader()Override this method to change to reader preferenceprotected WriterPreferenceReadWriteLock.SignallerendRead()Called upon termination of a read.protected WriterPreferenceReadWriteLock.SignallerendWrite()Called upon termination of a write.booleanisReadLockAcquired()booleanisReadLockAcquired(Thread thread)booleanisWriteLockAcquired()booleanisWriteLockAcquired(Thread thread)protected booleanstartRead()protected booleanstartWrite()-
Methods inherited from class com.intellij.util.concurrency.WriterPreferenceReadWriteLock
cancelledWaitingReader, cancelledWaitingWriter, readLock, startReadFromNewReader, startReadFromWaitingReader, startWriteFromNewWriter, startWriteFromWaitingWriter, writeLock
-
-
-
-
Method Detail
-
isReadLockAcquired
public boolean isReadLockAcquired()
-
isWriteLockAcquired
public boolean isWriteLockAcquired()
-
isReadLockAcquired
public boolean isReadLockAcquired(Thread thread)
-
isWriteLockAcquired
public boolean isWriteLockAcquired(Thread thread)
-
allowReader
protected boolean allowReader()
Description copied from class:WriterPreferenceReadWriteLockOverride this method to change to reader preference- Overrides:
allowReaderin classWriterPreferenceReadWriteLock
-
startRead
protected boolean startRead()
- Overrides:
startReadin classWriterPreferenceReadWriteLock
-
startWrite
protected boolean startWrite()
- Overrides:
startWritein classWriterPreferenceReadWriteLock
-
endRead
protected WriterPreferenceReadWriteLock.Signaller endRead()
Description copied from class:WriterPreferenceReadWriteLockCalled upon termination of a read. Returns the object to signal to wake up a waiter, or null if no such- Overrides:
endReadin classWriterPreferenceReadWriteLock
-
endWrite
protected WriterPreferenceReadWriteLock.Signaller endWrite()
Description copied from class:WriterPreferenceReadWriteLockCalled upon termination of a write. Returns the object to signal to wake up a waiter, or null if no such- Overrides:
endWritein classWriterPreferenceReadWriteLock
-
-