private long getRemainingTime(long startTime, long time) {
      long timeout = time - (System.nanoTime() - startTime);
      return (timeout < 0) ? 0 : timeout;
    }
Combination of global semaphore and session reentrant lock.
ql/src/java/org/apache/hadoop/hive/ql/lock/CompileLockFactory.java