org.jboss.cache.lock
Class IdentityLock

java.lang.Object
  extended by org.jboss.cache.lock.IdentityLock
All Implemented Interfaces:
NodeLock

public class IdentityLock
extends java.lang.Object
implements NodeLock

Lock object which grants and releases locks, and associates locks with owners. The methods to acquire and release a lock require an owner (Object). When a lock is acquired, we store the current owner with the lock. When the same owner (but possibly running in a different thread) wants to acquire the lock, it is immediately granted. When an owner different from the one currently owning the lock wants to release the lock, we do nothing (no-op).

Consider the following example:

 FIFOSemaphore lock=new FIFOSemaphore(1);
 lock.acquire();
 lock.acquire();
 lock.release();
 
This would block on the second acquire() (although we currently already hold the lock) because the lock only has 1 permit. So IdentityLock will allow the following code to work properly:
 IdentityLock lock=new IdentityLock();
 lock.readLock().acquire(this, timeout);
 lock.writeLock().acquire(this, timeout);
 lock.release(this);
 
If the owner is null, then the current thread is taken by default. This allow the following code to work:
 IdentityLock lock=new IdentityLock();
 lock.readLock().attempt(null, timeout);
 lock.writeLock().attempt(null, timeout);
 lock.release(null);
 

Note that the Object given as owner is required to implement Object.equals(java.lang.Object). For a use case see the examples in the testsuite.

Version:
$Revision: 1.25 $
Author:
Bela Ban Apr 11, 2003, Ben Wang July 2003

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.jboss.cache.lock.NodeLock
NodeLock.LockType
 
Constructor Summary
IdentityLock(IsolationLevel level, NodeSPI node)
          Creates a new IdentityLock based on the lock level in force.
IdentityLock(NodeSPI node)
          Creates a new Identity lock based on the lock level set in the LockStrategyFactory
 
Method Summary
 boolean acquire(java.lang.Object caller, long timeout, NodeLock.LockType lock_type)
           
 java.util.Set<NodeLock> acquireAll(java.lang.Object caller, long timeout, NodeLock.LockType lock_type)
          Recursively acquire locks for this node and all subnodes.
 boolean acquireReadLock(java.lang.Object caller, long timeout)
          Acquire a read lock with a timeout period of timeout milliseconds.
 boolean acquireWriteLock(java.lang.Object caller, long timeout)
          Acquire a write lock with a timeout of timeout milliseconds.
 Fqn getFqn()
          Returns the FQN this lock, may be null.
 Node getNode()
          Returns the node for this lock, may be null.
 java.util.Set getReaderOwners()
          Return a copy of the reader lock owner in List.
 java.lang.Object getWriterOwner()
          Return the writer lock owner object.
 boolean isLocked()
          Check if there is a read or write lock
 boolean isOwner(java.lang.Object o)
          Am I a lock owner?
 boolean isReadLocked()
          Check if there is a read lock.
 boolean isWriteLocked()
          Check if there is a write lock.
 void printLockInfo(java.lang.StringBuffer sb, int indent)
           
 void release(java.lang.Object caller)
          Release the lock held by the owner.
 void releaseAll()
          Release all locks associated with this instance.
 void releaseAll(java.lang.Object owner)
          Releases all locks with this owner.
 void releaseForce()
          Same as releaseAll now.
 java.lang.String toString()
           
 java.lang.String toString(boolean print_lock_details)
           
 void toString(java.lang.StringBuffer sb)
           
 void toString(java.lang.StringBuffer sb, boolean print_lock_details)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IdentityLock

public IdentityLock(NodeSPI node)
Creates a new Identity lock based on the lock level set in the LockStrategyFactory


IdentityLock

public IdentityLock(IsolationLevel level,
                    NodeSPI node)
Creates a new IdentityLock based on the lock level in force.

Parameters:
level -
Method Detail

getNode

public Node getNode()
Returns the node for this lock, may be null.


getFqn

