Class AbstractReadWriteLockManager
- java.lang.Object
-
- org.eclipse.rdf4j.common.concurrent.locks.AbstractReadWriteLockManager
-
- All Implemented Interfaces:
ReadWriteLockManager
- Direct Known Subclasses:
ReadPrefReadWriteLockManager,WritePrefReadWriteLockManager
public abstract class AbstractReadWriteLockManager extends java.lang.Object implements ReadWriteLockManager
An abstract base implementation of a read/write-lock manager.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classAbstractReadWriteLockManager.ReadLock(package private) static classAbstractReadWriteLockManager.WriteLock
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.concurrent.atomic.LongAdderreadersLocked(package private) java.util.concurrent.atomic.LongAdderreadersUnlockedprivate LockMonitoring<AbstractReadWriteLockManager.ReadLock>readLockMonitoring(package private) java.util.concurrent.locks.StampedLockstampedLockprivate inttryWriteLockMillisprivate LockMonitoring<AbstractReadWriteLockManager.WriteLock>writeLockMonitoring(package private) intwritePreferenceWhen acquiring a write-lock, the thread will acquire the write-lock and then spin & yield while waiting for readers to unlock their locks.
-
Constructor Summary
Constructors Constructor Description AbstractReadWriteLockManager()AbstractReadWriteLockManager(boolean trackLocks)AbstractReadWriteLockManager(boolean trackLocks, int waitToCollect)AbstractReadWriteLockManager(java.lang.String alias, int waitToCollect, LockDiagnostics... lockDiagnostics)AbstractReadWriteLockManager(java.lang.String alias, LockDiagnostics... lockDiagnostics)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description (package private) AbstractReadWriteLockManager.ReadLockcreateReadLockInner()private AbstractReadWriteLockManager.WriteLockcreateWriteLockInner()LockgetReadLock()Gets a read lock.LockgetWriteLock()Gets an exclusive write lock.(package private) abstract intgetWriterPreference()booleanisReaderActive()Returnsfalseif there are no active read locks, otherwise returnstrue.booleanisWriterActive()Returnsfalseif there are no active write locks, otherwise returnstrue.(package private) voidspinWait()(package private) voidspinWaitAtReadLock()LocktryReadLock()Gets a read lock, if available.private AbstractReadWriteLockManager.ReadLocktryReadLockInner()LocktryWriteLock()Gets an exclusive write lock, if available.private AbstractReadWriteLockManager.WriteLocktryWriteLockInner()voidwaitForActiveReaders()Blocks until all read locks have been released.voidwaitForActiveWriter()Blocks until all write locks have been released.private longwriteLockInterruptibly()private voidyieldWait()
-
-
-
Field Detail
-
readLockMonitoring
private final LockMonitoring<AbstractReadWriteLockManager.ReadLock> readLockMonitoring
-
writeLockMonitoring
private final LockMonitoring<AbstractReadWriteLockManager.WriteLock> writeLockMonitoring
-
stampedLock
final java.util.concurrent.locks.StampedLock stampedLock
-
readersLocked
final java.util.concurrent.atomic.LongAdder readersLocked
-
readersUnlocked
final java.util.concurrent.atomic.LongAdder readersUnlocked
-
tryWriteLockMillis
private final int tryWriteLockMillis
-
writePreference
final int writePreference
When acquiring a write-lock, the thread will acquire the write-lock and then spin & yield while waiting for readers to unlock their locks. A deadlock is possible if someone already holding a read-lock acquires another read-lock at the same time that another thread is waiting for a write-lock. To stop this from happening we can set READ_PREFERENCE to a number higher than zero. READ_PREFERENCE of 1 means that the thread acquiring a write-lock will release the write-lock if there are any readers. A READ_PREFERENCE of 100 means that the thread acquiring a write-lock will spin & yield 100 times before it attempts to release the write-lock.
-
-
Constructor Detail
-
AbstractReadWriteLockManager
public AbstractReadWriteLockManager()
-
AbstractReadWriteLockManager
public AbstractReadWriteLockManager(boolean trackLocks)
-
AbstractReadWriteLockManager
public AbstractReadWriteLockManager(boolean trackLocks, int waitToCollect)
-
AbstractReadWriteLockManager
public AbstractReadWriteLockManager(java.lang.String alias, LockDiagnostics... lockDiagnostics)
-
AbstractReadWriteLockManager
public AbstractReadWriteLockManager(java.lang.String alias, int waitToCollect, LockDiagnostics... lockDiagnostics)
-
-
Method Detail
-
getWriterPreference
abstract int getWriterPreference()
-
isWriterActive
public boolean isWriterActive()
Returnsfalseif there are no active write locks, otherwise returnstrue.- Specified by:
isWriterActivein interfaceReadWriteLockManager
-
isReaderActive
public boolean isReaderActive()
Returnsfalseif there are no active read locks, otherwise returnstrue.- Specified by:
isReaderActivein interfaceReadWriteLockManager
-
waitForActiveWriter
public void waitForActiveWriter() throws java.lang.InterruptedExceptionBlocks until all write locks have been released.- Specified by:
waitForActiveWriterin interfaceReadWriteLockManager- Throws:
java.lang.InterruptedException- In case the thread requesting the lock wasinterrupted.
-
waitForActiveReaders
public void waitForActiveReaders() throws java.lang.InterruptedExceptionBlocks until all read locks have been released.- Specified by:
waitForActiveReadersin interfaceReadWriteLockManager- Throws:
java.lang.InterruptedException- In case the thread requesting the lock wasinterrupted.
-
getReadLock
public Lock getReadLock() throws java.lang.InterruptedException
Gets a read lock. This method blocks until the read lock is available.- Specified by:
getReadLockin interfaceReadWriteLockManager- Throws:
java.lang.InterruptedException- In case the thread requesting the lock wasinterrupted.
-
createReadLockInner
AbstractReadWriteLockManager.ReadLock createReadLockInner() throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
getWriteLock
public Lock getWriteLock() throws java.lang.InterruptedException
Gets an exclusive write lock. This method blocks until the write lock is available.- Specified by:
getWriteLockin interfaceReadWriteLockManager- Throws:
java.lang.InterruptedException- In case the thread requesting the lock wasinterrupted.
-
createWriteLockInner
private AbstractReadWriteLockManager.WriteLock createWriteLockInner() throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
writeLockInterruptibly
private long writeLockInterruptibly() throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException
-
tryReadLock
public Lock tryReadLock()
Gets a read lock, if available. This method will return null if the read lock is not immediately available.- Specified by:
tryReadLockin interfaceReadWriteLockManager
-
tryReadLockInner
private AbstractReadWriteLockManager.ReadLock tryReadLockInner()
-
tryWriteLock
public Lock tryWriteLock()
Gets an exclusive write lock, if available. This method will return null if the write lock is not immediately available.- Specified by:
tryWriteLockin interfaceReadWriteLockManager
-
tryWriteLockInner
private AbstractReadWriteLockManager.WriteLock tryWriteLockInner()
-
spinWait
void spinWait() throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException
-
spinWaitAtReadLock
void spinWaitAtReadLock() throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException
-
yieldWait
private void yieldWait() throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException
-
-