private boolean _suspend(long time, TimeUnit unit, boolean failOnError)
      throws IllegalStateException {
    boolean suspendSuccessful = false;

    try {
      executionStateMonitor.enter();
      switch (executionState) {
        case RESUMED:
          break;
        case SUSPENDED:
        case CANCELLED:
          if (failOnError) {
            throw new IllegalStateException(
                LocalizationMessages.ILLEGAL_INVOCATION_CONTEXT_STATE(executionState, "suspend"));
          }
          break;
        case RUNNING:
          executionState = State.SUSPENDED;
          suspendSuccessful = true;
      }
    } finally {
      executionStateMonitor.leave();
    }

    // we don't want to invoke the callback or log message
    // as part of the synchronized code
    if (suspendSuccessful) {
      callback.suspended(time, unit, this);
    } else {
      // already resumed - just log fine message & ignore the call
      LOGGER.log(Level.FINE, LocalizationMessages.REQUEST_SUSPEND_FAILED(executionState));
    }

    return suspendSuccessful;
  }
Esempio n. 2
0
 public void unlock(Object name) {
   Monitor lock;
   synchronized (stringLocks) {
     lock = stringLocks.getUnchecked(name);
   }
   lock.leave();
 }
  /**
   * Invoke request on the wrapped inflector. The adapter itself serves as a {@link ListenableFuture
   * listenable response future}.
   *
   * @param request request data to be processed.
   */
  public ListenableFuture<RESPONSE> apply(REQUEST request) {
    originatingRequest.set(request);
    final RESPONSE response;

    try {
      response = wrapped.apply(request);

      if (executionStateMonitor.enterIf(runningState)) {
        // The context was not suspended during the call to the wrapped inflector's apply(...)
        // method;
        // mark as resumed & don't invoke callback.resume() since we are resuming synchronously
        try {
          executionState = State.RESUMED;
          set(response);
        } finally {
          executionStateMonitor.leave();
        }
      } else if (response != null) {
        LOGGER.log(Level.FINE, LocalizationMessages.REQUEST_SUSPENDED_RESPONSE_IGNORED(response));
      }
    } catch (Throwable t) {
      resume(t); // resume with throwable (if not resumed by an external event already)
    }
    return this;
  }
Esempio n. 4
0
 public CountingReference<V> acquire(V val) {
   monitor.enter();
   try {
     return intern(val).increment();
   } finally {
     monitor.leave();
   }
 }
Esempio n. 5
0
 public boolean isShutdown() {
   monitor.enter();
   try {
     return isShutdown;
   } finally {
     monitor.leave();
   }
 }
Esempio n. 6
0
 public boolean awaitTermination(long time, TimeUnit unit) throws InterruptedException {
   monitor.enter();
   try {
     return monitor.waitFor(isTerminated, time, unit);
   } finally {
     monitor.leave();
   }
 }
Esempio n. 7
0
 public void shutdown() {
   monitor.enter();
   try {
     isShutdown = true;
   } finally {
     monitor.leave();
   }
 }
Esempio n. 8
0
 public void decrement() {
   monitor.enter();
   try {
     count--;
   } finally {
     monitor.leave();
   }
 }
Esempio n. 9
0
 public void release(V val) {
   monitor.enter();
   try {
     intern(val).decrement();
   } finally {
     monitor.leave();
   }
 }
Esempio n. 10
0
 private void enterSatisfyGuardAndLeaveInCurrentThread() {
   monitor.enter();
   try {
     guard.setSatisfied(true);
   } finally {
     monitor.leave();
   }
 }
 @Override
 public State state() {
   executionStateMonitor.enter();
   try {
     return executionState;
   } finally {
     executionStateMonitor.leave();
   }
 }
  @Override
  public boolean isSuspended() {
    try {
      executionStateMonitor.enter();

      return executionState == State.SUSPENDED;
    } finally {
      executionStateMonitor.leave();
    }
  }
