Package com.google.inject.internal
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 java.lang.Object implements CycleDetectingLock<ID>
The implementation forCycleDetectingLock
.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.google.inject.internal.CycleDetectingLock
CycleDetectingLock.CycleDetectingLockFactory<ID>
-
-
Field Summary
Fields Modifier and Type Field Description private CycleDetectingLock.CycleDetectingLockFactory<ID>
lockFactory
Factory that was used to create this lock.private java.util.concurrent.locks.Lock
lockImplementation
Underlying lock used for actual waiting when no potential deadlocks are detected.private java.lang.Thread
lockOwnerThread
Thread that owns this lock.private int
lockReentranceCount
Number of times that thread owned this lock.private ID
userLockId
User id for this lock.
-
Constructor Summary
Constructors Constructor Description ReentrantCycleDetectingLock(CycleDetectingLock.CycleDetectingLockFactory<ID> lockFactory, ID userLockId, java.util.concurrent.locks.Lock lockImplementation)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<?>
addAllLockIdsAfter(java.lang.Thread thread, CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<?> lock, com.google.common.collect.ListMultimap<java.lang.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(package private) void
checkState()
Check consistency of an internal state.private com.google.common.collect.ListMultimap<java.lang.Thread,ID>
detectPotentialLocksCycle()
Algorithm to detect a potential lock cycle.com.google.common.collect.ListMultimap<java.lang.Thread,ID>
lockOrDetectPotentialLocksCycle()
Takes a lock in a blocking fashion in case no potential deadlocks are detected.java.lang.String
toString()
void
unlock()
Unlocks previously locked lock.
-
-
-
Field Detail
-
lockImplementation
private final java.util.concurrent.locks.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
private final CycleDetectingLock.CycleDetectingLockFactory<ID> lockFactory
Factory that was used to create this lock.
-
lockOwnerThread
private java.lang.Thread lockOwnerThread
Thread that owns this lock. Nullable. Guarded byCycleDetectingLockFactory.this
.
-
lockReentranceCount
private int lockReentranceCount
Number of times that thread owned this lock. Guarded byCycleDetectingLockFactory.this
.
-
-
Constructor Detail
-
ReentrantCycleDetectingLock
ReentrantCycleDetectingLock(CycleDetectingLock.CycleDetectingLockFactory<ID> lockFactory, ID userLockId, java.util.concurrent.locks.Lock lockImplementation)
-
-
Method Detail
-
lockOrDetectPotentialLocksCycle
public com.google.common.collect.ListMultimap<java.lang.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 interfaceCycleDetectingLock<ID>
-
unlock
public void unlock()
Description copied from interface:CycleDetectingLock
Unlocks previously locked lock.- Specified by:
unlock
in interfaceCycleDetectingLock<ID>
-
checkState
void checkState() throws java.lang.IllegalStateException
Check consistency of an internal state.- Throws:
java.lang.IllegalStateException
-
detectPotentialLocksCycle
private com.google.common.collect.ListMultimap<java.lang.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.
-
addAllLockIdsAfter
private CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<?> addAllLockIdsAfter(java.lang.Thread thread, CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<?> lock, com.google.common.collect.ListMultimap<java.lang.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 java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-