use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.
the class RunConfigurationBeforeRunProvider method doRunTask.
public static boolean doRunTask(final String executorId, final ExecutionEnvironment environment, ProgramRunner<?> runner) {
final Semaphore targetDone = new Semaphore();
final Ref<Boolean> result = new Ref<>(false);
final Disposable disposable = Disposer.newDisposable();
environment.getProject().getMessageBus().connect(disposable).subscribe(ExecutionManager.EXECUTION_TOPIC, new ExecutionListener() {
@Override
public void processStartScheduled(@NotNull final String executorIdLocal, @NotNull final ExecutionEnvironment environmentLocal) {
if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
targetDone.down();
}
}
@Override
public void processNotStarted(@NotNull final String executorIdLocal, @NotNull final ExecutionEnvironment environmentLocal) {
if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
Boolean skipRun = environment.getUserData(ExecutionManagerImpl.EXECUTION_SKIP_RUN);
if (skipRun != null && skipRun) {
result.set(true);
}
targetDone.up();
}
}
@Override
public void processTerminated(@NotNull String executorIdLocal, @NotNull ExecutionEnvironment environmentLocal, @NotNull ProcessHandler handler, int exitCode) {
if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
result.set(exitCode == 0);
targetDone.up();
}
}
});
try {
ApplicationManager.getApplication().invokeAndWait(() -> {
try {
runner.execute(environment);
} catch (ExecutionException e) {
targetDone.up();
LOG.error(e);
}
}, ModalityState.NON_MODAL);
} catch (Exception e) {
LOG.error(e);
Disposer.dispose(disposable);
return false;
}
targetDone.waitFor();
Disposer.dispose(disposable);
return result.get();
}
use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.
the class PsiDocumentManagerBase method commitAndRunReadAction.
@Override
public void commitAndRunReadAction(@NotNull final Runnable runnable) {
final Application application = ApplicationManager.getApplication();
if (SwingUtilities.isEventDispatchThread()) {
commitAllDocuments();
runnable.run();
return;
}
if (ApplicationManager.getApplication().isReadAccessAllowed()) {
LOG.error("Don't call commitAndRunReadAction inside ReadAction, it will cause a deadlock otherwise. " + Thread.currentThread());
}
while (true) {
boolean executed = application.runReadAction((Computable<Boolean>) () -> {
if (myUncommittedDocuments.isEmpty()) {
runnable.run();
return true;
}
return false;
});
if (executed)
break;
final Semaphore semaphore = new Semaphore();
semaphore.down();
application.invokeLater(() -> {
if (myProject.isDisposed()) {
// committedness doesn't matter anymore; give clients a chance to do checkCanceled
semaphore.up();
return;
}
performWhenAllCommitted(() -> semaphore.up());
}, ModalityState.any());
semaphore.waitFor();
}
}
use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.
the class ExecutionHelper method createTimeLimitedExecutionProcess.
private static Runnable createTimeLimitedExecutionProcess(final ProcessHandler processHandler, final ExecutionMode mode, @NotNull final String presentableCmdline) {
return new Runnable() {
private final Semaphore mySemaphore = new Semaphore();
private final Runnable myProcessThread = () -> {
try {
final boolean finished = processHandler.waitFor(1000 * mode.getTimeout());
if (!finished) {
mode.getTimeoutCallback().consume(mode, presentableCmdline);
processHandler.destroyProcess();
}
} finally {
mySemaphore.up();
}
};
@Override
public void run() {
mySemaphore.down();
ApplicationManager.getApplication().executeOnPooledThread(myProcessThread);
mySemaphore.waitFor();
}
};
}
use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.
the class ValueDescriptorImpl method getValue.
@Override
public Value getValue() {
// to keep temporary objects
if (Patches.IBM_JDK_DISABLE_COLLECTION_BUG) {
final EvaluationContextImpl evalContext = myStoredEvaluationContext;
if (evalContext != null && !evalContext.getSuspendContext().isResumed() && myValue instanceof ObjectReference && VirtualMachineProxyImpl.isCollected((ObjectReference) myValue)) {
final Semaphore semaphore = new Semaphore();
semaphore.down();
evalContext.getDebugProcess().getManagerThread().invoke(new SuspendContextCommandImpl(evalContext.getSuspendContext()) {
@Override
public void contextAction() throws Exception {
// re-setting the context will cause value recalculation
try {
setContext(myStoredEvaluationContext);
} finally {
semaphore.up();
}
}
@Override
protected void commandCancelled() {
semaphore.up();
}
});
semaphore.waitFor();
}
}
assertValueReady();
return myValue;
}
use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.
the class BombedProgressIndicator method runBombed.
/**
* @return whether the indicator was canceled during runnable execution.
*/
public boolean runBombed(final Runnable runnable) {
myThread = Thread.currentThread();
final Semaphore canStart = new Semaphore();
canStart.down();
final Semaphore finished = new Semaphore();
finished.down();
// ProgressManager invokes indicator.checkCanceled only when there's at least one canceled indicator. So we have to create a mock one
// on an unrelated thread and cancel it immediately.
Future<?> future = ApplicationManager.getApplication().executeOnPooledThread(() -> {
final ProgressIndicatorBase mockIndicator = new ProgressIndicatorBase();
ProgressManager.getInstance().runProcess(() -> {
mockIndicator.cancel();
canStart.up();
finished.waitFor();
try {
ProgressManager.checkCanceled();
TestCase.fail();
} catch (ProcessCanceledException ignored) {
}
}, mockIndicator);
});
ProgressManager.getInstance().runProcess(() -> {
canStart.waitFor();
try {
runnable.run();
} catch (ProcessCanceledException ignore) {
} finally {
finished.up();
}
}, this);
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
return isCanceled();
}
Aggregations