/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the “License”). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.worker.block;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;

import javax.annotation.concurrent.ThreadSafe;

/**
 * Read/write lock associated with clients rather than threads. Either its read lock or write lock
 * can be released by a thread different from the one acquiring them (but supposed to be requested
 * by the same client).
 */
@ThreadSafe
public final class ClientRWLock implements ReadWriteLock {
  // TODO(bin): Make this const a configurable.
  /** Total number of permits. This value decides the max number of concurrent readers. */
  private static final int MAX_AVAILABLE = 100;
  /** Underlying Semaphore. */
  private final Semaphore mAvailable = new Semaphore(MAX_AVAILABLE, true);
  /** Reference count. */
  private Integer mReferences = 0;

  @Override
  public Lock readLock() {
    return new SessionLock(1);
  }

  @Override
  public Lock writeLock() {
    return new SessionLock(MAX_AVAILABLE);
  }

  /**
   * Increments the reference count.
   */
  public void addReference() {
    synchronized (mReferences) {
      mReferences++;
    }
  }

  /**
   * Decrements the reference count.
   *
   * @return the new reference count
   */
  public Integer dropReference() {
    synchronized (mReferences) {
      mReferences--;
      return mReferences;
    }
  }

  private final class SessionLock implements Lock {
    private final int mPermits;

    private SessionLock(int permits) {
      mPermits = permits;
    }

    @Override
    public void lock() {
      mAvailable.acquireUninterruptibly(mPermits);
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {
      mAvailable.acquire(mPermits);
    }

    @Override
    public boolean tryLock() {
      return mAvailable.tryAcquire(mPermits);
    }

    @Override
    public boolean tryLock(long time, TimeUnit unit) {
      try {
        return mAvailable.tryAcquire(mPermits, time, unit);
      } catch (InterruptedException e) {
        return false;
      }
    }

    @Override
    public void unlock() {
      mAvailable.release(mPermits);
    }

    @Override
    public Condition newCondition() {
      throw new UnsupportedOperationException("newCondition() is not supported");
    }
  }
}

@@ -615,16 +615,66 @@
 meUnit;%0A
+import java.util.concurrent.atomic.AtomicInteger;%0A
 import j
@@ -1478,16 +1478,22 @@
 private 
+Atomic
 Integer 
@@ -1506,17 +1506,35 @@
 ences = 
-0
+new AtomicInteger()
 ;%0A%0A  @Ov
@@ -1779,63 +1779,38 @@
     
-synchronized (mReferences) %7B%0A      mReferences++;%0A    %7D
+mReferences.incrementAndGet();
 %0A  %7D
@@ -1945,89 +1945,45 @@
     
-synchronized (mReferences) %7B%0A      mReferences--;%0A      return mReferences;%0A    %7D
+return mReferences.decrementAndGet();
 %0A  %7D

Use AtomicInteger instead of synchronizing around an Integer