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 class
AbstractReadWriteLockManager.ReadLock
(package private) static class
AbstractReadWriteLockManager.WriteLock
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.concurrent.atomic.LongAdder
readersLocked
(package private) java.util.concurrent.atomic.LongAdder
readersUnlocked
private LockMonitoring<AbstractReadWriteLockManager.ReadLock>
readLockMonitoring
(package private) java.util.concurrent.locks.StampedLock
stampedLock
private int
tryWriteLockMillis
private LockMonitoring<AbstractReadWriteLockManager.WriteLock>
writeLockMonitoring
(package private) 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.
-
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.ReadLock
createReadLockInner()
private AbstractReadWriteLockManager.WriteLock
createWriteLockInner()
Lock
getReadLock()
Gets a read lock.Lock
getWriteLock()
Gets an exclusive write lock.(package private) abstract int
getWriterPreference()
boolean
isReaderActive()
Returnsfalse
if there are no active read locks, otherwise returnstrue
.boolean
isWriterActive()
Returnsfalse
if there are no active write locks, otherwise returnstrue
.(package private) void
spinWait()
(package private) void
spinWaitAtReadLock()
Lock
tryReadLock()
Gets a read lock, if available.private AbstractReadWriteLockManager.ReadLock
tryReadLockInner()
Lock
tryWriteLock()
Gets an exclusive write lock, if available.private AbstractReadWriteLockManager.WriteLock
tryWriteLockInner()
void
waitForActiveReaders()
Blocks until all read locks have been released.void
waitForActiveWriter()
Blocks until all write locks have been released.private long
writeLockInterruptibly()
private void
yieldWait()
-
-
-
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()
Returnsfalse
if there are no active write locks, otherwise returnstrue
.- Specified by:
isWriterActive
in interfaceReadWriteLockManager
-
isReaderActive
public boolean isReaderActive()
Returnsfalse
if there are no active read locks, otherwise returnstrue
.- Specified by:
isReaderActive
in interfaceReadWriteLockManager
-
waitForActiveWriter
public void waitForActiveWriter() throws java.lang.InterruptedException
Blocks until all write locks have been released.- Specified by:
waitForActiveWriter
in interfaceReadWriteLockManager
- Throws:
java.lang.InterruptedException
- In case the thread requesting the lock wasinterrupted
.
-
waitForActiveReaders
public void waitForActiveReaders() throws java.lang.InterruptedException
Blocks until all read locks have been released.- Specified by:
waitForActiveReaders
in 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:
getReadLock
in 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:
getWriteLock
in 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:
tryReadLock
in 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:
tryWriteLock
in 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
-
-