Search in sources :

Example 26 with Semaphore

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

the class DumbServiceImpl method waitForSmartMode.

@Override
public void waitForSmartMode() {
    if (!isDumb()) {
        return;
    }
    final Application application = ApplicationManager.getApplication();
    if (application.isReadAccessAllowed() || application.isDispatchThread()) {
        throw new AssertionError("Don't invoke waitForSmartMode from inside read action in dumb mode");
    }
    final Semaphore semaphore = new Semaphore();
    semaphore.down();
    runWhenSmart(semaphore::up);
    while (true) {
        if (semaphore.waitFor(50)) {
            return;
        }
        ProgressManager.checkCanceled();
    }
}
Also used : Semaphore(com.intellij.util.concurrency.Semaphore)

Example 27 with Semaphore

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

the class VfsUtilTest method doRenameAndRefreshTest.

private void doRenameAndRefreshTest(boolean full) throws IOException {
    assertFalse(ApplicationManager.getApplication().isDispatchThread());
    File tempDir = myTempDir.newFolder();
    assertTrue(new File(tempDir, "child").createNewFile());
    VirtualFile parent = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(tempDir);
    assertNotNull(parent);
    if (full) {
        assertEquals(1, parent.getChildren().length);
    }
    VirtualFile child = parent.findChild("child");
    assertNotNull(child);
    List<VirtualFile> files = Collections.singletonList(parent);
    Semaphore semaphore = new Semaphore();
    for (int i = 0; i < 1000; i++) {
        semaphore.down();
        VfsUtil.markDirty(true, false, parent);
        LocalFileSystem.getInstance().refreshFiles(files, true, true, semaphore::up);
        assertTrue(child.isValid());
        String newName = "name" + i;
        new WriteAction() {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                child.rename(this, newName);
            }
        }.execute();
        assertTrue(child.isValid());
        // needed to prevent frequent event detector from triggering
        TimeoutUtil.sleep(1);
    }
    assertTrue(semaphore.waitFor(60000));
}
Also used : NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) WriteAction(com.intellij.openapi.application.WriteAction) Semaphore(com.intellij.util.concurrency.Semaphore) NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) File(java.io.File) Result(com.intellij.openapi.application.Result)

Example 28 with Semaphore

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

the class ProgressIndicatorTest method assertInvokeAndWaitWorks.

private static void assertInvokeAndWaitWorks() {
    Semaphore semaphore = new Semaphore();
    semaphore.down();
    ApplicationManager.getApplication().invokeLater(semaphore::up);
    assertTrue("invokeAndWait would deadlock", semaphore.waitFor(1000));
}
Also used : Semaphore(com.intellij.util.concurrency.Semaphore)

Example 29 with Semaphore

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

the class CloudRuntimeTask method perform.

private T perform(boolean modal, final Disposable disposable) {
    final Semaphore semaphore = new Semaphore();
    semaphore.down();
    final AtomicReference<T> result = new AtomicReference<>();
    final Progressive progressive = new Progressive() {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            indicator.setIndeterminate(true);
            while (!indicator.isCanceled()) {
                if (semaphore.waitFor(500)) {
                    if (mySuccess.get()) {
                        UIUtil.invokeLaterIfNeeded(() -> {
                            if (disposable == null || !Disposer.isDisposed(disposable)) {
                                postPerform(result.get());
                            }
                        });
                    }
                    break;
                }
            }
        }
    };
    Task task;
    boolean cancellable = isCancellable(modal);
    if (modal) {
        task = new Task.Modal(myProject, myTitle, cancellable) {

            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                progressive.run(indicator);
            }
        };
    } else {
        task = new Task.Backgroundable(myProject, myTitle, cancellable) {

            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                progressive.run(indicator);
            }

            @Override
            public boolean shouldStartInBackground() {
                return CloudRuntimeTask.this.shouldStartInBackground();
            }
        };
    }
    mySuccess.set(false);
    myErrorMessage.set(null);
    run(semaphore, result);
    task.queue();
    return result.get();
}
Also used : Progressive(com.intellij.openapi.progress.Progressive) Task(com.intellij.openapi.progress.Task) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(com.intellij.util.concurrency.Semaphore) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with Semaphore

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

the class AntConfigurationImpl method executeTargetSynchronously.

public static boolean executeTargetSynchronously(final DataContext dataContext, final AntBuildTarget target, final List<BuildFileProperty> additionalProperties) {
    final Semaphore targetDone = new Semaphore();
    targetDone.down();
    final Ref<Boolean> result = Ref.create(Boolean.FALSE);
    ApplicationManager.getApplication().invokeLater(() -> {
        try {
            final Project project = dataContext.getData(CommonDataKeys.PROJECT);
            if (project == null || project.isDisposed()) {
                targetDone.up();
            } else {
                target.run(dataContext, additionalProperties, new AntBuildListener() {

                    public void buildFinished(int state, int errorCount) {
                        result.set((state == AntBuildListener.FINISHED_SUCCESSFULLY) && (errorCount == 0));
                        targetDone.up();
                    }
                });
            }
        } catch (Throwable e) {
            targetDone.up();
            LOG.error(e);
        }
    });
    targetDone.waitFor();
    return result.get();
}
Also used : Project(com.intellij.openapi.project.Project) 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