/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package tachyon.worker;

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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tachyon.Constants;

/**
 * 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 thread acquiring them.
 */
public class ClientRWLock implements ReadWriteLock {
  private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);

  private static final int MAX_AVAILABLE = 100;
  private final Semaphore mAvailable = new Semaphore(MAX_AVAILABLE, true);

  @Override
  public Lock readLock() {
    return new UserLock(mAvailable, 1);
  }

  @Override
  public Lock writeLock() {
    return new UserLock(mAvailable, MAX_AVAILABLE);
  }

  private class UserLock implements Lock {
    private final int mPermits;
    private Semaphore mSemaphore;

    private UserLock(Semaphore semaphore, int permits) {
      mSemaphore = semaphore;
      mPermits = permits;
    }

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

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

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

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

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

    @Override
    public Condition newCondition() {
      // Not supported
      return null;
    }
  }
}

@@ -1261,22 +1261,19 @@
 rom the 
-thread
+one
  acquiri
@@ -1279,16 +1279,69 @@
 ing them
+ (but supposed to be requested%0A * by the same client)
 .%0A */%0Apu
@@ -1475,16 +1475,100 @@
 _TYPE);%0A
+  /** Total number of permits. This value is the max number of concurrent readers */
 %0A  priva
@@ -1608,16 +1608,46 @@
  = 100;%0A
+  /** Underlying Semaphore */%0A
   privat
@@ -2808,42 +2808,82 @@
     
-// Not supported%0A      return null
+throw new UnsupportedOperationException(%22newCondition() is not supported%22)
 ;%0A  

Update ClientRWLock to address Haoyuan's comments