Class ReadWriteLockExecutor
java.lang.Object
com.inductiveautomation.historian.gateway.util.ReadWriteLockExecutor
Runs actions under the read or write side of a
StampedLock: withReadLock and
withWriteLock, plus *Interruptibly variants for cancellable callers.
StampedLock is not reentrant, so this executor makes same-thread nesting safe with
per-instance hold counters:
withWriteLockinside a held write section runs inline.withReadLockinside a held write or read section runs inline.withWriteLockinside a held read section throwsIllegalStateException: a same-thread read-to-write upgrade would deadlock and is a programming error.
The *Interruptibly variants honor a pending interrupt even when they run inline.
Methods that require the caller to hold the lock can check isWriteLockedByCurrentThread() and isReadLockedByCurrentThread() in an assert.
Fair scheduling is not available.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanWhether the current thread holds this executor's read lock.booleanWhether the current thread holds this executor's write lock.<E extends Exception>
booleantryWithWriteLock(long timeout, TimeUnit unit, CheckedRunnable<E> action) Runs the action under the write lock if it can be acquired withintimeout.<E extends Exception>
voidwithReadLock(CheckedRunnable<E> action) Executes the given runnable within a read lock.<T, E extends Exception>
TwithReadLock(CheckedSupplier<T, E> action) Executes the given supplier within a read lock, returning its result.<E extends Exception>
voidwithReadLockInterruptibly(CheckedRunnable<E> action) Runs the action under the read lock, abandoning the wait if the thread is interrupted.<E extends Exception>
voidwithWriteLock(CheckedRunnable<E> action) Executes the given runnable within a write lock.<T, E extends Exception>
TwithWriteLock(CheckedSupplier<T, E> action) Executes the given supplier within a write lock, returning its result.<E extends Exception>
voidwithWriteLockInterruptibly(CheckedRunnable<E> action) Runs the action under the write lock, abandoning the wait if the thread is interrupted.
-
Constructor Details
-
ReadWriteLockExecutor
public ReadWriteLockExecutor()
-
-
Method Details
-
withWriteLock
Executes the given supplier within a write lock, returning its result.- Throws:
E
-
withWriteLock
Executes the given runnable within a write lock.- Throws:
E
-
withReadLock
Executes the given supplier within a read lock, returning its result.- Throws:
E
-
withReadLock
Executes the given runnable within a read lock.- Throws:
E
-
withWriteLockInterruptibly
public <E extends Exception> void withWriteLockInterruptibly(CheckedRunnable<E> action) throws E, InterruptedException Runs the action under the write lock, abandoning the wait if the thread is interrupted. Reentrancy is as forwithWriteLock(CheckedRunnable), with a pending interrupt checked before an inline run.- Parameters:
action- the action to run- Throws:
InterruptedException- if the thread is interrupted before the lock is acquiredE
-
tryWithWriteLock
public <E extends Exception> boolean tryWithWriteLock(long timeout, TimeUnit unit, CheckedRunnable<E> action) throws E, InterruptedException Runs the action under the write lock if it can be acquired withintimeout. Reentrancy is as forwithWriteLockInterruptibly(CheckedRunnable).- Parameters:
timeout- the longest time to wait for the lockunit- the unit oftimeoutaction- the action to run- Returns:
trueif the action ran,falseif the timeout elapsed first- Throws:
InterruptedException- if the thread is interrupted while waitingE
-
withReadLockInterruptibly
public <E extends Exception> void withReadLockInterruptibly(CheckedRunnable<E> action) throws E, InterruptedException Runs the action under the read lock, abandoning the wait if the thread is interrupted. Reentrancy is as forwithReadLock(CheckedRunnable), with a pending interrupt checked before an inline run.- Parameters:
action- the action to run- Throws:
InterruptedException- if the thread is interrupted before the lock is acquiredE
-
isWriteLockedByCurrentThread
public boolean isWriteLockedByCurrentThread()Whether the current thread holds this executor's write lock. -
isReadLockedByCurrentThread
public boolean isReadLockedByCurrentThread()Whether the current thread holds this executor's read lock.
-