Search in sources :

Example 31 with Semaphore

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;
}
Also used : Semaphore(com.intellij.util.concurrency.Semaphore) Application(com.intellij.openapi.application.Application)

Example 32 with Semaphore

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();
}
Also used : DartVmServiceValue(com.jetbrains.lang.dart.ide.runner.server.vmService.frame.DartVmServiceValue) Ref(com.intellij.openapi.util.Ref) XDebuggerEvaluator(com.intellij.xdebugger.evaluation.XDebuggerEvaluator) Semaphore(com.intellij.util.concurrency.Semaphore) XValue(com.intellij.xdebugger.frame.XValue) Nullable(org.jetbrains.annotations.Nullable)

Example 33 with Semaphore

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();
}
Also used : DownloadableFileDescription(com.intellij.util.download.DownloadableFileDescription) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FileDownloader(com.intellij.util.download.FileDownloader) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Semaphore(com.intellij.util.concurrency.Semaphore) IOException(java.io.IOException) File(java.io.File)

Example 34 with Semaphore

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();
}
Also used : PhoneGapCommandLine(com.github.masahirosuzuka.PhoneGapIntelliJPlugin.commandLine.PhoneGapCommandLine) Task(com.intellij.openapi.progress.Task) BeforeRunTask(com.intellij.execution.BeforeRunTask) ArrayList(java.util.ArrayList) Semaphore(com.intellij.util.concurrency.Semaphore) Project(com.intellij.openapi.project.Project) Ref(com.intellij.openapi.util.Ref) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProcessOutput(com.intellij.execution.process.ProcessOutput)

Example 35 with Semaphore

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();
}
Also used : UIUtil(com.intellij.util.ui.UIUtil) GoRunConfigurationBase(com.goide.runconfig.GoRunConfigurationBase) RunConfiguration(com.intellij.execution.configurations.RunConfiguration) DataContext(com.intellij.openapi.actionSystem.DataContext) GoIcons(com.goide.GoIcons) StringUtil(com.intellij.openapi.util.text.StringUtil) GoExecutor(com.goide.util.GoExecutor) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) Key(com.intellij.openapi.util.Key) VirtualFileManager(com.intellij.openapi.vfs.VirtualFileManager) GoSdkService(com.goide.sdk.GoSdkService) Nullable(org.jetbrains.annotations.Nullable) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) Semaphore(com.intellij.util.concurrency.Semaphore) BeforeRunTaskProvider(com.intellij.execution.BeforeRunTaskProvider) Project(com.intellij.openapi.project.Project) ProcessEvent(com.intellij.execution.process.ProcessEvent) Messages(com.intellij.openapi.ui.Messages) Module(com.intellij.openapi.module.Module) Ref(com.intellij.openapi.util.Ref) javax.swing(javax.swing) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) Semaphore(com.intellij.util.concurrency.Semaphore) GoSdkService(com.goide.sdk.GoSdkService) GoRunConfigurationBase(com.goide.runconfig.GoRunConfigurationBase) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module)

Aggregations

Semaphore (com.intellij.util.concurrency.Semaphore)74 Ref (com.intellij.openapi.util.Ref)10 Project (com.intellij.openapi.project.Project)8 IOException (java.io.IOException)8 Nullable (org.jetbrains.annotations.Nullable)8 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)7 NotNull (org.jetbrains.annotations.NotNull)7 Test (org.junit.Test)7 VcsFileRevision (com.intellij.openapi.vcs.history.VcsFileRevision)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 ProcessEvent (com.intellij.execution.process.ProcessEvent)5 File (java.io.File)5 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)4 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)4 Disposable (com.intellij.openapi.Disposable)4 Application (com.intellij.openapi.application.Application)4 Task (com.intellij.openapi.progress.Task)4 VcsAbstractHistorySession (com.intellij.openapi.vcs.history.VcsAbstractHistorySession)4 VcsAppendableHistorySessionPartner (com.intellij.openapi.vcs.history.VcsAppendableHistorySessionPartner)4 VcsHistoryProvider (com.intellij.openapi.vcs.history.VcsHistoryProvider)4