Exemplo n.º 1
0
 @Override
 public void awaitUninterruptibly() {
   try {
     await(false);
   } catch (InterruptedException e) {
     // This should never happen
   } finally {
     lock.lock();
   }
 }
Exemplo n.º 2
0
    @Override
    public void await() throws InterruptedException {
      InterruptedException ex = null;
      try {
        await(true);
      } catch (InterruptedException e) {
        ex = e;
        throw ex;
      } finally {
        lock.lock();

        // If we are throwing an InterruptedException
        // then clear the interrupt state as well.
        if (ex != null) {
          Thread.interrupted();
        }
      }
    }
Exemplo n.º 3
0
    @Override
    public long awaitNanos(long nanosTimeout) throws InterruptedException {
      long beforeLock;
      InterruptedException ex = null;
      try {
        beforeLock = await(nanosTimeout) + System.nanoTime();
      } catch (InterruptedException e) {
        ex = e;
        throw ex;
      } finally {
        lock.lock();

        // If we are throwing an InterruptedException
        // then clear the interrupt state as well.
        if (ex != null) {
          Thread.interrupted();
        }
      }

      return beforeLock - System.nanoTime();
    }
Exemplo n.º 4
0
  public Object down(Event evt) {
    switch (evt.getType()) {
      case Event.LOCK:
        LockInfo info = (LockInfo) evt.getArg();
        ClientLock lock = getLock(info.getName());
        if (!info.isTrylock()) {
          if (info.isLockInterruptibly()) {
            try {
              lock.lockInterruptibly();
            } catch (InterruptedException e) {
              Thread.currentThread()
                  .interrupt(); // has to be checked by caller who has to rethrow ...
            }
          } else lock.lock();
        } else {
          if (info.isUseTimeout()) {
            try {
              return lock.tryLock(info.getTimeout(), info.getTimeUnit());
            } catch (InterruptedException e) {
              Thread.currentThread().interrupt();
            }
          } else {
            return lock.tryLock();
          }
        }
        return null;

      case Event.UNLOCK:
        info = (LockInfo) evt.getArg();
        lock = getLock(info.getName(), false);
        if (lock != null) lock.unlock();
        return null;

      case Event.UNLOCK_ALL:
        unlockAll();
        return null;
      case Event.LOCK_AWAIT:
        info = (LockInfo) evt.getArg();
        lock = getLock(info.getName(), false);
        if (lock == null || !lock.acquired) {
          throw new IllegalMonitorStateException();
        }
        Condition condition = lock.newCondition();
        if (info.isUseTimeout()) {
          try {
            return condition.awaitNanos(info.getTimeUnit().toNanos(info.getTimeout()));
          } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
          }
        } else if (info.isLockInterruptibly()) {
          try {
            condition.await();
          } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
          }
        } else {
          condition.awaitUninterruptibly();
        }
        break;
      case Event.LOCK_SIGNAL:
        AwaitInfo awaitInfo = (AwaitInfo) evt.getArg();
        lock = getLock(awaitInfo.getName(), false);
        if (lock == null || !lock.acquired) {
          throw new IllegalMonitorStateException();
        }
        sendSignalConditionRequest(awaitInfo.getName(), awaitInfo.isAll());
        break;
      case Event.SET_LOCAL_ADDRESS:
        local_addr = (Address) evt.getArg();
        break;

      case Event.VIEW_CHANGE:
        handleView((View) evt.getArg());
        break;
    }
    return down_prot.down(evt);
  }