Java Code Examples for com.intellij.util.concurrency.Semaphore#down()

The following examples show how to use com.intellij.util.concurrency.Semaphore#down() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScriptManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
private Library getLibrary(LibraryRef libraryRef) {
  // TODO(devoncarew): Consider changing the signature to `CompletableFuture getLibrary(LibraryRef instance)`
  // (see also the EvalOnDartLibrary implementation).

  final Ref<Library> resultRef = Ref.create();
  final Semaphore semaphore = new Semaphore();
  semaphore.down();

  vmService.getLibrary(isolateRef.getId(), libraryRef.getId(), new GetLibraryConsumer() {
    @Override
    public void received(Library library) {
      resultRef.set(library);
      semaphore.up();
    }

    @Override
    public void onError(RPCError error) {
      semaphore.up();
    }
  });
  semaphore.waitFor(RESPONSE_WAIT_TIMEOUT);
  return resultRef.get();
}
 
Example 2
Source File: ScriptManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Script getScriptSync(@NotNull final ScriptRef scriptRef) {
  final Ref<Script> resultRef = Ref.create();
  final Semaphore semaphore = new Semaphore();
  semaphore.down();

  vmService.getObject(isolateRef.getId(), scriptRef.getId(), new GetObjectConsumer() {
    @Override
    public void received(Obj script) {
      resultRef.set((Script)script);
      semaphore.up();
    }

    @Override
    public void received(Sentinel response) {
      semaphore.up();
    }

    @Override
    public void onError(RPCError error) {
      semaphore.up();
    }
  });

  semaphore.waitFor(RESPONSE_WAIT_TIMEOUT);
  return resultRef.get();
}
 
Example 3
Source File: ScriptManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
private Isolate getCurrentIsolate() {
  final Ref<Isolate> resultRef = Ref.create();
  final Semaphore semaphore = new Semaphore();
  semaphore.down();

  vmService.getIsolate(isolateRef.getId(), new GetIsolateConsumer() {
    @Override
    public void received(Isolate isolate) {
      resultRef.set(isolate);
      semaphore.up();
    }

    @Override
    public void received(Sentinel sentinel) {
      semaphore.up();
    }

    @Override
    public void onError(RPCError error) {
      semaphore.up();
    }
  });
  semaphore.waitFor(RESPONSE_WAIT_TIMEOUT);
  return resultRef.get();
}
 
Example 4
Source File: ChangeListManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void freeze(@Nonnull String reason) {
  myUpdater.setIgnoreBackgroundOperation(true);
  Semaphore sem = new Semaphore();
  sem.down();

  invokeAfterUpdate(() -> {
    myUpdater.setIgnoreBackgroundOperation(false);
    myUpdater.pause();
    myFreezeName.set(reason);
    sem.up();
  }, InvokeAfterUpdateMode.SILENT_CALLBACK_POOLED, "", ModalityState.defaultModalityState());

  boolean free = false;
  while (!free) {
    ProgressIndicator pi = ProgressManager.getInstance().getProgressIndicator();
    if (pi != null) pi.checkCanceled();
    free = sem.waitFor(500);
  }
}
 
Example 5
Source File: ScriptManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
private Library getLibrary(LibraryRef libraryRef) {
  // TODO(devoncarew): Consider changing the signature to `CompletableFuture getLibrary(LibraryRef instance)`
  // (see also the EvalOnDartLibrary implementation).

  final Ref<Library> resultRef = Ref.create();
  final Semaphore semaphore = new Semaphore();
  semaphore.down();

  vmService.getLibrary(isolateRef.getId(), libraryRef.getId(), new GetLibraryConsumer() {
    @Override
    public void received(Library library) {
      resultRef.set(library);
      semaphore.up();
    }

    @Override
    public void onError(RPCError error) {
      semaphore.up();
    }
  });
  semaphore.waitFor(RESPONSE_WAIT_TIMEOUT);
  return resultRef.get();
}
 
Example 6
Source File: ScriptManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Script getScriptSync(@NotNull final ScriptRef scriptRef) {
  final Ref<Script> resultRef = Ref.create();
  final Semaphore semaphore = new Semaphore();
  semaphore.down();

  vmService.getObject(isolateRef.getId(), scriptRef.getId(), new GetObjectConsumer() {
    @Override
    public void received(Obj script) {
      resultRef.set((Script)script);
      semaphore.up();
    }

    @Override
    public void received(Sentinel response) {
      semaphore.up();
    }

    @Override
    public void onError(RPCError error) {
      semaphore.up();
    }
  });

  semaphore.waitFor(RESPONSE_WAIT_TIMEOUT);
  return resultRef.get();
}
 
