com.faunos.util.cc
Class LockStack

java.lang.Object
  extended by com.faunos.util.cc.LockStack

public class LockStack
extends Object

Utility for lock acquisition and unwinding. Mostly, this has to do with the fact that exception-safe code in Java ends up with a clutter of try/finally clauses. (Language envy: C++ shines here..)

Example usage

      LockStack locks = new LockStack();
      try {
          Lock a = ..
          locks.lock(a);
          Lock b = ..
          if (!locks.tryLock(b))
              return;
          Lock c = ..
          Lock d = ..
          if (!locks.tryLock(c))
              locks.lock(d);
          // do some work
          ..
      } finally {
          locks.unwindAll();
      }
 

Concurrency characteristics

Instances of this class are not thread-safe, and have undefined behavior if accessed (serially or concurrently) from any thread other than the one in which they are created.

Author:
Babak Farhang

Constructor Summary
LockStack()
           
 
Method Summary
 void lock(Lock lock)
          Acquires the given lock and places it on the stack.
 int size()
          Returns the number of locks held in this stack.
 boolean tryLock(Lock lock)
          Attempts to acquire the given lock and returns the result.
 void unlockFirst()
          Removes the first lock from the bottom of the stack and unlocks it.
 void unlockLast()
          Removes the last lock from the top of the stack and unlocks it.
 void unwind(int count)
          Unwinds (unlocks) the last count locks in reverse of the order in which they were acquired.
 void unwindAll()
          Unwinds (unlocks) all the acquired locks in reverse of the order in which they were acquired.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LockStack

public LockStack()
Method Detail

tryLock

public boolean tryLock(Lock lock)
Attempts to acquire the given lock and returns the result. If successful, the lock is placed on top of the stack.


lock

public void lock(Lock lock)
Acquires the given lock and places it on the stack.


unlockLast

public void unlockLast()
Removes the last lock from the top of the stack and unlocks it.

Throws:
IllegalStateException - if the stack is empty

unlockFirst

public void unlockFirst()
Removes the first lock from the bottom of the stack and unlocks it.

Throws:
IllegalStateException - if the stack is empty

size

public int size()
Returns the number of locks held in this stack.


unwindAll

public void unwindAll()
Unwinds (unlocks) all the acquired locks in reverse of the order in which they were acquired. It's okay to invoke this method even if the stack is empty.

On return, the stack is empty.


unwind

public void unwind(int count)
Unwinds (unlocks) the last count locks in reverse of the order in which they were acquired.

On return, the stack is count locks smaller.

Throws:
IllegalArgumentException - if count > size()


SourceForge.net Logo