public void lock(final T tx,final long timeout) throws InterruptedException, DeadlockException {
  if (tx == null)   throw new NullPointerException();
  if (timeout < 0L)   throw new IllegalArgumentException();
  if (DEBUG)   log.debug("enter: tx=" + tx + ", queue="+ this);
  lock.lock();
  if (DEBUG)   log.debug("have private lock: tx=" + tx + ", queue="+ this);
  final long begin=System.currentTimeMillis();
  try {
    assertNotDead();
    if (queue.peek() == tx) {
      if (INFO)       log.info("Already owns lock: tx=" + tx + ", queue="+ this);
      return;
    }
    if (queue.isEmpty()) {
      queue.add(tx);
      if (INFO)       log.info("Granted lock with empty queue: tx=" + tx + ", queue="+ this);
      return;
    }
    if (waitsFor != null) {
      final Object[] predecessors=queue.toArray();
      try {
        waitsFor.addEdges(tx,predecessors);
      }
 catch (      DeadlockException ex) {
        log.warn("Deadlock: tx=" + tx + ", queue="+ this);
        throw ex;
      }
    }
    queue.add(tx);
    try {
      while (true) {
        final long elapsed=System.currentTimeMillis() - begin;
        if (timeout != 0L && elapsed >= timeout) {
          throw new TimeoutException("After " + elapsed + " ms: tx="+ tx+ ", queue="+ this);
        }
        if (INFO)         log.info("Awaiting resource: tx=" + tx + ", queue="+ this);
        final long remaining=timeout - elapsed;
        if (timeout == 0L) {
          available.await();
        }
 else {
          if (!available.await(remaining,TimeUnit.MILLISECONDS)) {
            throw new TimeoutException("After " + elapsed + " ms: tx="+ tx+ ", queue="+ this);
          }
        }
        if (INFO)         log.info("Continuing after wait: tx=" + tx + ", queue="+ this);
        if (dead.get()) {
          throw new InterruptedException("Resource is dead: " + resource);
        }
        if (queue.peek() == tx) {
          if (INFO)           log.info("Lock granted after wait: tx=" + tx + ", queue="+ this);
          return;
        }
      }
    }
 catch (    Throwable t) {
      if (waitsFor != null) {
synchronized (waitsFor) {
          try {
            final boolean waiting=true;
            waitsFor.removeEdges(tx,waiting);
          }
 catch (          Throwable t2) {
            log.warn(t2);
          }
        }
      }
      queue.remove(tx);
      if (t instanceof RuntimeException)       throw (RuntimeException)t;
      if (t instanceof InterruptedException)       throw (InterruptedException)t;
      throw new RuntimeException(t);
    }
  }
  finally {
    lock.unlock();
    if (DEBUG)     log.debug("released private lock: tx=" + tx + ", queue="+ this);
  }
}

public void lock ( final T tx , final long timeout ) throws InterruptedException , DeadlockException { if ( tx == null ) throw new NullPointerException ( ) ; if ( timeout < 0L ) throw new IllegalArgumentException ( ) ; if ( DEBUG ) log . debug ( "enter: tx=" + tx + ", queue=" + this ) ; lock . lock ( ) ; if ( DEBUG ) log . debug ( "have private lock: tx=" + tx + ", queue=" + this ) ; final long begin = System . currentTimeMillis ( ) ; try { assertNotDead ( ) ; if ( queue . peek ( ) == tx ) { if ( INFO ) log . info ( "Already owns lock: tx=" + tx + ", queue=" + this ) ; return ; } if ( queue . isEmpty ( ) ) { queue . add ( tx ) ; if ( INFO ) log . info ( "Granted lock with empty queue: tx=" + tx + ", queue=" + this ) ; return ; } if ( waitsFor != null ) { final Object [ ] predecessors = queue . toArray ( ) ; try { waitsFor . addEdges ( tx , predecessors ) ; } catch ( DeadlockException ex ) { log . warn ( "Deadlock: tx=" + tx + ", queue=" + this ) ; throw ex ; } } queue . add ( tx ) ; try { while ( true ) { final long elapsed = System . currentTimeMillis ( ) - begin ; if ( timeout != 0L && elapsed >= timeout ) { throw new TimeoutException ( "After " + elapsed + " ms: tx=" + tx + ", queue=" + this ) ; } if ( INFO ) log . info ( "Awaiting resource: tx=" + tx + ", queue=" + this ) ; final long remaining = timeout - elapsed ; if ( timeout == 0L ) { available . await ( ) ; } else { if ( ! available . await ( remaining , TimeUnit . MILLISECONDS ) ) { throw new TimeoutException ( "After " + elapsed + " ms: tx=" + tx + ", queue=" + this ) ; } } if ( INFO ) log . info ( "Continuing after wait: tx=" + tx + ", queue=" + this ) ; if ( dead . get ( ) ) { throw new InterruptedException ( "Resource is dead: " + resource ) ; } if ( queue . peek ( ) == tx ) { if ( INFO ) log . info ( "Lock granted after wait: tx=" + tx + ", queue=" + this ) ; return ; } } } catch ( Throwable t ) { if ( waitsFor != null ) { synchronized ( waitsFor ) { try { final boolean waiting = true ; waitsFor . removeEdges ( tx , waiting ) ; } catch ( Throwable t2 ) { log . warn ( t2 ) ; } } } queue . remove ( tx ) ; if ( t instanceof RuntimeException ) throw ( RuntimeException ) t ; if ( t instanceof InterruptedException ) throw ( InterruptedException ) t ; throw new RuntimeException ( t ) ; } } finally { lock . unlock ( ) ; if ( DEBUG ) log . debug ( "released private lock: tx=" + tx + ", queue=" + this ) ; } }
obtain a lock on the resource .
