Search in sources :

Example 56 with Semaphore

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

Example 57 with Semaphore

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

the class SvnAuthenticationTest method synchronousBackground.

private static void synchronousBackground(final Runnable runnable) throws InterruptedException {
    final Semaphore semaphore = new Semaphore();
    semaphore.down();
    ApplicationManager.getApplication().executeOnPooledThread((Runnable) () -> {
        try {
            runnable.run();
        } finally {
            semaphore.up();
        }
    });
    semaphore.waitFor();
}
Also used : Semaphore(com.intellij.util.concurrency.Semaphore)

Example 58 with Semaphore

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

the class SvnHistoryTest method testLocallyMovedToRenamedDirectory.

@Test
public void testLocallyMovedToRenamedDirectory() 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();
    }
    VcsTestUtil.renameFileInCommand(myProject, tree.myTargetDir, "renamedTarget");
    VcsTestUtil.moveFileInCommand(myProject, tree.myS1File, tree.myTargetDir);
    VcsDirtyScopeManager.getInstance(myProject).markEverythingDirty();
    ChangeListManager.getInstance(myProject).ensureUpToDate(false);
    final Semaphore semaphore = new Semaphore();
    semaphore.down();
    provider.reportAppendableHistory(VcsUtil.getFilePath(tree.myS1File), new VcsAppendableHistorySessionPartner() {

        @Override
        public void reportCreatedEmptySession(VcsAbstractHistorySession session) {
        }

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

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

        @Override
        public void finished() {
            semaphore.up();
        }

        @Override
        public void beforeRefresh() {
        }

        @Override
        public void forceRefresh() {
        }
    });
    semaphore.waitFor(1000);
    Assert.assertEquals(11, myCnt);
    myCnt = 0;
    semaphore.down();
    provider.reportAppendableHistory(VcsUtil.getFilePath(tree.myTargetDir), new VcsAppendableHistorySessionPartner() {

        @Override
        public void reportCreatedEmptySession(VcsAbstractHistorySession session) {
        }

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

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

        @Override
        public void finished() {
            semaphore.up();
        }

        @Override
        public void beforeRefresh() {
        }

        @Override
        public void forceRefresh() {
        }
    });
    semaphore.waitFor(1000);
    Assert.assertEquals(1, myCnt);
    myCnt = 0;
    semaphore.down();
    provider.reportAppendableHistory(VcsUtil.getFilePath(tree.myTargetFiles.get(0)), new VcsAppendableHistorySessionPartner() {

        @Override
        public void reportCreatedEmptySession(VcsAbstractHistorySession session) {
        }

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

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

        @Override
        public void finished() {
            semaphore.up();
        }

        @Override
        public void beforeRefresh() {
        }

        @Override
        public void forceRefresh() {
        }
    });
    semaphore.waitFor(1000);
    Assert.assertEquals(1, myCnt);
}
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)

Example 59 with Semaphore

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

the class ConnectionOnProcess method waitForProcess.

private void waitForProcess(Application application) {
    myWaitSemaphore = new Semaphore();
    myWaitSemaphore.down();
    myWaitForThreadFuture = application.executeOnPooledThread(() -> {
        try {
            myProcess.waitFor();
        } catch (InterruptedException ignored) {
        } finally {
            myWaitSemaphore.up();
        }
    });
}
Also used : Semaphore(com.intellij.util.concurrency.Semaphore)

Example 60 with Semaphore

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

the class ProcessCloseUtil method close.

public static void close(final Process process) {
    final Semaphore outerSemaphore = new Semaphore();
    outerSemaphore.down();
    final Application application = ApplicationManager.getApplication();
    application.executeOnPooledThread(() -> {
        try {
            final Semaphore semaphore = new Semaphore();
            semaphore.down();
            final Runnable closeRunnable = () -> {
                try {
                    closeProcessImpl(process);
                } finally {
                    semaphore.up();
                }
            };
            final Future<?> innerFuture = application.executeOnPooledThread(closeRunnable);
            semaphore.waitFor(ourAsynchronousWaitTimeout);
            if (!(innerFuture.isDone() || innerFuture.isCancelled())) {
                // will call interrupt()
                innerFuture.cancel(true);
            }
        } finally {
            outerSemaphore.up();
        }
    });
    // just wait
    outerSemaphore.waitFor(ourSynchronousWaitTimeout);
}
Also used : Semaphore(com.intellij.util.concurrency.Semaphore) Application(com.intellij.openapi.application.Application)

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