@@ -0,0 +1,2525 @@
+/*%0A * Licensed to the University of California, Berkeley under one or more contributor license%0A * agreements. See the NOTICE file distributed with this work for additional information regarding%0A * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the%0A * %22License%22); you may not use this file except in compliance with the License. You may obtain a%0A * copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software distributed under the License%0A * is distributed on an %22AS IS%22 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express%0A * or implied. See the License for the specific language governing permissions and limitations under%0A * the License.%0A */%0A%0Apackage tachyon.worker;%0A%0Aimport java.util.concurrent.Semaphore;%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.concurrent.locks.Condition;%0Aimport java.util.concurrent.locks.Lock;%0Aimport java.util.concurrent.locks.ReadWriteLock;%0A%0A/**%0A * Read/write lock associated with clients rather than threads. Either its read lock or write lock%0A * can be released by a thread different from the thread acquiring them.%0A */%0Apublic class ClientRWLock implements ReadWriteLock %7B%0A  private static final int MAX_AVAILABLE = 100;%0A  private final Semaphore mAvailable = new Semaphore(MAX_AVAILABLE, true);%0A%0A  @Override%0A  public Lock readLock() %7B%0A    return new UserLock(mAvailable, 1);%0A  %7D%0A%0A  @Override%0A  public Lock writeLock() %7B%0A    return new UserLock(mAvailable, MAX_AVAILABLE);%0A  %7D%0A%0A  private class UserLock implements Lock %7B%0A    private final int mPermits;%0A    private Semaphore mSemaphore;%0A%0A    private UserLock(Semaphore semaphore, int permits) %7B%0A      mSemaphore = semaphore;%0A      mPermits = permits;%0A    %7D%0A%0A    @Override%0A    public void lock() %7B%0A      mSemaphore.acquireUninterruptibly(mPermits);%0A    %7D%0A%0A    @Override%0A    public void lockInterruptibly() throws InterruptedException %7B%0A      mSemaphore.acquire(mPermits);%0A    %7D%0A%0A    @Override%0A    public boolean tryLock() %7B%0A      return mSemaphore.tryAcquire(mPermits);%0A    %7D%0A%0A    @Override%0A    public boolean tryLock(long time, TimeUnit unit) %7B%0A      try %7B%0A        return mSemaphore.tryAcquire(mPermits, time, unit);%0A      %7D catch (InterruptedException e) %7B%0A        return false;%0A      %7D%0A    %7D%0A%0A    @Override%0A    public void unlock() %7B%0A      mSemaphore.release(mPermits);%0A    %7D%0A%0A    @Override%0A    public Condition newCondition() %7B%0A      // Not supported%0A      return null;%0A    %7D%0A  %7D%0A%7D%0A

Add a ReadWrite lock implementation based on Semaphore. The reason to have this lock is to ensure we can release previously acquired locks from different threads