org.apache.slide.util.locking.impl
Class GenericLock

java.lang.Object
  extended byorg.apache.slide.util.locking.impl.GenericLock
All Implemented Interfaces:
MultiLevelLock

public class GenericLock
extends java.lang.Object
implements MultiLevelLock

A generic implementaion of a simple multi level lock.

The idea is to have an ascending number of lock levels ranging from 0 to maxLockLevel as specified in GenericLock(Object, int, StoreLogger): the higher the lock level the stronger and more restrictive the lock. To determine which lock may coexist with other locks you have to imagine matching pairs of lock levels. For each pair both parts allow for all lock levels less than or equal to the matching other part. Pairs are composed by the lowest and highest level not yet part of a pair and successively applying this method until no lock level is left. For an even amount of levels each level is part of exactly one pair. For an odd amount the middle level is paired with itself. The highst lock level may coexist with the lowest one (0) which by definition means NO LOCK. This implies that you will have to specify at least one other lock level and thus set maxLockLevel to at least 1.

Although this may sound complicated, in practice this is quite simple. Let us imagine you have three lock levels:

Accordingly, you will have to set maxLockLevel to 2. Now, there are two pairs of levels This means when the current highest lock level is NO LOCK everything less or equal to EXCLUSIVE is allowed - which means every other lock level. On the other side EXCLUSIVE allows exacly for NO LOCK - which means nothing else. In conclusion, SHARED allows for SHARED or NO LOCK, but not for EXCLUSIVE. To make this very clear have a look at this table, where o means compatible or can coexist and x means incompatible or can not coexist:

NO LOCK SHAREDEXCLUSIVE
NO LOCKooo
SHAREDoox
EXCLUSIVEoxx

General limitations include:

Author:
Oliver Zeigermann

Field Summary
protected  StoreLogger logger
           
 
Constructor Summary
GenericLock(java.lang.Object resourceId, int maxLockLevel, StoreLogger logger)
          Creates a new lock.
 
Method Summary
 boolean acquire(java.lang.Object ownerId, int targetLockLevel, boolean wait, boolean reentrant, long timeoutMSecs)
          Tries to acquire a certain lock level on this lock.
 int getLevelMaxLock()
          Gets the highst lock level possible.
 int getLevelMinLock()
          Gets the lowest lock level possible.
 int getLockLevel(java.lang.Object ownerId)
          Retuns the highest lock level the specified owner holds on this lock or 0 if it holds no locks at all.
protected  org.apache.slide.util.locking.impl.GenericLock.LockOwner getMaxLevelOwner()
           
protected  org.apache.slide.util.locking.impl.GenericLock.LockOwner getMaxLevelOwnerNotMe(org.apache.slide.util.locking.impl.GenericLock.LockOwner me)
           
 java.lang.Object getResourceId()
          Gets the resource assotiated to this lock.
 void release(java.lang.Object ownerId)
          Releases any lock levels the specified owner may hold on this lock.
protected  void setLockLevel(java.lang.Object ownerId, org.apache.slide.util.locking.impl.GenericLock.LockOwner lock, int targetLockLevel)
           
protected  boolean tryLock(java.lang.Object ownerId, int targetLockLevel, boolean reentrant)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected StoreLogger logger
Constructor Detail

GenericLock

public GenericLock(java.lang.Object resourceId,
                   int maxLockLevel,
                   StoreLogger logger)
Creates a new lock.

Parameters:
resourceId - identifier for the resource associated to this lock
maxLockLevel - highest allowed lock level as described in class intro
Method Detail

acquire

public boolean acquire(java.lang.Object ownerId,
                       int targetLockLevel,
                       boolean wait,
                       boolean reentrant,
                       long timeoutMSecs)
                throws java.lang.InterruptedException
Description copied from interface: MultiLevelLock
Tries to acquire a certain lock level on this lock.

Specified by:
acquire in interface MultiLevelLock
Parameters:
ownerId - a unique id identifying the entity that wants to acquire a certain lock level on this lock
targetLockLevel - the lock level to acquire
wait - true if this method shall block when the desired lock level can not be acquired
reentrant - true if lock levels of the same entity acquired earlier should not restrict compatibility with the lock level desired now
timeoutMSecs - if blocking is enabled by the wait parameter this specifies the maximum wait time in milliseconds
Returns:
true if the lock actually was acquired
Throws:
java.lang.InterruptedException - when the thread waiting on this method is interrupted

release

public void release(java.lang.Object ownerId)
Description copied from interface: MultiLevelLock
Releases any lock levels the specified owner may hold on this lock.

Specified by:
release in interface MultiLevelLock
Parameters:
ownerId - a unique id identifying the entity that wants to release all lock levels

getLockLevel

public int getLockLevel(java.lang.Object ownerId)
Description copied from interface: MultiLevelLock
Retuns the highest lock level the specified owner holds on this lock or 0 if it holds no locks at all.

Specified by:
getLockLevel in interface MultiLevelLock
Parameters:
ownerId - a unique id identifying the entity that wants to know its highest lock level
Returns:
the highest lock level

getResourceId

public java.lang.Object getResourceId()
Gets the resource assotiated to this lock.

Returns:
identifier for the resource associated to this lock

getLevelMinLock

public int getLevelMinLock()
Gets the lowest lock level possible.

Returns:
minimum lock level

getLevelMaxLock

public int getLevelMaxLock()
Gets the highst lock level possible.

Returns:
maximum lock level

getMaxLevelOwner

protected org.apache.slide.util.locking.impl.GenericLock.LockOwner getMaxLevelOwner()

getMaxLevelOwnerNotMe

protected org.apache.slide.util.locking.impl.GenericLock.LockOwner getMaxLevelOwnerNotMe(org.apache.slide.util.locking.impl.GenericLock.LockOwner me)

setLockLevel

protected void setLockLevel(java.lang.Object ownerId,
                            org.apache.slide.util.locking.impl.GenericLock.LockOwner lock,
                            int targetLockLevel)

tryLock

protected boolean tryLock(java.lang.Object ownerId,
                          int targetLockLevel,
                          boolean reentrant)