Esempio n. 13
0
 public boolean increment() {
   monitor.enter();
   try {
     if (isShutdown) {
       return false;
     } else {
       count++;
       return true;
     }
   } finally {
     monitor.leave();
   }
 }
 @Override
 public void cancel() {
   if (executionStateMonitor.enterIf(cancellableState)) {
     try {
       executionState = State.CANCELLED;
     } finally {
       executionStateMonitor.leave();
     }
     super.cancel(true);
   } else {
     // just log fine message & ignore the call
     LOGGER.log(Level.FINE, LocalizationMessages.REQUEST_CANCEL_FAILED(executionState));
   }
 }
 private void resume(final Runnable resumeHandler) {
   if (executionStateMonitor.enterIf(resumableState)) {
     final boolean invokeCallback;
     try {
       invokeCallback = executionState == State.SUSPENDED;
       executionState = State.RESUMED;
     } finally {
       executionStateMonitor.leave();
     }
     try {
       resumeHandler.run();
     } finally {
       if (invokeCallback) {
         callback.resumed();
       }
     }
   } else {
     throw new IllegalStateException(
         LocalizationMessages.ILLEGAL_INVOCATION_CONTEXT_STATE(executionState, "resume"));
   }
 }
Esempio n. 16
0
  private void runEnterTest() {
    assertFalse(Thread.currentThread().isInterrupted());
    assertFalse(monitor.isOccupiedByCurrentThread());

    doEnterScenarioSetUp();

    boolean interruptedBeforeCall = Thread.currentThread().isInterrupted();
    Outcome actualOutcome = doCall();
    boolean occupiedAfterCall = monitor.isOccupiedByCurrentThread();
    boolean interruptedAfterCall = Thread.currentThread().isInterrupted();

    if (occupiedAfterCall) {
      guard.setSatisfied(true);
      monitor.leave();
      assertFalse(monitor.isOccupiedByCurrentThread());
    }

    assertEquals(expectedOutcome, actualOutcome);
    assertEquals(expectedOutcome == Outcome.SUCCESS, occupiedAfterCall);
    assertEquals(
        interruptedBeforeCall && expectedOutcome != Outcome.INTERRUPT, interruptedAfterCall);
  }
Esempio n. 17
0
  private void runWaitTest() {
    assertFalse(Thread.currentThread().isInterrupted());
    assertFalse(monitor.isOccupiedByCurrentThread());
    monitor.enter();
    try {
      assertTrue(monitor.isOccupiedByCurrentThread());

      doWaitScenarioSetUp();

      boolean interruptedBeforeCall = Thread.currentThread().isInterrupted();
      Outcome actualOutcome = doCall();
      boolean occupiedAfterCall = monitor.isOccupiedByCurrentThread();
      boolean interruptedAfterCall = Thread.currentThread().isInterrupted();

      assertEquals(expectedOutcome, actualOutcome);
      assertTrue(occupiedAfterCall);
      assertEquals(
          interruptedBeforeCall && expectedOutcome != Outcome.INTERRUPT, interruptedAfterCall);
    } finally {
      guard.setSatisfied(true);
      monitor.leave();
      assertFalse(monitor.isOccupiedByCurrentThread());
    }
  }
Esempio n. 18
0
  @Override
  protected void tearDown() throws Exception {
    // We don't want to leave stray threads running after each test. At this point, every thread
    // launched by this test is either:
    //
    // (a) Blocked attempting to enter the monitor.
    // (b) Waiting for the single guard to become satisfied.
    // (c) Occupying the monitor and awaiting the tearDownLatch.
    //
    // Except for (c), every thread should occupy the monitor very briefly, and every thread leaves
    // the monitor with the guard satisfied. Therefore as soon as tearDownLatch is triggered, we
    // should be able to enter the monitor, and then we set the guard to satisfied for the benefit
    // of any remaining waiting threads.

    tearDownLatch.countDown();
    assertTrue(
        "Monitor still occupied in tearDown()",
        monitor.enter(UNEXPECTED_HANG_DELAY_MILLIS, TimeUnit.MILLISECONDS));
    try {
      guard.setSatisfied(true);
    } finally {
      monitor.leave();
    }
  }