use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.
the class InterruptibleActivity method execute.
public final int execute() {
final Application application = ApplicationManager.getApplication();
/* TODO: uncomment assertion when problems in Perforce plugin are fixed.
LOG.assertTrue(!application.isDispatchThread(), "InterruptibleActivity is supposed to be lengthy thus must not block Swing UI thread");
*/
final Semaphore semaphore = new Semaphore();
semaphore.down();
application.executeOnPooledThread(() -> {
try {
start();
} finally {
semaphore.up();
}
});
final int rc = waitForSemaphore(semaphore);
if (rc != 0) {
application.executeOnPooledThread(() -> interrupt());
}
return rc;
}
use of com.intellij.util.concurrency.Semaphore in project intellij-plugins by JetBrains.
the class DartVmServiceListener method evaluateExpression.
@Nullable
private String evaluateExpression(@NotNull final String isolateId, @Nullable final Frame vmTopFrame, @Nullable final 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();
}
use of com.intellij.util.concurrency.Semaphore in project android by JetBrains.
the class DistributionServiceTest method testAsync.
/**
* Test that refresh will run asynchronously.
*/
public void testAsync() throws Exception {
final FileDownloader downloader = Mockito.mock(FileDownloader.class);
final Semaphore s = new Semaphore();
s.down();
Mockito.when(downloader.download(Matchers.any(File.class))).thenAnswer(new Answer<List<Pair<File, DownloadableFileDescription>>>() {
@Override
public List<Pair<File, DownloadableFileDescription>> answer(InvocationOnMock invocation) throws Throwable {
assertTrue(s.waitFor(5000));
return ImmutableList.of(Pair.create(myDistributionFile, myDescription));
}
});
final DistributionService service = new DistributionService(downloader, CACHE_PATH, myDistributionFileUrl);
service.refresh(() -> {
service.getSupportedDistributionForApiLevel(19);
service.getDistributionForApiLevel(21);
try {
Mockito.verify(downloader).download(Matchers.any(File.class));
} catch (IOException e) {
throw new RuntimeException(e);
}
}, null);
s.up();
}
use of com.intellij.util.concurrency.Semaphore in project intellij-plugins by JetBrains.
the class PhoneGapAddPlatformBeforeRun method executeTask.
@Override
public boolean executeTask(DataContext context, final RunConfiguration configuration, ExecutionEnvironment env, PhoneGapAddPlatformTask task) {
final PhoneGapRunConfiguration phoneGapRunConfiguration = (PhoneGapRunConfiguration) configuration;
final PhoneGapCommandLine line = phoneGapRunConfiguration.getCommandLine();
if (!line.needAddPlatform()) {
return true;
}
final Project project = configuration.getProject();
final Semaphore targetDone = new Semaphore();
final Ref<Boolean> result = new Ref<>(true);
final List<Exception> exceptions = new ArrayList<>();
ApplicationManager.getApplication().invokeAndWait(() -> {
//Save all opened documents
FileDocumentManager.getInstance().saveAllDocuments();
targetDone.down();
new Task.Backgroundable(project, PhoneGapBundle.message("phonegap.before.task.init.title"), true) {
public boolean shouldStartInBackground() {
return true;
}
public void run(@NotNull final ProgressIndicator indicator) {
try {
String platform = phoneGapRunConfiguration.getPlatform();
assert platform != null;
ProcessOutput output = line.platformAdd(platform);
if (output.getExitCode() != 0) {
ExecutionHelper.showOutput(project, output, PhoneGapBundle.message("phonegap.before.task.init.title"), null, true);
result.set(false);
}
} catch (final Exception e) {
exceptions.add(e);
result.set(false);
} finally {
targetDone.up();
}
}
}.queue();
}, ModalityState.NON_MODAL);
if (!targetDone.waitFor(TimeUnit.MINUTES.toMillis(2))) {
ExecutionHelper.showErrors(project, ContainerUtil.createMaybeSingletonList(new RuntimeException("Timeout")), PhoneGapBundle.message("phonegap.before.task.init.error"), null);
} else if (!exceptions.isEmpty()) {
ExecutionHelper.showErrors(project, exceptions, PhoneGapBundle.message("phonegap.before.task.init.error"), null);
}
return result.get();
}
use of com.intellij.util.concurrency.Semaphore in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoBeforeRunTaskProvider method executeTask.
@Override
public boolean executeTask(DataContext context, RunConfiguration configuration, ExecutionEnvironment env, GoCommandBeforeRunTask task) {
Semaphore done = new Semaphore();
Ref<Boolean> result = Ref.create(false);
GoRunConfigurationBase goRunConfiguration = (GoRunConfigurationBase) configuration;
Module module = goRunConfiguration.getConfigurationModule().getModule();
Project project = configuration.getProject();
String workingDirectory = goRunConfiguration.getWorkingDirectory();
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
if (StringUtil.isEmpty(task.getCommand()))
return;
if (project == null || project.isDisposed())
return;
GoSdkService sdkService = GoSdkService.getInstance(project);
if (!sdkService.isGoModule(module))
return;
done.down();
GoExecutor.in(module).withParameterString(task.getCommand()).withWorkDirectory(workingDirectory).showOutputOnError().showNotifications(false, true).withPresentableName("Executing `" + task + "`").withProcessListener(new ProcessAdapter() {
@Override
public void processTerminated(ProcessEvent event) {
done.up();
result.set(event.getExitCode() == 0);
}
}).executeWithProgress(false, result1 -> VirtualFileManager.getInstance().asyncRefresh(null));
}
});
done.waitFor();
return result.get();
}
Aggregations