org.exist.storage.lock
Class MultiReadReentrantLock

java.lang.Object
  extended by org.exist.storage.lock.MultiReadReentrantLock
All Implemented Interfaces:
Lock

public class MultiReadReentrantLock
extends Object
implements Lock

A reentrant read/write lock, which allows multiple readers to acquire a lock. Waiting writers are preferred.

This is an adapted and bug-fixed version of code taken from Apache's Turbine JCS.


Field Summary
 
Fields inherited from interface org.exist.storage.lock.Lock
NO_LOCK, READ_LOCK, WRITE_LOCK
 
Constructor Summary
MultiReadReentrantLock(Object id)
          Default constructor.
 
Method Summary
 boolean acquire()
          Acquire a lock for read.
 boolean acquire(int mode)
          Acquire a lock for read or write.
 boolean attempt(int mode)
          Attempt to acquire a lock for read or write.
 String getId()
           
 LockInfo getLockInfo()
          Create a LockInfo entry for the given lock.
 Thread getWriteLockedThread()
           
 boolean hasLock()
          Check if the lock is currently locked by someone.
 boolean hasLock(Thread owner)
          Check if the specified thread holds either a write or a read lock on the resource.
 boolean isLockedForRead(Thread owner)
          Check if the specified thread does currently hold a read lock.
 boolean isLockedForWrite()
          Returns true if there are active or pending write locks.
 void release()
           
 void release(int mode)
          Release a lock of the specified type.
 void release(int mode, int count)
           
 void wakeUp()
          Wake up waiting threads and recompute dependencies.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultiReadReentrantLock

public MultiReadReentrantLock(Object id)
Default constructor.

Method Detail

getId

public String getId()
Specified by:
getId in interface Lock

acquire

public boolean acquire()
                throws LockException
Description copied from interface: Lock
Acquire a lock for read.

Specified by:
acquire in interface Lock
Throws:
LockException

acquire

public boolean acquire(int mode)
                throws LockException
Description copied from interface: Lock
Acquire a lock for read or write. mode is one of Lock.READ_LOCK or Lock.WRITE_LOCK.

Specified by:
acquire in interface Lock
Throws:
LockException

attempt

public boolean attempt(int mode)
Description copied from interface: Lock
Attempt to acquire a lock for read or write. This method will fail immediately if the lock cannot be acquired.

Specified by:
attempt in interface Lock

release

public void release()

release

public void release(int mode)
Description copied from interface: Lock
Release a lock of the specified type.

Specified by:
release in interface Lock

release

public void release(int mode,
                    int count)
Specified by:
release in interface Lock

isLockedForWrite

public boolean isLockedForWrite()
Description copied from interface: Lock
Returns true if there are active or pending write locks.

Specified by:
isLockedForWrite in interface Lock

hasLock

public boolean hasLock()
Description copied from interface: Lock
Check if the lock is currently locked by someone.

Specified by:
hasLock in interface Lock
Returns:
true if there's an active read or write lock

isLockedForRead

public boolean isLockedForRead(Thread owner)
Description copied from interface: Lock
Check if the specified thread does currently hold a read lock.

Specified by:
isLockedForRead in interface Lock
Parameters:
owner - the thread to search for
Returns:
true if the thread holds a read lock

getWriteLockedThread

public Thread getWriteLockedThread()

hasLock

public boolean hasLock(Thread owner)
Check if the specified thread holds either a write or a read lock on the resource.

Specified by:
hasLock in interface Lock
Parameters:
owner - the thread
Returns:
true if owner has a lock

wakeUp

public void wakeUp()
Description copied from interface: Lock
Wake up waiting threads and recompute dependencies. Currently used to rerun deadlock detection.

Specified by:
wakeUp in interface Lock

getLockInfo

public LockInfo getLockInfo()
Description copied from interface: Lock
Create a LockInfo entry for the given lock.

Specified by:
getLockInfo in interface Lock
Returns:
the lock info


Copyright (C) Wolfgang Meier. All rights reserved.