Ejemplo n.º 1
0
 @ManagedOperation(description = "Unlocks all currently held locks")
 public void unlockAll() {
   List<ClientLock> lock_list = new ArrayList<ClientLock>();
   Collection<Map<Owner, ClientLock>> maps = client_locks.values();
   for (Map<Owner, ClientLock> map : maps) lock_list.addAll(map.values());
   for (ClientLock lock : lock_list) lock.unlock();
 }
Ejemplo n.º 2
0
    protected void await(boolean throwInterrupt) throws InterruptedException {
      if (!signaled.get()) {
        lock.acquired = false;
        sendAwaitConditionRequest(lock.name, lock.owner);
        boolean interrupted = false;
        while (!signaled.get()) {
          parker.set(Thread.currentThread());
          LockSupport.park(this);

          if (Thread.interrupted()) {
            // If we were interrupted and haven't received a response yet then we try to
            // clean up the lock request and throw the exception
            if (!signaled.get()) {
              sendDeleteAwaitConditionRequest(lock.name, lock.owner);
              throw new InterruptedException();
            }
            // In the case that we were signaled and interrupted
            // we want to return the signal but still interrupt
            // our thread
            interrupted = true;
          }
        }
        if (interrupted) Thread.currentThread().interrupt();
      }

      // We set as if this signal was no released.  This way if the
      // condition is reused again, but the client condition isn't lost
      // we won't think we were signaled immediately
      signaled.set(false);
    }
Ejemplo n.º 3
0
 @Override
 public void awaitUninterruptibly() {
   try {
     await(false);
   } catch (InterruptedException e) {
     // This should never happen
   } finally {
     lock.lock();
   }
 }
Ejemplo n.º 4
0
    protected long await(long nanoSeconds) throws InterruptedException {
      long target_nano = System.nanoTime() + nanoSeconds;

      if (!signaled.get()) {
        // We release the lock at the same time as waiting on the
        // condition
        lock.acquired = false;
        sendAwaitConditionRequest(lock.name, lock.owner);

        boolean interrupted = false;
        while (!signaled.get()) {
          long wait_nano = target_nano - System.nanoTime();
          // If we waited max time break out
          if (wait_nano > 0) {
            parker.set(Thread.currentThread());
            LockSupport.parkNanos(this, wait_nano);

            if (Thread.interrupted()) {
              // If we were interrupted and haven't received a response yet then we try to
              // clean up the lock request and throw the exception
              if (!signaled.get()) {
                sendDeleteAwaitConditionRequest(lock.name, lock.owner);
                throw new InterruptedException();
              }
              // In the case that we were signaled and interrupted
              // we want to return the signal but still interrupt
              // our thread
              interrupted = true;
            }
          } else {
            break;
          }
        }
        if (interrupted) Thread.currentThread().interrupt();
      }

      // We set as if this signal was no released.  This way if the
      // condition is reused again, but the client condition isn't lost
      // we won't think we were signaled immediately
      // If we weren't signaled then delete our request
      if (!signaled.getAndSet(false)) {
        sendDeleteAwaitConditionRequest(lock.name, lock.owner);
      }
      return target_nano - System.nanoTime();
    }
Ejemplo n.º 5
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();
        }
      }
    }
Ejemplo n.º 6
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();
    }
Ejemplo n.º 7
0
 protected void handleLockDeniedResponse(String lock_name, Owner owner) {
   ClientLock lock = getLock(lock_name, owner, false);
   if (lock != null) lock.lockDenied();
 }
Ejemplo n.º 8
0
 protected void handleLockGrantedResponse(String lock_name, Owner owner, Address sender) {
   ClientLock lock = getLock(lock_name, owner, false);
   if (lock != null) lock.handleLockGrantedResponse(owner, sender);
 }
Ejemplo n.º 9
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);
  }