Class ReadWriteLockExecutor

java.lang.Object
com.inductiveautomation.historian.gateway.util.ReadWriteLockExecutor

public class ReadWriteLockExecutor extends Object
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:

  • withWriteLock inside a held write section runs inline.
  • withReadLock inside a held write or read section runs inline.
  • withWriteLock inside a held read section throws IllegalStateException: 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 Details

    • ReadWriteLockExecutor

      public ReadWriteLockExecutor()
  • Method Details

    • withWriteLock

      public <T, E extends Exception> T withWriteLock(CheckedSupplier<T,E> action) throws E
      Executes the given supplier within a write lock, returning its result.
      Throws:
      E
    • withWriteLock

      public <E extends Exception> void withWriteLock(CheckedRunnable<E> action) throws E
      Executes the given runnable within a write lock.
      Throws:
      E
    • withReadLock

      public <T, E extends Exception> T withReadLock(CheckedSupplier<T,E> action) throws E
      Executes the given supplier within a read lock, returning its result.
      Throws:
      E
    • withReadLock

      public <E extends Exception> void withReadLock(CheckedRunnable<E> action) throws E
      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 for withWriteLock(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 acquired
      E
    • 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 within timeout. Reentrancy is as for withWriteLockInterruptibly(CheckedRunnable).
      Parameters:
      timeout - the longest time to wait for the lock
      unit - the unit of timeout
      action - the action to run
      Returns:
      true if the action ran, false if the timeout elapsed first
      Throws:
      InterruptedException - if the thread is interrupted while waiting
      E
    • 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 for withReadLock(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 acquired
      E
    • 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.