public Fqn getFqn()
Returns the FQN this lock, may be null.


getReaderOwners

public java.util.Set getReaderOwners()
Return a copy of the reader lock owner in List. Size is zero is not available. Note that this list is synchronized.

Specified by:
getReaderOwners in interface NodeLock
Returns:
Set of readers

getWriterOwner

public java.lang.Object getWriterOwner()
Return the writer lock owner object. Null if not available.

Specified by:
getWriterOwner in interface NodeLock
Returns:
Object owner

acquireWriteLock

public boolean acquireWriteLock(java.lang.Object caller,
                                long timeout)
                         throws LockingException,
                                TimeoutException,
                                java.lang.InterruptedException
Acquire a write lock with a timeout of timeout milliseconds. Note that if the current owner owns a read lock, it will be upgraded automatically. However, if upgrade fails, i.e., timeout, the read lock will be released automatically.

Specified by:
acquireWriteLock in interface NodeLock
Parameters:
caller - Can't be null.
timeout -
Returns:
boolean True if lock was acquired and was not held before, false if lock was held
Throws:
LockingException
TimeoutException
java.lang.InterruptedException

acquireReadLock

public boolean acquireReadLock(java.lang.Object caller,
                               long timeout)
                        throws LockingException,
                               TimeoutException,
                               java.lang.InterruptedException
Acquire a read lock with a timeout period of timeout milliseconds.

Specified by:
acquireReadLock in interface NodeLock
Parameters:
caller - Can't be null.
timeout -
Returns:
boolean True if lock was acquired and was not held before, false if lock was held
Throws:
LockingException
TimeoutException
java.lang.InterruptedException

release

public void release(java.lang.Object caller)
Release the lock held by the owner.

Specified by:
release in interface NodeLock
Parameters:
caller - Can't be null.

releaseAll

public void releaseAll()
Release all locks associated with this instance.

Specified by:
releaseAll in interface NodeLock

releaseForce

public void releaseForce()
Same as releaseAll now.


isReadLocked

public boolean isReadLocked()
Check if there is a read lock.

Specified by:
isReadLocked in interface NodeLock

isWriteLocked

public boolean isWriteLocked()
Check if there is a write lock.

Specified by:
isWriteLocked in interface NodeLock

isLocked

public boolean isLocked()
Check if there is a read or write lock

Specified by:
isLocked in interface NodeLock

isOwner

public boolean isOwner(java.lang.Object o)
Am I a lock owner?

Specified by:
isOwner in interface NodeLock
Parameters:
o -

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toString

public java.lang.String toString(boolean print_lock_details)

toString

public void toString(java.lang.StringBuffer sb)

toString

public void toString(java.lang.StringBuffer sb,
                     boolean print_lock_details)

acquire

public boolean acquire(java.lang.Object caller,
                       long timeout,
                       NodeLock.LockType lock_type)
                throws LockingException,
                       TimeoutException,
                       java.lang.InterruptedException
Specified by:
acquire in interface NodeLock
Throws:
LockingException
TimeoutException
java.lang.InterruptedException

acquireAll

public java.util.Set<NodeLock> acquireAll(java.lang.Object caller,
                                          long timeout,
                                          NodeLock.LockType lock_type)
                                   throws LockingException,
                                          TimeoutException,
                                          java.lang.InterruptedException
Description copied from interface: NodeLock
Recursively acquire locks for this node and all subnodes.

Specified by:
acquireAll in interface NodeLock
Parameters:
caller - lock owner
timeout - time to wait
lock_type - type of lock
Returns:
locks acquired
Throws:
LockingException
TimeoutException
java.lang.InterruptedException

releaseAll

public void releaseAll(java.lang.Object owner)
Description copied from interface: NodeLock
Releases all locks with this owner.

Specified by:
releaseAll in interface NodeLock

printLockInfo

public void printLockInfo(java.lang.StringBuffer sb,
                          int indent)
Specified by:
printLockInfo in interface NodeLock