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 Object
implements CycleDetectingLock<ID>
The implementation for
CycleDetectingLock
.-
Nested Class Summary
Nested classes/interfaces inherited from interface com.google.inject.internal.CycleDetectingLock
CycleDetectingLock.CycleDetectingLockFactory<ID>
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CycleDetectingLock.CycleDetectingLockFactory
<ID> Factory that was used to create this lock.private final Lock
Underlying lock used for actual waiting when no potential deadlocks are detected.private Thread
Thread that owns this lock.private int
Number of times that thread owned this lock.private final ID
User id for this lock. -
Constructor Summary
ConstructorsConstructorDescriptionReentrantCycleDetectingLock
(CycleDetectingLock.CycleDetectingLockFactory<ID> lockFactory, ID userLockId, Lock lockImplementation) -
Method Summary
Modifier and TypeMethodDescriptionaddAllLockIdsAfter
(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(package private) void
Check consistency of an internal state.Algorithm to detect a potential lock cycle.Takes a lock in a blocking fashion in case no potential deadlocks are detected.toString()
void
unlock()
Unlocks previously locked lock.
-
Field Details
-
lockImplementation
Underlying lock used for actual waiting when no potential deadlocks are detected. -
userLockId
User id for this lock. -
lockFactory
Factory that was used to create this lock. -
lockOwnerThread
Thread that owns this lock. Nullable. Guarded byCycleDetectingLockFactory.this
. -
lockReentranceCount
private int lockReentranceCountNumber of times that thread owned this lock. Guarded byCycleDetectingLockFactory.this
.
-
-
Constructor Details
-
ReentrantCycleDetectingLock
ReentrantCycleDetectingLock(CycleDetectingLock.CycleDetectingLockFactory<ID> lockFactory, ID userLockId, Lock lockImplementation)
-
-
Method Details
-
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
Check consistency of an internal state.- Throws:
IllegalStateException
-
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
-