org.apache.labs.bananadb.store.lock
Class Lock

java.lang.Object
  extended by org.apache.labs.bananadb.store.lock.Lock

public abstract class Lock
extends java.lang.Object

An interprocess mutex lock.

Typical use might look like:

 new Lock.With(directory.makeLock("my.lock")) {
     public Object doBody() {
       ... code to execute while locked ...
     }
   }.run();
 


Nested Class Summary
static class Lock.With<T>
          Utility class for executing code with exclusive access.
 
Field Summary
protected  java.lang.Throwable failureReason
          If a lock obtain called, this failureReason may be set with the "root cause" Exception as to why the lock was not obtained.
static long LOCK_OBTAIN_WAIT_FOREVER
          Pass this value to obtain(long) to try forever to obtain the lock.
static long LOCK_POLL_INTERVAL
          How long obtain(long) waits, in milliseconds, in between attempts to acquire the lock.
 
Constructor Summary
Lock()
           
 
Method Summary
protected abstract  boolean doObtain()
           
abstract  boolean doRelease()
          Releases exclusive access.
 java.util.Set<LockListener> getListeners()
           
abstract  boolean isLocked()
          Returns true if the resource is currently locked.
 boolean obtain()
          Attempts to obtain exclusive access and immediately return upon success or failure.
 boolean obtain(long lockWaitTimeout)
          Attempts to obtain an exclusive lock within amount of time given.
 void release()
          Releases exclusive access.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCK_POLL_INTERVAL

public static long LOCK_POLL_INTERVAL
How long obtain(long) waits, in milliseconds, in between attempts to acquire the lock.


LOCK_OBTAIN_WAIT_FOREVER

public static final long LOCK_OBTAIN_WAIT_FOREVER
Pass this value to obtain(long) to try forever to obtain the lock.

See Also:
Constant Field Values

failureReason

protected java.lang.Throwable failureReason
If a lock obtain called, this failureReason may be set with the "root cause" Exception as to why the lock was not obtained.

Constructor Detail

Lock

public Lock()
Method Detail

getListeners

public java.util.Set<LockListener> getListeners()

obtain

public final boolean obtain()
                     throws java.io.IOException
Attempts to obtain exclusive access and immediately return upon success or failure.

Returns:
true iff exclusive access is obtained
Throws:
java.io.IOException

doObtain

protected abstract boolean doObtain()
                             throws java.io.IOException
Throws:
java.io.IOException

obtain

public boolean obtain(long lockWaitTimeout)
               throws LockObtainFailedException,
                      java.io.IOException
Attempts to obtain an exclusive lock within amount of time given. Polls once per LOCK_POLL_INTERVAL (currently 1000) milliseconds until lockWaitTimeout is passed.

Parameters:
lockWaitTimeout - length of time to wait in milliseconds or LOCK_OBTAIN_WAIT_FOREVER to retry forever
Returns:
true if lock was obtained
Throws:
LockObtainFailedException - if lock wait times out
java.lang.IllegalArgumentException - if lockWaitTimeout is out of bounds
java.io.IOException - if obtain() throws IOException

release

public final void release()
                   throws java.io.IOException
Releases exclusive access.

Throws:
java.io.IOException

doRelease

public abstract boolean doRelease()
                           throws java.io.IOException
Releases exclusive access.

Throws:
java.io.IOException

isLocked

public abstract boolean isLocked()
Returns true if the resource is currently locked. Note that one must still call obtain() before using the resource.



Copyright © 2009 Apache Software Foundation. All Rights Reserved.