Search in sources :

Example 61 with Semaphore

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

the class CommitHelper method delegateCommitToVcsThread.

private void delegateCommitToVcsThread(final GeneralCommitProcessor processor) {
    final ProgressIndicator indicator = new DelegatingProgressIndicator();
    final Semaphore endSemaphore = new Semaphore();
    endSemaphore.down();
    ChangeListManagerImpl.getInstanceImpl(myProject).executeOnUpdaterThread(new Runnable() {

        @Override
        public void run() {
            indicator.setText("Performing VCS commit...");
            try {
                ProgressManager.getInstance().runProcess(new Runnable() {

                    @Override
                    public void run() {
                        indicator.checkCanceled();
                        generalCommit(processor);
                    }
                }, indicator);
            } finally {
                endSemaphore.up();
            }
        }
    });
    indicator.setText("Waiting for VCS background tasks to finish...");
    while (!endSemaphore.waitFor(20)) {
        indicator.checkCanceled();
    }
}
Also used : DelegatingProgressIndicator(com.intellij.ide.util.DelegatingProgressIndicator) DelegatingProgressIndicator(com.intellij.ide.util.DelegatingProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Semaphore(com.intellij.util.concurrency.Semaphore)

Example 62 with Semaphore

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

the class ApplicationImplTest method checkPooledThreadsDontGetWrongPrivileges.

private static void checkPooledThreadsDontGetWrongPrivileges() {
    ApplicationImpl app = (ApplicationImpl) ApplicationManager.getApplication();
    Ref<Future> future = Ref.create();
    Disposable disableStderrDumping = Disposer.newDisposable();
    LoggedErrorProcessor.getInstance().disableStderrDumping(disableStderrDumping);
    Semaphore mayFinish = new Semaphore();
    mayFinish.down();
    try {
        app.executeSuspendingWriteAction(ourProject, "", () -> future.set(app.executeOnPooledThread(() -> assertTrue(mayFinish.waitFor(1000)))));
    } catch (AssertionError e) {
        if (!isEscapingThreadAssertion(e)) {
            e.printStackTrace();
            throw e;
        }
    } finally {
        Disposer.dispose(disableStderrDumping);
    }
    app.executeSuspendingWriteAction(ourProject, "", () -> {
    });
    mayFinish.up();
    waitForFuture(future.get());
}
Also used : Disposable(com.intellij.openapi.Disposable) Future(java.util.concurrent.Future) Semaphore(com.intellij.util.concurrency.Semaphore)

Example 63 with Semaphore

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

the class ApplicationImplTest method testCheckCanceledReadAction.

public void testCheckCanceledReadAction() throws Exception {
    Semaphore mayStartReadAction = new Semaphore();
    mayStartReadAction.down();
    ProgressIndicatorBase progress = new ProgressIndicatorBase();
    Future<?> future = ApplicationManager.getApplication().executeOnPooledThread(() -> ProgressManager.getInstance().runProcess(() -> {
        mayStartReadAction.waitFor();
        ReadAction.run(() -> fail("should be canceled before entering read action"));
    }, progress));
    WriteAction.run(() -> {
        mayStartReadAction.up();
        progress.cancel();
        future.get(1, TimeUnit.SECONDS);
    });
}
Also used : ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) Semaphore(com.intellij.util.concurrency.Semaphore)

Example 64 with Semaphore

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

the class ApplicationImplTest method testPooledThreadsThatHappenInSuspendedWriteActionStayInSuspendedWriteAction.

public void testPooledThreadsThatHappenInSuspendedWriteActionStayInSuspendedWriteAction() {
    LoggedErrorProcessor.getInstance().disableStderrDumping(getTestRootDisposable());
    Ref<Future> future = Ref.create();
    ApplicationImpl app = (ApplicationImpl) ApplicationManager.getApplication();
    safeWrite(() -> {
        try {
            Semaphore started = new Semaphore();
            started.down();
            app.executeSuspendingWriteAction(ourProject, "", () -> {
                future.set(app.executeOnPooledThread(() -> {
                    started.up();
                    TimeoutUtil.sleep(1000);
                }));
                assertTrue(started.waitFor(1000));
            });
            fail("should not allow pooled thread to stay there");
        } catch (AssertionError e) {
            assertTrue(ExceptionUtil.getThrowableText(e), isEscapingThreadAssertion(e));
        }
    });
    waitForFuture(future.get());
}
Also used : Future(java.util.concurrent.Future) Semaphore(com.intellij.util.concurrency.Semaphore)

Example 65 with Semaphore

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

the class MavenExecutionTest method execute.

private void execute(final MavenRunnerParameters params) {
    final Semaphore sema = new Semaphore();
    sema.down();
    UIUtil.invokeLaterIfNeeded(() -> MavenRunConfigurationType.runConfiguration(myProject, params, getMavenGeneralSettings(), new MavenRunnerSettings(), new ProgramRunner.Callback() {

        @Override
        public void processStarted(final RunContentDescriptor descriptor) {
            descriptor.getProcessHandler().addProcessListener(new ProcessAdapter() {

                @Override
                public void processTerminated(ProcessEvent event) {
                    sema.up();
                    UIUtil.invokeLaterIfNeeded(() -> Disposer.dispose(descriptor));
                }
            });
        }
    }));
    sema.waitFor();
}
Also used : RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) Semaphore(com.intellij.util.concurrency.Semaphore)

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