org.infinispan.util.concurrent.locks
Class OwnableReentrantLock

java.lang.Object
  extended by java.util.concurrent.locks.AbstractOwnableSynchronizer
      extended by java.util.concurrent.locks.AbstractQueuedSynchronizer
          extended by org.infinispan.util.concurrent.locks.OwnableReentrantLock
All Implemented Interfaces:
Serializable, Lock

@ThreadSafe
public class OwnableReentrantLock
extends AbstractQueuedSynchronizer
implements Lock

A lock that supports reentrancy based on owner (and not on current thread). For this to work, the lock needs to be constructed with a reference to the InvocationContextContainer, so it is able to determine whether the caller's "owner" reference is the current thread or a GlobalTransaction instance.

This makes this lock implementation very closely tied to Infinispan internals, but it provides for a very clean, efficient and moreover familiar interface to work with, since it implements Lock.

For the sake of performance, this lock only supports nonfair queueing.

Since:
4.0
Author:
Manik Surtani (manik@jboss.org)
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.concurrent.locks.AbstractQueuedSynchronizer
AbstractQueuedSynchronizer.ConditionObject
 
Constructor Summary
OwnableReentrantLock(InvocationContextContainer icc)
          Creates a new lock instance.
 
Method Summary
protected  Object currentRequestor()
           
 int getHoldCount()
           
 Object getOwner()
           
protected  boolean isHeldExclusively()
           
 boolean isLocked()
           
 void lock()
           
 void lockInterruptibly()
           
 AbstractQueuedSynchronizer.ConditionObject newCondition()
           
 String toString()
          Returns a string identifying this lock, as well as its lock state.
protected  boolean tryAcquire(int acquires)
           
 boolean tryLock()
           
 boolean tryLock(long time, TimeUnit unit)
           
protected  boolean tryRelease(int releases)
           
 void unlock()
           
 
Methods inherited from class java.util.concurrent.locks.AbstractQueuedSynchronizer
acquire, acquireInterruptibly, acquireShared, acquireSharedInterruptibly, compareAndSetState, getExclusiveQueuedThreads, getFirstQueuedThread, getQueuedThreads, getQueueLength, getSharedQueuedThreads, getState, getWaitingThreads, getWaitQueueLength, hasContended, hasQueuedThreads, hasWaiters, isQueued, owns, release, releaseShared, setState, tryAcquireNanos, tryAcquireShared, tryAcquireSharedNanos, tryReleaseShared
 
Methods inherited from class java.util.concurrent.locks.AbstractOwnableSynchronizer
getExclusiveOwnerThread, setExclusiveOwnerThread
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

OwnableReentrantLock

public OwnableReentrantLock(InvocationContextContainer icc)
Creates a new lock instance.

Parameters:
icc - InvocationContextContainer instance to consult for the invocation context of the call.
Method Detail

currentRequestor

protected final Object currentRequestor()
Returns:
a GlobalTransaction instance if the current call is participating in a transaction, or the current thread otherwise.

lock

public void lock()
Specified by:
lock in interface Lock

lockInterruptibly

public void lockInterruptibly()
                       throws InterruptedException
Specified by:
lockInterruptibly in interface Lock
Throws:
InterruptedException

tryLock

public boolean tryLock()
Specified by:
tryLock in interface Lock

tryLock

public boolean tryLock(long time,
                       TimeUnit unit)
                throws InterruptedException
Specified by:
tryLock in interface Lock
Throws:
InterruptedException

unlock

public void unlock()
Specified by:
unlock in interface Lock

newCondition

public AbstractQueuedSynchronizer.ConditionObject newCondition()
Specified by:
newCondition in interface Lock

tryAcquire

protected final boolean tryAcquire(int acquires)
Overrides:
tryAcquire in class AbstractQueuedSynchronizer

tryRelease

protected final boolean tryRelease(int releases)
Overrides:
tryRelease in class AbstractQueuedSynchronizer

isHeldExclusively

protected final boolean isHeldExclusively()
Overrides:
isHeldExclusively in class AbstractQueuedSynchronizer

getOwner

public final Object getOwner()
Returns:
the owner of the lock, or null if it is currently unlocked.

getHoldCount

public final int getHoldCount()
Returns:
the hold count of the current lock, or 0 if it is not locked.

isLocked

public final boolean isLocked()
Returns:
true if the lock is locked, false otherwise

toString

public String toString()
Returns a string identifying this lock, as well as its lock state. The state, in brackets, includes either the String "Unlocked" or the String "Locked by" followed by the String representation of the lock owner.

Overrides:
toString in class AbstractQueuedSynchronizer
Returns:
a string identifying this lock, as well as its lock state.

-->

Copyright © 2011 JBoss, a division of Red Hat. All Rights Reserved.