Example 7
Source File: CommitHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void delegateCommitToVcsThread(final GeneralCommitProcessor processor) {
  final ProgressIndicator indicator = new DelegatingProgressIndicator();

  final Semaphore endSemaphore = new Semaphore();
  endSemaphore.down();

  ChangeListManagerImpl.getInstanceImpl(myProject).executeOnUpdaterThread(new Runnable() {
    @Override
    public void run() {
      indicator.setText("Performing VCS commit...");
      try {
        ProgressManager.getInstance().runProcess(new Runnable() {
          @Override
          public void run() {
            indicator.checkCanceled();
            generalCommit(processor);
          }
        }, indicator);
      }
      finally {
        endSemaphore.up();
      }
    }
  });

  indicator.setText("Waiting for VCS background tasks to finish...");
  while (!endSemaphore.waitFor(20)) {
    indicator.checkCanceled();
  }
}
 
Example 8
Source File: ProcessHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected ProcessHandler() {
  myEventMulticaster = createEventMulticaster();
  myWaitSemaphore = new Semaphore();
  myWaitSemaphore.down();
  myAfterStartNotifiedRunner = new TasksRunner();
  myListeners.add(myAfterStartNotifiedRunner);
}
 
Example 9
Source File: PotemkinProgress.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void ensureBackgroundThreadStarted(@Nonnull Runnable action) {
  Semaphore started = new Semaphore();
  started.down();
  myApp.executeOnPooledThread(() -> ProgressManager.getInstance().runProcess(() -> {
    started.up();
    action.run();
  }, this));

  started.waitFor();
}
 
Example 10
Source File: LaterInvocator.java    From consulo with Apache License 2.0 5 votes vote down vote up
@TestOnly
public static void dispatchPendingFlushes() {
  if (!isDispatchThread()) throw new IllegalStateException("Must call from EDT");

  Semaphore semaphore = new Semaphore();
  semaphore.down();
  invokeLaterWithCallback(semaphore::up, ModalityState.any(), Conditions.FALSE, null);
  while (!semaphore.isUp()) {
    UIUtil.dispatchAllInvocationEvents();
  }
}
 
Example 11
Source File: LaterInvocator.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void invokeAndWait(@Nonnull final Runnable runnable, @Nonnull ModalityState modalityState) {
  LOG.assertTrue(!isDispatchThread());

  final Semaphore semaphore = new Semaphore();
  semaphore.down();
  final Ref<Throwable> exception = Ref.create();
  Runnable runnable1 = new Runnable() {
    @Override
    public void run() {
      try {
        runnable.run();
      }
      catch (Throwable e) {
        exception.set(e);
      }
      finally {
        semaphore.up();
      }
    }

    @Override
    @NonNls
    public String toString() {
      return "InvokeAndWait[" + runnable + "]";
    }
  };
  invokeLaterWithCallback(runnable1, modalityState, Conditions.FALSE, null);
  semaphore.waitFor();
  if (!exception.isNull()) {
    Throwable cause = exception.get();
    if (SystemProperties.getBooleanProperty("invoke.later.wrap.error", true)) {
      // wrap everything to keep the current thread stacktrace
      // also TC ComparisonFailure feature depends on this
      throw new RuntimeException(cause);
    }
    else {
      ExceptionUtil.rethrow(cause);
    }
  }
}
 
Example 12
Source File: ProcessCloseUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void close(final Process process) {
  final Semaphore outerSemaphore = new Semaphore();
  outerSemaphore.down();

  final Application application = ApplicationManager.getApplication();
  application.executeOnPooledThread(new Runnable() {
    public void run() {
      try {
        final Semaphore semaphore = new Semaphore();
        semaphore.down();

        final Runnable closeRunnable = new Runnable() {
          public void run() {
            try {
              closeProcessImpl(process);
            }
            finally {
              semaphore.up();
            }
          }
        };

        final Future<?> innerFuture = application.executeOnPooledThread(closeRunnable);
        semaphore.waitFor(ourAsynchronousWaitTimeout);
        if ( ! (innerFuture.isDone() || innerFuture.isCancelled())) {
          innerFuture.cancel(true); // will call interrupt()
        }
      }
      finally {
        outerSemaphore.up();
      }
    }
  });

  // just wait
  outerSemaphore.waitFor(ourSynchronousWaitTimeout);
}
 
Example 13
Source File: SequentialTaskExecutor.java    From consulo with Apache License 2.0 5 votes vote down vote up
public <V> V queueAndWaitTask(final Callable<V> task) throws Throwable {
  final Ref<V> resultRef = new Ref<V>();
  final Ref<Throwable> throwableRef = new Ref<Throwable>();

  final Semaphore taskSemaphore = new Semaphore();
  taskSemaphore.down();

  queueTask(new Runnable() {

    @Override
    public void run() {
      try {
        resultRef.set(task.call());
      }
      catch (Throwable e) {
        throwableRef.set(e);
        LOG.error(e);
      }
      finally {
        taskSemaphore.up();
      }
    }
  });

  taskSemaphore.waitFor();

  if (!throwableRef.isNull()) {
    throw throwableRef.get();
  }

  return resultRef.get();
}
 
