Search in sources :

Example 1 with Semaphore

use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.

the class MavenUtil method smartInvokeAndWait.

public static void smartInvokeAndWait(final Project p, final ModalityState state, final Runnable r) {
    if (isNoBackgroundMode() || ApplicationManager.getApplication().isDispatchThread()) {
        r.run();
    } else {
        final Semaphore semaphore = new Semaphore();
        semaphore.down();
        DumbService.getInstance(p).smartInvokeLater(() -> {
            try {
                r.run();
            } finally {
                semaphore.up();
            }
        }, state);
        semaphore.waitFor();
    }
}
Also used : Semaphore(com.intellij.util.concurrency.Semaphore)

Example 2 with Semaphore

use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.

the class SvnBusyOnAddTest method testStatusDoesNotLockForWrite.

public void testStatusDoesNotLockForWrite() throws Exception {
    final File ioFile = new File(myWorkingCopyRoot, filename);
    ioFile.getParentFile().mkdirs();
    /*SVNWCClient client11 = new SVNWCClient((ISVNRepositoryPool)null, new DefaultSVNOptions());
    client11.doAdd(ioFile.getParentFile(), true, false, true, true);*/
    ioFile.createNewFile();
    try {
        final SVNStatusClient readClient = new SVNStatusClient((ISVNRepositoryPool) null, new DefaultSVNOptions());
        final Semaphore semaphore = new Semaphore();
        final Semaphore semaphoreMain = new Semaphore();
        final Semaphore semaphoreWokeUp = new Semaphore();
        final AtomicReference<Boolean> wasUp = new AtomicReference<>(false);
        final ISVNStatusHandler handler = status -> {
            semaphore.waitFor();
            wasUp.set(true);
        };
        semaphore.down();
        semaphoreMain.down();
        semaphoreWokeUp.down();
        final SVNException[] exception = new SVNException[1];
        Thread thread = new Thread(() -> {
            try {
                semaphoreMain.up();
                readClient.doStatus(myWorkingCopyRoot, true, false, true, false, handler);
                semaphoreWokeUp.up();
            } catch (SVNException e) {
                exception[0] = e;
            }
        }, "svn test");
        thread.start();
        semaphoreMain.waitFor();
        TimeoutUtil.sleep(5);
        SVNWCClient client = new SVNWCClient((ISVNRepositoryPool) null, new DefaultSVNOptions());
        client.doAdd(ioFile.getParentFile(), true, false, true, true);
        semaphore.up();
        semaphoreWokeUp.waitFor();
        Assert.assertEquals(true, wasUp.get().booleanValue());
        if (exception[0] != null) {
            throw exception[0];
        }
        thread.join();
    } finally {
        ioFile.delete();
    }
}
Also used : SVNCancelException(org.tmatesoft.svn.core.SVNCancelException) PluginPathManager(com.intellij.openapi.application.PluginPathManager) SVNException(org.tmatesoft.svn.core.SVNException) PathManager(com.intellij.openapi.application.PathManager) Test(org.junit.Test) SVNErrorCode(org.tmatesoft.svn.core.SVNErrorCode) ISVNWCDb(org.tmatesoft.svn.core.internal.wc17.db.ISVNWCDb) AtomicReference(java.util.concurrent.atomic.AtomicReference) File(java.io.File) SVNWCContext(org.tmatesoft.svn.core.internal.wc17.SVNWCContext) org.tmatesoft.svn.core.wc(org.tmatesoft.svn.core.wc) Nullable(org.jetbrains.annotations.Nullable) Assert(junit.framework.Assert) TimeoutUtil(com.intellij.util.TimeoutUtil) Semaphore(com.intellij.util.concurrency.Semaphore) TestCase(junit.framework.TestCase) DefaultSVNOptions(org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions) SVNWCDb(org.tmatesoft.svn.core.internal.wc17.db.SVNWCDb) Before(org.junit.Before) DefaultSVNOptions(org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(com.intellij.util.concurrency.Semaphore) SVNException(org.tmatesoft.svn.core.SVNException) File(java.io.File)

Example 3 with Semaphore

use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.

the class MavenBeforeRunTasksProvider method executeTask.

public boolean executeTask(final DataContext context, RunConfiguration configuration, ExecutionEnvironment env, final MavenBeforeRunTask task) {
    final Semaphore targetDone = new Semaphore();
    final boolean[] result = new boolean[] { true };
    try {
        ApplicationManager.getApplication().invokeAndWait(() -> {
            final Project project = CommonDataKeys.PROJECT.getData(context);
            final MavenProject mavenProject = getMavenProject(task);
            if (project == null || project.isDisposed() || mavenProject == null)
                return;
            FileDocumentManager.getInstance().saveAllDocuments();
            final MavenExplicitProfiles explicitProfiles = MavenProjectsManager.getInstance(project).getExplicitProfiles();
            final MavenRunner mavenRunner = MavenRunner.getInstance(project);
            targetDone.down();
            new Task.Backgroundable(project, TasksBundle.message("maven.tasks.executing"), true) {

                public void run(@NotNull ProgressIndicator indicator) {
                    try {
                        MavenRunnerParameters params = new MavenRunnerParameters(true, mavenProject.getDirectory(), ParametersListUtil.parse(task.getGoal()), explicitProfiles.getEnabledProfiles(), explicitProfiles.getDisabledProfiles());
                        result[0] = mavenRunner.runBatch(Collections.singletonList(params), null, null, TasksBundle.message("maven.tasks.executing"), indicator);
                    } finally {
                        targetDone.up();
                    }
                }

                @Override
                public boolean shouldStartInBackground() {
                    return MavenRunner.getInstance(project).getSettings().isRunMavenInBackground();
                }

                @Override
                public void processSentToBackground() {
                    MavenRunner.getInstance(project).getSettings().setRunMavenInBackground(true);
                }
            }.queue();
        }, ModalityState.NON_MODAL);
    } catch (Exception e) {
        MavenLog.LOG.error(e);
        return false;
    }
    targetDone.waitFor();
    return result[0];
}
Also used : Project(com.intellij.openapi.project.Project) MavenProject(org.jetbrains.idea.maven.project.MavenProject) Task(com.intellij.openapi.progress.Task) MavenProject(org.jetbrains.idea.maven.project.MavenProject) MavenRunner(org.jetbrains.idea.maven.execution.MavenRunner) MavenExplicitProfiles(org.jetbrains.idea.maven.model.MavenExplicitProfiles) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Semaphore(com.intellij.util.concurrency.Semaphore) MavenRunnerParameters(org.jetbrains.idea.maven.execution.MavenRunnerParameters)

Example 4 with Semaphore

use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.

the class MavenProjectsProcessor method waitForCompletion.

public void waitForCompletion() {
    if (isStopped)
        return;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        synchronized (myQueue) {
            while (!myQueue.isEmpty()) {
                startProcessing(myQueue.poll());
            }
        }
        return;
    }
    final Semaphore semaphore = new Semaphore();
    semaphore.down();
    scheduleTask(new MavenProjectsProcessorTask() {

        public void perform(Project project, MavenEmbeddersManager embeddersManager, MavenConsole console, MavenProgressIndicator indicator) throws MavenProcessCanceledException {
            semaphore.up();
        }
    });
    while (true) {
        if (isStopped || semaphore.waitFor(1000))
            return;
    }
}
Also used : Project(com.intellij.openapi.project.Project) Semaphore(com.intellij.util.concurrency.Semaphore) SoutMavenConsole(org.jetbrains.idea.maven.execution.SoutMavenConsole)

