public static class CycleDetectingLock.CycleDetectingLockFactory<ID>
extends java.lang.Object
CycleDetectingLock.lockOrDetectPotentialLocksCycle()
we check for dependency cycles
within locks created by the same factory. Either we detect a cycle and return it
or take it atomically.
Important to note that we do not prevent deadlocks in the client code. As an example: Thread A takes lock L and creates singleton class CA depending on the singleton class CB. Meanwhile thread B is creating class CB and is waiting on the lock L. Issue happens due to client code creating interdependent classes and using locks, where no guarantees on the creation order from Guice are provided.
Instances of these locks are not intended to be exposed outside of SingletonScope
.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<ID>
The implementation for
CycleDetectingLock . |
Modifier and Type | Field and Description |
---|---|
private static com.google.common.collect.Multimap<java.lang.Long,CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<?>> |
locksOwnedByThread
Lists locks that thread owns.
|
private static java.util.Map<java.lang.Long,CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<?>> |
lockThreadIsWaitingOn
Specifies lock that thread is currently waiting on to own it.
|
Constructor and Description |
---|
CycleDetectingLockFactory() |
Modifier and Type | Method and Description |
---|---|
(package private) CycleDetectingLock<ID> |
create(ID userLockId)
Creates new lock within this factory context.
|
private static java.util.Map<java.lang.Long,CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<?>> lockThreadIsWaitingOn
CycleDetectingLock.lockOrDetectPotentialLocksCycle()
before Lock.lock()
is called. Element is removed inside CycleDetectingLock.lockOrDetectPotentialLocksCycle()
after
Lock.lock()
and synchronously with adding it to locksOwnedByThread
.
Same lock can be added for several threads in case all of them are trying to
take it.
Guarded by CycleDetectingLockFactory.class
.private static final com.google.common.collect.Multimap<java.lang.Long,CycleDetectingLock.CycleDetectingLockFactory.ReentrantCycleDetectingLock<?>> locksOwnedByThread
CycleDetectingLock.lockOrDetectPotentialLocksCycle()
after Lock.lock()
is called. Element is removed inside CycleDetectingLock.unlock()
synchronously with
Lock.unlock()
call.
Same lock can only be present several times for the same thread as locks are
reentrant. Lock can not be owned by several different threads as the same time.
Guarded by CycleDetectingLockFactory.class
.CycleDetectingLock<ID> create(ID userLockId)
userLockId
- lock id that would be used to report lock cycles if detected