/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed 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 com.intellij.util.concurrency;

import com.intellij.openapi.progress.ProcessCanceledException;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;

public class Semaphore {
  private static final class Sync extends AbstractQueuedSynchronizer {
    public int tryAcquireShared(int acquires) {
      return getState() == 0 ? 1 : -1;
    }

    public boolean tryReleaseShared(int releases) {
      // Decrement count; signal when transition to zero
      while (true) {
        int c = getState();
        if (c == 0) return false;
        int nextc = c - 1;
        if (compareAndSetState(c, nextc)) return nextc == 0;
      }
    }

    final void down() {
      while (true) {
        int current = getState();
        int next = current + 1;
        if (compareAndSetState(current, next)) return;
      }
    }
  }

  private final Sync sync = new Sync();

  public boolean up() {
    return sync.releaseShared(1);
  }

  public void down() {
    sync.down();
  }

  public void waitFor() {
    try {
      sync.acquireSharedInterruptibly(1);
    }
    catch (InterruptedException e) {
      throw new ProcessCanceledException(e);
    }
  }

  public boolean waitFor(final long timeout)  {
    try {
      if (sync.tryAcquireShared(1) >= 0) return true;

      return sync.tryAcquireSharedNanos(1, TimeUnit.MILLISECONDS.toNanos(timeout));
    }
    catch (InterruptedException e) {
      throw new ProcessCanceledException(e);
    }
  }

}

@@ -1664,41 +1664,22 @@
     
-sync.acquireSharedInterruptibly(1
+waitForUnsafe(
 );%0A 
@@ -1789,59 +1789,373 @@
 lic 
-boolean waitFor(final long timeout)  %7B%0A    try %7B%0A  
+void waitForUnsafe() throws InterruptedException %7B%0A    sync.acquireSharedInterruptibly(1);%0A  %7D%0A%0A  public boolean waitFor(final long timeout)  %7B%0A    try %7B%0A      return waitForUnsafe(timeout);%0A    %7D%0A    catch (InterruptedException e) %7B%0A      throw new ProcessCanceledException(e);%0A    %7D%0A  %7D%0A%0A  public boolean waitForUnsafe(long timeout) throws InterruptedException %7B%0A
     
@@ -2202,19 +2202,16 @@
 n true;%0A
-%0A  
     retu
@@ -2288,102 +2288,8 @@
 ));%0A
-    %7D%0A    catch (InterruptedException e) %7B%0A      throw new ProcessCanceledException(e);%0A    %7D%0A
   %7D%0A

target output