Class CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<ID>

java.lang.Object
com.google.inject.internal.CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<ID>
All Implemented Interfaces:
CycleDetectingLock<ID>
Enclosing class:
CycleDetectingLock.CycleDetectingLockFactory<ID>

static class CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<ID> extends Object implements CycleDetectingLock<ID>
The implementation for CycleDetectingLock.
  • Field Details

    • lockImplementation

      private final Lock lockImplementation
      Underlying lock used for actual waiting when no potential deadlocks are detected.
    • userLockId

      private final ID userLockId
      User id for this lock.
    • lockFactory

      Factory that was used to create this lock.
    • lockOwnerThread

      private Thread lockOwnerThread
      Thread that owns this lock. Nullable. Guarded by CycleDetectingLockFactory.this.
    • lockReentranceCount

      private int lockReentranceCount
      Number of times that thread owned this lock. Guarded by CycleDetectingLockFactory.this.
  • Constructor Details

  • Method Details

    • lockOrDetectPotentialLocksCycle

      public com.google.common.collect.ListMultimap<Thread,ID> lockOrDetectPotentialLocksCycle()
      Description copied from interface: CycleDetectingLock
      Takes a lock in a blocking fashion in case no potential deadlocks are detected. If the lock was successfully owned, returns an empty map indicating no detected potential deadlocks.

      Otherwise, a map indicating threads involved in a potential deadlock are returned. Map is ordered by dependency cycle and lists locks for each thread that are part of the loop in order, the last lock in the list is the one that the thread is currently waiting for. Returned map is created atomically.

      In case no cycle is detected performance is O(threads creating singletons), in case cycle is detected performance is O(singleton locks).

      Specified by:
      lockOrDetectPotentialLocksCycle in interface CycleDetectingLock<ID>
    • unlock

      public void unlock()
      Description copied from interface: CycleDetectingLock
      Unlocks previously locked lock.
      Specified by:
      unlock in interface CycleDetectingLock<ID>
    • checkState

      void checkState() throws IllegalStateException
      Check consistency of an internal state.
      Throws:
      IllegalStateException
    • detectPotentialLocksCycle

      private com.google.common.collect.ListMultimap<Thread,ID> detectPotentialLocksCycle()
      Algorithm to detect a potential lock cycle.

      For lock's thread owner check which lock is it trying to take. Repeat recursively. When current thread is found a potential cycle is detected.

      See Also:
    • addAllLockIdsAfter

      private CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<?> addAllLockIdsAfter(Thread thread, CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<?> lock, com.google.common.collect.ListMultimap<Thread,ID> potentialLocksCycle)
      Adds all locks held by the given thread that are after the given lock and then returns the lock the thread is currently waiting on, if any
    • toString

      public String toString()
      Overrides:
      toString in class Object