com.sleepycat.je.latch
Class SharedLatchImpl

java.lang.Object
  extended by com.sleepycat.je.latch.SharedLatchImpl
All Implemented Interfaces:
SharedLatch

public class SharedLatchImpl
extends Object
implements SharedLatch

Simple thread-based non-transactional reader-writer/shared-exclusive latch. Latches provide simple exclusive or shared transient locks on objects. Latches are expected to be held for short, defined periods of time. No deadlock detection is provided so it is the caller's responsibility to sequence latch acquisition in an ordered fashion to avoid deadlocks. Nested latches for a single thread are supported, but upgrading a shared latch to an exclusive latch is not. This implementation is based on the section Reader-Writer Locks in the book Java Threads by Scott Oaks, 2nd Edition, Chapter 8. This SharedLatch implementation is only used when Java 5 ReentrantReadWriteLocks are not available.


Constructor Summary
SharedLatchImpl(String name, EnvironmentImpl env)
          Create a shared latch.
 
Method Summary
 void acquireExclusive()
          Acquire a latch for exclusive/write access.
 boolean acquireExclusiveNoWait()
          Probe a latch for exclusive access, but don't block if it's not available.
 void acquireShared()
          Acquire a latch for shared/read access.
 boolean isOwner()
          Return true if this thread is an owner, reader, or write.
 boolean isWriteLockedByCurrentThread()
           
 void release()
          Release an exclusive or shared latch.
 void releaseIfOwner()
          Release the latch.
 void setExclusiveOnly(boolean exclusiveOnly)
          Indicate whether this latch can only be set exclusively (not shared).
 void setName(String name)
          Set the latch name, used for latches in objects instantiated from the log.
 boolean setNoteLatch(boolean noteLatch)
          If noteLatch is true, then track latch usage in the latchTable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SharedLatchImpl

public SharedLatchImpl(String name,
                       EnvironmentImpl env)
Create a shared latch.

Method Detail

setExclusiveOnly

public void setExclusiveOnly(boolean exclusiveOnly)
Indicate whether this latch can only be set exclusively (not shared).

Specified by:
setExclusiveOnly in interface SharedLatch

setName

public void setName(String name)
Set the latch name, used for latches in objects instantiated from the log.

Specified by:
setName in interface SharedLatch

setNoteLatch

public boolean setNoteLatch(boolean noteLatch)
If noteLatch is true, then track latch usage in the latchTable. Return true so this can be called under an assert.

Specified by:
setNoteLatch in interface SharedLatch

acquireExclusive

public void acquireExclusive()
                      throws DatabaseException
Acquire a latch for exclusive/write access. Nesting is allowed, that is, the latch may be acquired more than once by the same thread for exclusive access. However, if the thread already holds the latch for shared access, it cannot be upgraded and LatchException will be thrown. Wait for the latch if some other thread is holding it. If there are threads waiting for access, they will be granted the latch on a FIFO basis. When the method returns, the latch is held for exclusive access.

Specified by:
acquireExclusive in interface SharedLatch
Throws:
LatchException - if the latch is already held by the current thread for shared access.
RunRecoveryException - if an InterruptedException exception occurs.
DatabaseException

acquireExclusiveNoWait

public boolean acquireExclusiveNoWait()
                               throws DatabaseException
Description copied from interface: SharedLatch
Probe a latch for exclusive access, but don't block if it's not available.

Specified by:
acquireExclusiveNoWait in interface SharedLatch
Returns:
true if the latch was acquired, false if it is not available.
Throws:
LatchException - if the latch is already held by the calling thread.
DatabaseException

acquireShared

public void acquireShared()
                   throws DatabaseException
Acquire a latch for shared/read access. Nesting is allowed, that is, the latch may be acquired more than once by the same thread.

Specified by:
acquireShared in interface SharedLatch
Throws:
RunRecoveryException - if an InterruptedException exception occurs.
DatabaseException

isOwner

public boolean isOwner()
Description copied from interface: SharedLatch
Return true if this thread is an owner, reader, or write.

Specified by:
isOwner in interface SharedLatch

release

public void release()
             throws LatchNotHeldException
Release an exclusive or shared latch. If there are other thread(s) waiting for the latch, they are woken up and granted the latch.

Specified by:
release in interface SharedLatch
Throws:
LatchNotHeldException

releaseIfOwner

public void releaseIfOwner()
                    throws LatchNotHeldException
Description copied from interface: SharedLatch
Release the latch. If there are other thread(s) waiting for the latch, one is woken up and granted the latch. If the latch was not owned by the caller, just return.

Specified by:
releaseIfOwner in interface SharedLatch
Throws:
LatchNotHeldException

isWriteLockedByCurrentThread

public boolean isWriteLockedByCurrentThread()
Specified by:
isWriteLockedByCurrentThread in interface SharedLatch


Copyright 2004-2006 Sleepycat, Inc. All Rights Reserved.