public class GenericLockManager extends Object implements LockManager, LockManager2
GenericLock
s on resources. This implementation includes
Modifier and Type | Field and Description |
---|---|
protected long |
checkThreshhold |
static long |
DEFAULT_CHECK_THRESHHOLD |
static long |
DEFAULT_TIMEOUT |
protected Map |
effectiveGlobalTimeouts
Maps onwerId to global effective time outs (i.e.
|
protected Map |
globalLocks
Maps resourceId to lock.
|
protected Map |
globalOwners
Maps onwerId to locks it (partially) owns.
|
protected long |
globalTimeoutMSecs |
protected LoggerFacade |
logger |
protected int |
maxLockLevel |
protected Set |
timedOutOwners |
Constructor and Description |
---|
GenericLockManager(int maxLockLevel,
LoggerFacade logger) |
GenericLockManager(int maxLockLevel,
LoggerFacade logger,
long timeoutMSecs) |
GenericLockManager(int maxLockLevel,
LoggerFacade logger,
long timeoutMSecs,
long checkThreshholdMSecs)
Creates a new generic lock manager.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addOwner(Object ownerId,
GenericLock lock) |
MultiLevelLock |
atomicGetOrCreateLock(Object resourceId)
Either gets an existing lock on the specified resource or creates one if none exists.
|
boolean |
checkLock(Object ownerId,
Object resourceId,
int targetLockLevel,
boolean reentrant)
Determines if a lock could be acquire without actually acquiring it.
|
protected GenericLock |
createLock(Object resourceId) |
protected void |
doLock(GenericLock lock,
Object ownerId,
Object resourceId,
int targetLockLevel,
int compatibility,
boolean preferred,
long timeoutMSecs) |
Set |
getAll(Object ownerId)
Gets all locks (partially) held by an owner.
|
int |
getLevel(Object ownerId,
Object resourceId)
Gets the lock level held by certain owner on a certain resource.
|
MultiLevelLock |
getLock(Object resourceId)
Gets an existing lock on the specified resource.
|
Collection |
getLocks()
Gets all locks as orignials, no copies.
|
protected long |
getNextGlobalConflictTimeout(Set conflicts) |
boolean |
hasLock(Object ownerId,
Object resourceId,
int lockLevel)
Determines if a lock is owner by an owner.
|
void |
lock(Object ownerId,
Object resourceId,
int targetLockLevel,
boolean reentrant)
Tries to acquire a lock on a resource.
|
void |
lock(Object ownerId,
Object resourceId,
int targetLockLevel,
boolean reentrant,
long timeoutMSecs)
Tries to acquire a lock on a resource.
|
void |
lock(Object ownerId,
Object resourceId,
int targetLockLevel,
int compatibility,
boolean preferred,
long timeoutMSecs)
Most flexible way to acquire a lock on a resource.
|
boolean |
release(Object ownerId,
Object resourceId)
Releases all locks for a certain resource held by a certain owner.
|
void |
releaseAll(Object ownerId)
Releases all locks (partially) held by an owner.
|
protected void |
releaseAllNoTimeOutReset(Object ownerId) |
protected boolean |
releaseTimedOutOwners() |
void |
removeLock(MultiLevelLock lock)
Removes the specified lock from the associated resource.
|
protected void |
removeOwner(Object ownerId,
GenericLock lock) |
protected void |
removeOwnerWithoutLocks(Object ownerId) |
void |
startGlobalTimeout(Object ownerId,
long timeoutMSecs)
Starts a global timeout for an owner.
|
protected boolean |
timeOut(Object ownerId) |
protected void |
timeoutCheck(Object ownerId) |
String |
toString() |
boolean |
tryLock(Object ownerId,
Object resourceId,
int targetLockLevel,
boolean reentrant)
Tries to acquire a lock on a resource.
|
protected boolean |
wouldDeadlock(Object ownerId,
Set path)
Checks if an owner is deadlocked.
|
public static final long DEFAULT_TIMEOUT
public static final long DEFAULT_CHECK_THRESHHOLD
protected Map globalOwners
protected Map globalLocks
protected Map effectiveGlobalTimeouts
protected Set timedOutOwners
protected int maxLockLevel
protected LoggerFacade logger
protected long globalTimeoutMSecs
protected long checkThreshhold
public GenericLockManager(int maxLockLevel, LoggerFacade logger, long timeoutMSecs, long checkThreshholdMSecs) throws IllegalArgumentException
maxLockLevel
- highest allowed lock level as described in GenericLock
's class intrologger
- generic logger used for all kind of debug loggingtimeoutMSecs
- specifies the maximum time to wait for a lock in millisecondscheckThreshholdMSecs
- specifies a special wait threshhold before deadlock and
timeout detection come into play or -1
switch
it off and check for directlyIllegalArgumentException
- if maxLockLevel is less than 1public GenericLockManager(int maxLockLevel, LoggerFacade logger, long timeoutMSecs) throws IllegalArgumentException
IllegalArgumentException
public GenericLockManager(int maxLockLevel, LoggerFacade logger) throws IllegalArgumentException
IllegalArgumentException
public void startGlobalTimeout(Object ownerId, long timeoutMSecs)
LockManager2
LockManager2.releaseAll(Object)
.startGlobalTimeout
in interface LockManager2
ownerId
- a unique id identifying the entity that wants to acquire this
locktimeoutMSecs
- specifies the global timeout in millisecondsLockManager2.startGlobalTimeout(Object, long)
public boolean tryLock(Object ownerId, Object resourceId, int targetLockLevel, boolean reentrant)
LockManager2
false
will be returned.tryLock
in interface LockManager2
ownerId
- a unique id identifying the entity that wants to acquire this
lockresourceId
- the resource to get the level fortargetLockLevel
- the lock level to acquirereentrant
- true
if this request shall not be influenced by
other locks held by the same ownertrue
if the lock has been acquired, false
otherwiseLockManager2.tryLock(Object, Object, int, boolean)
public boolean checkLock(Object ownerId, Object resourceId, int targetLockLevel, boolean reentrant)
LockManager2
checkLock
in interface LockManager2
ownerId
- a unique id identifying the entity that wants to check this
lockresourceId
- the resource to get the level fortargetLockLevel
- the lock level to checkreentrant
- true
if this request shall not be influenced by
other locks held by the same ownertrue
if the lock could be acquired, false
otherwiseLockManager2.checkLock(Object, Object, int, boolean)
public boolean hasLock(Object ownerId, Object resourceId, int lockLevel)
LockManager2
hasLock
in interface LockManager2
ownerId
- a unique id identifying the entity that wants to check this
lockresourceId
- the resource to get the level forlockLevel
- the lock level to checktrue
if the owner has the lock, false
otherwiseLockManager2.hasLock(Object, Object, int)
public void lock(Object ownerId, Object resourceId, int targetLockLevel, boolean reentrant) throws LockException
LockManager2
lock
in interface LockManager2
ownerId
- a unique id identifying the entity that wants to acquire this
lockresourceId
- the resource to get the level fortargetLockLevel
- the lock level to acquirereentrant
- true
if this request shall not be blocked by
other locks held by the same ownerLockException
- will be thrown when the lock can not be acquiredLockManager2.lock(Object, Object, int, boolean)
public void lock(Object ownerId, Object resourceId, int targetLockLevel, boolean reentrant, long timeoutMSecs) throws LockException
LockManager2
lock
in interface LockManager2
ownerId
- a unique id identifying the entity that wants to acquire this
lockresourceId
- the resource to get the level fortargetLockLevel
- the lock level to acquirereentrant
- true
if this request shall not be blocked by
other locks held by the same ownertimeoutMSecs
- specifies the maximum wait time in millisecondsLockException
- will be thrown when the lock can not be acquiredLockManager2.lock(Object, Object, int, boolean, long)
public void lock(Object ownerId, Object resourceId, int targetLockLevel, int compatibility, boolean preferred, long timeoutMSecs) throws LockException
LockManager2
lock
in interface LockManager2
ownerId
- a unique id identifying the entity that wants to acquire this
lockresourceId
- the resource to get the level fortargetLockLevel
- the lock level to acquirecompatibility
- MultiLevelLock2.COMPATIBILITY_NONE
if no additional compatibility is
desired (same as reentrant set to false) ,
MultiLevelLock2.COMPATIBILITY_REENTRANT
if lock level by the same
owner shall not affect compatibility (same as reentrant set to
true), or MultiLevelLock2.COMPATIBILITY_SUPPORT
if lock levels that
are the same as the desired shall not affect compatibility, or
finally MultiLevelLock2.COMPATIBILITY_REENTRANT_AND_SUPPORT
which is
a combination of reentrant and supportpreferred
- in case this lock request is incompatible with existing ones
and we wait, it shall be granted before other waiting requests
that are not preferredtimeoutMSecs
- specifies the maximum wait time in millisecondsLockException
- will be thrown when the lock can not be acquiredLockManager2.lock(Object, Object, int, int, boolean, long)
protected void doLock(GenericLock lock, Object ownerId, Object resourceId, int targetLockLevel, int compatibility, boolean preferred, long timeoutMSecs)
public int getLevel(Object ownerId, Object resourceId)
LockManager2
getLevel
in interface LockManager2
ownerId
- the id of the owner of the lockresourceId
- the resource to get the level forLockManager2.getLevel(Object, Object)
public boolean release(Object ownerId, Object resourceId)
LockManager2
release
in interface LockManager2
ownerId
- the id of the owner of the lockresourceId
- the resource to releases the lock fortrue
if the lock actually was released, false
in case
there was no lock held by the ownerLockManager2.release(Object, Object)
public void releaseAll(Object ownerId)
LockManager2
releaseAll
in interface LockManager2
ownerId
- the id of the ownerLockManager2.releaseAll(Object)
protected void releaseAllNoTimeOutReset(Object ownerId)
public Set getAll(Object ownerId)
LockManager2
getAll
in interface LockManager2
ownerId
- the id of the ownerLockManager2.getAll(Object)
protected void addOwner(Object ownerId, GenericLock lock)
protected void removeOwner(Object ownerId, GenericLock lock)
protected boolean wouldDeadlock(Object ownerId, Set path)
ownerId
- the owner to check for being deadlockedpath
- initially should be called with an empty settrue
if the owner is deadlocked,
false
otherwiseprotected boolean releaseTimedOutOwners()
protected long getNextGlobalConflictTimeout(Set conflicts)
public MultiLevelLock getLock(Object resourceId)
LockManager
null
.getLock
in interface LockManager
getLock
in interface LockManager2
resourceId
- the resource to get the lock forpublic MultiLevelLock atomicGetOrCreateLock(Object resourceId)
LockManager
atomicGetOrCreateLock
in interface LockManager
resourceId
- the resource to get or create the lock onpublic void removeLock(MultiLevelLock lock)
LockManager
removeLock
in interface LockManager
removeLock
in interface LockManager2
lock
- the lock to be removedpublic Collection getLocks()
protected GenericLock createLock(Object resourceId)
protected void timeoutCheck(Object ownerId) throws LockException
LockException
protected void removeOwnerWithoutLocks(Object ownerId)
Copyright © 2004-2013 The Apache Software Foundation. All Rights Reserved.