|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.slide.util.locking.impl.GenericLock
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:
0
: NO LOCK
(always needed by the
implementation of this lock)
1
: SHARED
2
: EXCLUSIVE
maxLockLevel
to 2
.
Now, there are two pairs of levels
NO LOCK
with EXCLUSIVE
SHARED
with SHARED
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 | SHARED | EXCLUSIVE | |
NO LOCK | o | o | o |
SHARED | o | o | x |
EXCLUSIVE | o | x | x |
3
which might stand for WRITE
access.
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 |
protected StoreLogger logger
Constructor Detail |
public GenericLock(java.lang.Object resourceId, int maxLockLevel, StoreLogger logger)
resourceId
- identifier for the resource associated to this lockmaxLockLevel
- highest allowed lock level as described in class introMethod Detail |
public boolean acquire(java.lang.Object ownerId, int targetLockLevel, boolean wait, boolean reentrant, long timeoutMSecs) throws java.lang.InterruptedException
MultiLevelLock
acquire
in interface MultiLevelLock
ownerId
- a unique id identifying the entity that wants to acquire a certain lock level on this locktargetLockLevel
- the lock level to acquirewait
- true
if this method shall block when the desired lock level can not be acquiredreentrant
- true
if lock levels of the same entity acquired earlier
should not restrict compatibility with the lock level desired nowtimeoutMSecs
- if blocking is enabled by the wait
parameter this specifies the maximum wait time in milliseconds
true
if the lock actually was acquired
java.lang.InterruptedException
- when the thread waiting on this method is interruptedpublic void release(java.lang.Object ownerId)
MultiLevelLock
release
in interface MultiLevelLock
ownerId
- a unique id identifying the entity that wants to release all lock levelspublic int getLockLevel(java.lang.Object ownerId)
MultiLevelLock
0
if it holds no locks at all.
getLockLevel
in interface MultiLevelLock
ownerId
- a unique id identifying the entity that wants to know its highest lock level
public java.lang.Object getResourceId()
public int getLevelMinLock()
public int getLevelMaxLock()
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)
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)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |