com.sleepycat.je.latch
Class LatchImpl

java.lang.Object
  extended by com.sleepycat.je.latch.LatchImpl
All Implemented Interfaces:
Latch

public class LatchImpl
extends java.lang.Object
implements Latch

This implementation is used in non-Java5 JVMs. In Java5 JVMs, the Java5LockWrapperImpl class is used. The switch hitting is performed in LatchSupport. Simple thread-based non-transactional exclusive non-nestable latch.

Latches provide simple exclusive 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.

A latch can be acquire in wait or no-wait modes. In the former, the caller will wait for any conflicting holders to release the latch. In the latter, if the latch is not available, control returns to the caller immediately.


Constructor Summary
LatchImpl(EnvironmentImpl env)
          Create a latch with no name, more optimal for shortlived latches.
LatchImpl(java.lang.String name, EnvironmentImpl env)
          Create a latch.
 
Method Summary
 void acquire()
          Acquire a latch for exclusive/write access.
 boolean acquireNoWait()
          Acquire a latch for exclusive/write access, but do not block if it's not available.
 LatchStats getLatchStats()
           
 boolean isOwner()
          Return true if the current thread holds this latch.
 int nWaiters()
          Return the number of threads waiting.
 java.lang.Thread owner()
          Used only for unit tests.
 void release()
          Release the latch.
 void releaseIfOwner()
          Release the latch.
 void setName(java.lang.String name)
          Set the latch name, used for latches in objects instantiated from the log.
 java.lang.String toString()
          Formats a latch owner and waiters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LatchImpl

public LatchImpl(java.lang.String name,
                 EnvironmentImpl env)
Create a latch.


LatchImpl

public LatchImpl(EnvironmentImpl env)
Create a latch with no name, more optimal for shortlived latches.

Method Detail

setName

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

Specified by:
setName in interface Latch

acquire

public void acquire()
             throws DatabaseException
Acquire a latch for exclusive/write access.

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:
acquire in interface Latch
Throws:
LatchException - if the latch is already held by the calling thread.
RunRecoveryException - if an InterruptedException exception occurs.
DatabaseException

acquireNoWait

public boolean acquireNoWait()
                      throws LatchException
Acquire a latch for exclusive/write access, but do not block if it's not available.

Specified by:
acquireNoWait in interface Latch
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.

releaseIfOwner

public void releaseIfOwner()
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 Latch

release

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

Specified by:
release in interface Latch
Throws:
LatchNotHeldException - if the latch is not currently held.

isOwner

public boolean isOwner()
Return true if the current thread holds this latch.

Specified by:
isOwner in interface Latch
Returns:
true if we hold this latch. False otherwise.

owner

public java.lang.Thread owner()
Used only for unit tests.

Specified by:
owner in interface Latch
Returns:
the thread that currently holds the latch for exclusive access.

nWaiters

public int nWaiters()
Return the number of threads waiting.

Specified by:
nWaiters in interface Latch
Returns:
the number of threads waiting for the latch.

getLatchStats

public LatchStats getLatchStats()
Specified by:
getLatchStats in interface Latch
Returns:
a LatchStats object with information about this latch.

toString

public java.lang.String toString()
Formats a latch owner and waiters.

Specified by:
toString in interface Latch
Overrides:
toString in class java.lang.Object


Copyright 2004,2008 Oracle. All rights reserved.