Example 14
Source File: DartVmServiceListener.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
private String evaluateExpression(final @NotNull String isolateId,
                                  final @Nullable Frame vmTopFrame,
                                  final @Nullable XExpression xExpression) {
  final String evalText = xExpression == null ? null : xExpression.getExpression();
  if (vmTopFrame == null || StringUtil.isEmptyOrSpaces(evalText)) return null;

  final Ref<String> evalResult = new Ref<>();
  final Semaphore semaphore = new Semaphore();
  semaphore.down();

  myDebugProcess.getVmServiceWrapper().evaluateInFrame(isolateId, vmTopFrame, evalText, new XDebuggerEvaluator.XEvaluationCallback() {
    @Override
    public void evaluated(@NotNull final XValue result) {
      if (result instanceof DartVmServiceValue) {
        evalResult.set(getSimpleStringPresentation(((DartVmServiceValue)result).getInstanceRef()));
      }
      semaphore.up();
    }

    @Override
    public void errorOccurred(@NotNull final String errorMessage) {
      evalResult.set("Failed to evaluate log expression [" + evalText + "]: " + errorMessage);
      semaphore.up();
    }
  });

  semaphore.waitFor(1000);
  return evalResult.get();
}
 
Example 15
Source File: VmServiceWrapper.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
public Script getScriptSync(@NotNull final String isolateId, @NotNull final String scriptId) {
  assertSyncRequestAllowed();

  final Semaphore semaphore = new Semaphore();
  semaphore.down();

  final Ref<Script> resultRef = Ref.create();

  addRequest(() -> myVmService.getObject(isolateId, scriptId, new GetObjectConsumer() {
    @Override
    public void received(Obj script) {
      resultRef.set((Script)script);
      semaphore.up();
    }

    @Override
    public void received(Sentinel response) {
      semaphore.up();
    }

    @Override
    public void onError(RPCError error) {
      semaphore.up();
    }
  }));

  semaphore.waitFor(RESPONSE_WAIT_TIMEOUT);
  return resultRef.get();
}
 
Example 16
Source File: TransferToEDTQueue.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void waitFor() {
  final Semaphore semaphore = new Semaphore();
  semaphore.down();
  schedule(new Runnable() {
    @Override
    public void run() {
      semaphore.up();
    }
  });
  semaphore.waitFor();
}
 
Example 17
Source File: DartVmServiceListener.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
private String evaluateExpression(final @NotNull String isolateId,
                                  final @Nullable Frame vmTopFrame,
                                  final @Nullable XExpression xExpression) {
  final String evalText = xExpression == null ? null : xExpression.getExpression();
  if (vmTopFrame == null || StringUtil.isEmptyOrSpaces(evalText)) return null;

  final Ref<String> evalResult = new Ref<>();
  final Semaphore semaphore = new Semaphore();
  semaphore.down();

  myDebugProcess.getVmServiceWrapper().evaluateInFrame(isolateId, vmTopFrame, evalText, new XDebuggerEvaluator.XEvaluationCallback() {
    @Override
    public void evaluated(@NotNull final XValue result) {
      if (result instanceof DartVmServiceValue) {
        evalResult.set(getSimpleStringPresentation(((DartVmServiceValue)result).getInstanceRef()));
      }
      semaphore.up();
    }

    @Override
    public void errorOccurred(@NotNull final String errorMessage) {
      evalResult.set("Failed to evaluate log expression [" + evalText + "]: " + errorMessage);
      semaphore.up();
    }
  });

  semaphore.waitFor(1000);
  return evalResult.get();
}
 
Example 18
Source File: VmServiceWrapper.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
public Script getScriptSync(@NotNull final String isolateId, @NotNull final String scriptId) {
  assertSyncRequestAllowed();

  final Semaphore semaphore = new Semaphore();
  semaphore.down();

  final Ref<Script> resultRef = Ref.create();

  addRequest(() -> myVmService.getObject(isolateId, scriptId, new GetObjectConsumer() {
    @Override
    public void received(Obj script) {
      resultRef.set((Script)script);
      semaphore.up();
    }

    @Override
    public void received(Sentinel response) {
      semaphore.up();
    }

    @Override
    public void onError(RPCError error) {
      semaphore.up();
    }
  }));

  semaphore.waitFor(RESPONSE_WAIT_TIMEOUT);
  return resultRef.get();
}
 
Example 19
Source File: ProducerConsumer.java    From consulo with Apache License 2.0 4 votes vote down vote up
private PooledConsumerRunnable() {
  mySemaphore = new Semaphore();
  mySemaphore.down();
}
 
Example 20
Source File: HeadlessValueEvaluationCallback.java    From consulo with Apache License 2.0 4 votes vote down vote up
public HeadlessValueEvaluationCallback(@Nonnull XValueNodeImpl node) {
  myNode = node;
  mySemaphore = new Semaphore();
  mySemaphore.down();
}