org.apache.fulcrum.yaafi.framework.locking
Class ReadWriteLock

java.lang.Object
  extended by org.apache.fulcrum.yaafi.framework.locking.GenericLock
      extended by org.apache.fulcrum.yaafi.framework.locking.ReadWriteLock
All Implemented Interfaces:
MultiLevelLock, MultiLevelLock2

public class ReadWriteLock
extends GenericLock

Convenience implementation of a read/write lock based on GenericLock.

Reads are shared which means there can be any number of concurrent read accesses allowed by this lock. Writes are exclusive. This means when there is a write access no other access neither read nor write are allowed by this lock. Additionally, writes are preferred over reads in order to avoid starvation. The idea is that there are many readers, but few writers and if things work out bad the writer would never be served at all. That's why it is preferred.

Calls to both acquireRead(Object, long)and acquireWrite(Object, long)are blocking and reentrant. Blocking means they will wait if they can not acquire the descired access, reentrant means that a lock request by a specific owner will always be compatible with other accesses on this lock by the same owner. E.g. if you already have a lock for writing and you try to acquire write access again you will not be blocked by this first lock, while others of course will be. This is the natural way you already know from Java monitors and synchronized blocks.

Version:
$Revision: 1.1 $
See Also:
GenericLock

Nested Class Summary
 
Nested classes/interfaces inherited from class org.apache.fulcrum.yaafi.framework.locking.GenericLock
GenericLock.LockOwner
 
Field Summary
static int NO_LOCK
           
static int READ_LOCK
           
static int WRITE_LOCK
           
 
Fields inherited from class org.apache.fulcrum.yaafi.framework.locking.GenericLock
logger, owners, resourceId, waiters, waitingOwners
 
Fields inherited from interface org.apache.fulcrum.yaafi.framework.locking.MultiLevelLock2
COMPATIBILITY_NONE, COMPATIBILITY_REENTRANT, COMPATIBILITY_REENTRANT_AND_SUPPORT, COMPATIBILITY_SUPPORT
 
Constructor Summary
ReadWriteLock(java.lang.Object resourceId, LoggerFacade logger)
          Creates a new read/write lock.
 
Method Summary
 boolean acquireRead(java.lang.Object ownerId, long timeoutMSecs)
          Tries to acquire a blocking, reentrant read lock.
 boolean acquireWrite(java.lang.Object ownerId, long timeoutMSecs)
          Tries to acquire a blocking, reentrant write lock.
 
Methods inherited from class org.apache.fulcrum.yaafi.framework.locking.GenericLock
acquire, acquire, acquire, acquire, equals, getConflictingOwners, getConflictingOwners, getConflictingWaiters, getLevelMaxLock, getLevelMinLock, getLockLevel, getMaxLevelOwner, getMaxLevelOwner, getMaxLevelOwner, getMaxLevelOwner, getOwner, getResourceId, has, hashCode, isCompatible, registerWaiter, release, setLockLevel, test, toString, tryLock, tryLock, unregisterWaiter
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

NO_LOCK

public static final int NO_LOCK
See Also:
Constant Field Values

READ_LOCK

public static final int READ_LOCK
See Also:
Constant Field Values

WRITE_LOCK

public static final int WRITE_LOCK
See Also:
Constant Field Values
Constructor Detail

ReadWriteLock

public ReadWriteLock(java.lang.Object resourceId,
                     LoggerFacade logger)
Creates a new read/write lock.

Parameters:
resourceId - identifier for the resource associated to this lock
logger - generic logger used for all kind of debug logging
Method Detail

acquireRead

public boolean acquireRead(java.lang.Object ownerId,
                           long timeoutMSecs)
                    throws java.lang.InterruptedException
Tries to acquire a blocking, reentrant read lock. A read lock is compatible with other read locks, but not with a write lock.

Parameters:
ownerId - a unique id identifying the entity that wants to acquire a certain lock level on this lock
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

acquireWrite

public boolean acquireWrite(java.lang.Object ownerId,
                            long timeoutMSecs)
                     throws java.lang.InterruptedException
Tries to acquire a blocking, reentrant write lock. A write lock is incompatible with any another read or write lock and is thus exclusive.

Parameters:
ownerId - a unique id identifying the entity that wants to acquire a certain lock level on this lock
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


Copyright © 2000-2008 Apache Software Foundation. All Rights Reserved.