org.infinispan.util.concurrent.locks
Class LockManagerImpl

java.lang.Object
  extended by org.infinispan.util.concurrent.locks.LockManagerImpl
All Implemented Interfaces:
LockManager
Direct Known Subclasses:
DeadlockDetectingLockManager

public class LockManagerImpl
extends Object
implements LockManager

Handles locks for the MVCC based LockingInterceptor

Since:
4.0
Author:
Manik Surtani (manik@jboss.org), Mircea.Markus@jboss.com

Field Summary
protected  Configuration configuration
           
protected  LockContainer<?> lockContainer
           
protected static boolean trace
           
 
Constructor Summary
LockManagerImpl()
           
 
Method Summary
 boolean acquireLock(InvocationContext ctx, Object key)
          Attempts to lock an entry if the lock isn't already held in the current scope, and records the lock in the context.
 boolean acquireLock(InvocationContext ctx, Object key, long timeoutMillis)
           
 boolean acquireLockNoCheck(InvocationContext ctx, Object key)
          Same as LockManager.acquireLock(org.infinispan.context.InvocationContext, Object), but doesn't check whether the lock is already acquired by the caller.
 int getConcurrencyLevel()
           
protected  long getLockAcquisitionTimeout(InvocationContext ctx)
           
 int getLockId(Object key)
          Returns the 'id' of the lock that will be used to guard access to a given key in the cache.
 int getNumberOfLocksAvailable()
           
 int getNumberOfLocksHeld()
          Retrieves the number of locks currently held.
 Object getOwner(Object key)
          Retrieves the write lock owner, if any, for the specified cache entry.
 void injectDependencies(Configuration configuration, LockContainer<?> lockContainer)
           
 boolean isLocked(Object key)
          Returns true if the cache entry is locked (either for reading or writing) by anyone, and false otherwise.
 boolean lockAndRecord(Object key, InvocationContext ctx, long timeoutMillis)
          Acquires a lock of type lockType, on a specific entry in the cache.
 boolean ownsLock(Object key, Object owner)
          Tests whether a given owner owns a lock of lockType on a particular cache entry.
 boolean possiblyLocked(CacheEntry entry)
          Inspects the entry for signs that it is possibly locked, and hence would need to be unlocked.
 String printLockInfo()
          Prints lock information for all locks.
 void unlock(Collection<Object> lockedKeys, Object lockOwner)
          Releases the lock passed in.
 void unlockAll(InvocationContext ctx)
          Releases locks present in an invocation context and transaction entry, if one is available.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

configuration

protected Configuration configuration

lockContainer

protected volatile LockContainer<?> lockContainer

trace

protected static final boolean trace
Constructor Detail

LockManagerImpl

public LockManagerImpl()
Method Detail

injectDependencies

public void injectDependencies(Configuration configuration,
                               LockContainer<?> lockContainer)

lockAndRecord

public boolean lockAndRecord(Object key,
                             InvocationContext ctx,
                             long timeoutMillis)
                      throws InterruptedException
Description copied from interface: LockManager
Acquires a lock of type lockType, on a specific entry in the cache. This method will try for a period of time and give up if it is unable to acquire the required lock. The period of time is specified in Configuration.getLockAcquisitionTimeout().

Specified by:
lockAndRecord in interface LockManager
Parameters:
key - key to lock
ctx - invocation context associated with this invocation
Returns:
true if the lock was acquired, false otherwise.
Throws:
InterruptedException - if interrupted

getLockAcquisitionTimeout

protected long getLockAcquisitionTimeout(InvocationContext ctx)

unlock

public void unlock(Collection<Object> lockedKeys,
                   Object lockOwner)
Description copied from interface: LockManager
Releases the lock passed in.

Specified by:
unlock in interface LockManager

unlockAll

public void unlockAll(InvocationContext ctx)
Description copied from interface: LockManager
Releases locks present in an invocation context and transaction entry, if one is available.

Locks are released in reverse order of which they are acquired and registered.

Specified by:
unlockAll in interface LockManager
Parameters:
ctx - invocation context to inspect