Example 5 with Semaphore

use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.

the class SvnHistoryTest method testRepositoryRootHistory.

@Test
public void testRepositoryRootHistory() throws Exception {
    enableSilentOperation(VcsConfiguration.StandardConfirmation.ADD);
    enableSilentOperation(VcsConfiguration.StandardConfirmation.REMOVE);
    myCnt = 0;
    final VcsHistoryProvider provider = SvnVcs.getInstance(myProject).getVcsHistoryProvider();
    final SubTree tree = new SubTree(myWorkingCopyDir);
    checkin();
    for (int i = 0; i < 10; i++) {
        VcsTestUtil.editFileInCommand(myProject, tree.myS1File, "1\n2\n3\n4\n" + i);
        checkin();
    }
    final Semaphore semaphore = new Semaphore();
    semaphore.down();
    final FilePath rootPath = VcsContextFactory.SERVICE.getInstance().createFilePathOnNonLocal(myRepoUrl, true);
    provider.reportAppendableHistory(rootPath, new VcsAppendableHistorySessionPartner() {

        @Override
        public void reportCreatedEmptySession(VcsAbstractHistorySession session) {
        }

        @Override
        public void acceptRevision(VcsFileRevision revision) {
            ++myCnt;
            semaphore.up();
        }

        @Override
        public void reportException(VcsException exception) {
            throw new RuntimeException(exception);
        }

        @Override
        public void finished() {
        }

        @Override
        public void beforeRefresh() {
        }

        @Override
        public void forceRefresh() {
        }
    });
    semaphore.waitFor(1000);
    Assert.assertTrue(myCnt > 0);
}
Also used : VcsAppendableHistorySessionPartner(com.intellij.openapi.vcs.history.VcsAppendableHistorySessionPartner) VcsHistoryProvider(com.intellij.openapi.vcs.history.VcsHistoryProvider) Semaphore(com.intellij.util.concurrency.Semaphore) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) VcsAbstractHistorySession(com.intellij.openapi.vcs.history.VcsAbstractHistorySession) Test(org.junit.Test)

Aggregations

Semaphore (com.intellij.util.concurrency.Semaphore)77 Ref (com.intellij.openapi.util.Ref)10 NotNull (org.jetbrains.annotations.NotNull)9 Nullable (org.jetbrains.annotations.Nullable)9 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)8 Project (com.intellij.openapi.project.Project)8 IOException (java.io.IOException)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Test (org.junit.Test)7 VcsFileRevision (com.intellij.openapi.vcs.history.VcsFileRevision)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)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