Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * JBoss, Home of Professional Open Source
   * Copyright 2009 Red Hat Inc. and/or its affiliates and other
   * contributors as indicated by the @author tags. All rights reserved.
   * See the copyright.txt in the distribution for a full listing of
   * individual contributors.
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site:
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 org.infinispan.context.InvocationContextContainer, so it is able to determine whether the caller's "owner" reference is the current thread or a org.infinispan.transaction.xa.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 java.util.concurrent.locks.Lock.

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

Manik Surtani (
Stuart Douglas
 public class OwnableReentrantLock extends AbstractQueuedSynchronizer implements Lock {
     private static final long serialVersionUID = 493297473462848792L;
     private final ThreadLocalStack currentRequestor = new ThreadLocalStack();

Current owner
     private transient Object owner;

Creates a new lock instance.
     public OwnableReentrantLock() {


a GlobalTransaction instance if the current call is participating in a transaction, or the current thread otherwise.
     protected Object currentRequestor() {
         return .get();
     public void pushOwner(Object owner) {
     public void popOwner() {
     public void lock() {
         if (compareAndSetState(0, 1))
              = currentRequestor();
     public void lockInterruptibly() throws InterruptedException {
     public boolean tryLock() {
         return tryAcquire(1);
     public boolean tryLock(long timeTimeUnit unitthrows InterruptedException {
         return tryAcquireNanos(1, unit.toNanos(time));
    public void unlock() {
        try {
        } catch (IllegalMonitorStateException imse) {
            // ignore?
    public ConditionObject newCondition() {
        throw new UnsupportedOperationException("Not supported in this implementation!");
    protected final boolean tryAcquire(int acquires) {
        final Object current = currentRequestor();
        int c = getState();
        if (c == 0) {
            if (compareAndSetState(0, acquires)) {
                 = current;
                return true;
        } else if (current.equals()) {
            setState(c + acquires);
            return true;
        return false;
    protected final boolean tryRelease(int releases) {
        int c = getState() - releases;
        if (!currentRequestor().equals()) {
            //throw new IllegalMonitorStateException(this.toString());
            // lets be quiet about this
            return false;
        boolean free = false;
        if (c == 0) {
            free = true;
             = null;
        return free;
    protected final boolean isHeldExclusively() {
        return getState() != 0 && currentRequestor().equals();


the owner of the lock, or null if it is currently unlocked.
    public final Object getOwner() {
        int c = getState();
        Object o = ;
        return (c == 0) ? null : o;


the hold count of the current lock, or 0 if it is not locked.
    public final int getHoldCount() {
        int c = getState();
        Object o = ;
        return (currentRequestor().equals(o)) ? c : 0;


true if the lock is locked, false otherwise
    public final boolean isLocked() {
        return getState() != 0;

Reconstitute this lock instance from a stream, resetting the lock to an unlocked state.

s the stream
    private void readObject( s)
            throws {
        setState(0); // reset to unlocked state

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.

a string identifying this lock, as well as its lock state.
    public String toString() {
        Object owner = getOwner();
        return super.toString() + ((owner == null) ?
                "[Unlocked]" :
                "[Locked by " + owner + "]");
New to GrepCode? Check out our FAQ X