ownsLock

public boolean ownsLock(Object key,
                        Object owner)
Description copied from interface: LockManager
Tests whether a given owner owns a lock of lockType on a particular cache entry.

Specified by:
ownsLock in interface LockManager
owner - owner
Returns:
true if the owner does own the specified lock type on the specified cache entry, false otherwise.

isLocked

public boolean isLocked(Object key)
Description copied from interface: LockManager
Returns true if the cache entry is locked (either for reading or writing) by anyone, and false otherwise.

Specified by:
isLocked in interface LockManager
Returns:
true of locked; false if not.

getOwner

public Object getOwner(Object key)
Description copied from interface: LockManager
Retrieves the write lock owner, if any, for the specified cache entry.

Specified by:
getOwner in interface LockManager
Returns:
the owner of the lock, or null if not locked.

printLockInfo

public String printLockInfo()
Description copied from interface: LockManager
Prints lock information for all locks.

Specified by:
printLockInfo in interface LockManager
Returns:
lock information

possiblyLocked

public final boolean possiblyLocked(CacheEntry entry)
Description copied from interface: LockManager
Inspects the entry for signs that it is possibly locked, and hence would need to be unlocked. Note that this is not deterministic, and is pessimistic in that even if an entry is not locked but *might* be locked, this will return true.

As such, this should only be used to determine whether *unlocking* is necessary, not whether locking is necessary. Unlocking an entry that has not been locked has no effect, so this is just an optimisation.

Specified by:
possiblyLocked in interface LockManager
Parameters:
entry - entry to inspect
Returns:
true if the entry *might* be locked, false if the entry definitely is *not* locked.

getConcurrencyLevel

public int getConcurrencyLevel()

getNumberOfLocksHeld

public int getNumberOfLocksHeld()
Description copied from interface: LockManager
Retrieves the number of locks currently held.

Specified by:
getNumberOfLocksHeld in interface LockManager
Returns:
an integer

getNumberOfLocksAvailable

public int getNumberOfLocksAvailable()

getLockId

public int getLockId(Object key)
Description copied from interface: LockManager
Returns the 'id' of the lock that will be used to guard access to a given key in the cache. Particularly useful if Lock Striping is used and locks may guard more than one key. This mechanism can be used to check whether keys may end up sharing the same lock.

If lock-striping is not used, the identity hash code of the lock created for this specific key is returned. While this may not be of much value, it is done to maintain API compatibility of this method regardless of underlying locking scheme.

Specified by:
getLockId in interface LockManager
Parameters:
key - key to test for
Returns:
the ID of the lock.

acquireLock

public final boolean acquireLock(InvocationContext ctx,
                                 Object key)
                          throws InterruptedException,
                                 TimeoutException
Description copied from interface: LockManager
Attempts to lock an entry if the lock isn't already held in the current scope, and records the lock in the context.

Specified by:
acquireLock in interface LockManager
Parameters:
ctx - context
key - Key to lock
Returns:
true if a lock was needed and acquired, false if it didn't need to acquire the lock (i.e., lock was already held)
Throws:
InterruptedException - if interrupted
TimeoutException - if we are unable to acquire the lock after a specified timeout.

acquireLock

public boolean acquireLock(InvocationContext ctx,
                           Object key,
                           long timeoutMillis)
                    throws InterruptedException,
                           TimeoutException
Specified by:
acquireLock in interface LockManager
Throws:
InterruptedException
TimeoutException

acquireLockNoCheck

public final boolean acquireLockNoCheck(InvocationContext ctx,
                                        Object key)
                                 throws InterruptedException,
                                        TimeoutException
Description copied from interface: LockManager
Same as LockManager.acquireLock(org.infinispan.context.InvocationContext, Object), but doesn't check whether the lock is already acquired by the caller. Useful in the case of transactions that use OwnableReentrantLocks ,as these locks already perform this check internally.

Specified by:
acquireLockNoCheck in interface LockManager
Throws:
InterruptedException
TimeoutException

-->

Copyright © 2012 JBoss, a division of Red Hat. All Rights Reserved.