Search in sources :

Example 21 with Semaphore

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

the class PsiDocumentManagerImplTest method testCommitInBackground.

public void testCommitInBackground() {
    PsiFile file = findFile(createFile());
    assertNotNull(file);
    assertTrue(file.isPhysical());
    final Document document = getDocument(file);
    assertNotNull(document);
    final Semaphore semaphore = new Semaphore();
    semaphore.down();
    getPsiDocumentManager().performWhenAllCommitted(() -> {
        assertTrue(getPsiDocumentManager().isCommitted(document));
        semaphore.up();
    });
    waitAndPump(semaphore, TIMEOUT);
    assertTrue(getPsiDocumentManager().isCommitted(document));
    WriteCommandAction.runWriteCommandAction(null, () -> document.insertString(0, "class X {}"));
    semaphore.down();
    getPsiDocumentManager().performWhenAllCommitted(() -> {
        assertTrue(getPsiDocumentManager().isCommitted(document));
        semaphore.up();
    });
    waitAndPump(semaphore, TIMEOUT);
    assertTrue(getPsiDocumentManager().isCommitted(document));
    final AtomicInteger count = new AtomicInteger();
    WriteCommandAction.runWriteCommandAction(null, () -> {
        document.insertString(0, "/**/");
        boolean executed = getPsiDocumentManager().cancelAndRunWhenAllCommitted("xxx", count::incrementAndGet);
        assertFalse(executed);
        executed = getPsiDocumentManager().cancelAndRunWhenAllCommitted("xxx", count::incrementAndGet);
        assertFalse(executed);
        assertEquals(0, count.get());
    });
    while (!getPsiDocumentManager().isCommitted(document)) {
        UIUtil.dispatchAllInvocationEvents();
    }
    assertTrue(getPsiDocumentManager().isCommitted(document));
    assertEquals(1, count.get());
    count.set(0);
    WriteCommandAction.runWriteCommandAction(null, () -> {
        document.insertString(0, "/**/");
        boolean executed = getPsiDocumentManager().performWhenAllCommitted(count::incrementAndGet);
        assertFalse(executed);
        executed = getPsiDocumentManager().performWhenAllCommitted(count::incrementAndGet);
        assertFalse(executed);
        assertEquals(0, count.get());
    });
    while (!getPsiDocumentManager().isCommitted(document)) {
        UIUtil.dispatchAllInvocationEvents();
    }
    assertTrue(getPsiDocumentManager().isCommitted(document));
    assertEquals(2, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockPsiFile(com.intellij.mock.MockPsiFile) Semaphore(com.intellij.util.concurrency.Semaphore) Document(com.intellij.openapi.editor.Document) MockDocument(com.intellij.mock.MockDocument)

Example 22 with Semaphore

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

the class SMTRunnerConsoleTest method testEnsureOrderedClearFlush.

public void testEnsureOrderedClearFlush() throws Exception {
    StringBuffer buf = new StringBuffer();
    String expected = "";
    for (int i = 0; i < 100; i++) {
        expected += "1";
        expected += "2";
        CompositePrintable.invokeInAlarm(() -> buf.append("1"), false);
        CompositePrintable.invokeInAlarm(() -> buf.append("2"), false);
    }
    Semaphore s = new Semaphore();
    s.down();
    CompositePrintable.invokeInAlarm(s::up, false);
    assertTrue(s.waitFor(1000));
    assertEquals(expected, buf.toString());
}
Also used : ProcessOutputTypes(com.intellij.execution.process.ProcessOutputTypes) com.intellij.execution.testframework.sm.runner.events(com.intellij.execution.testframework.sm.runner.events) TestConsoleProperties(com.intellij.execution.testframework.TestConsoleProperties) Semaphore(com.intellij.util.concurrency.Semaphore)

Example 23 with Semaphore

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

the class TransferToEDTQueue method waitFor.

// blocks until all elements in the queue are processed
public void waitFor() {
    final Semaphore semaphore = new Semaphore();
    semaphore.down();
    schedule(new Runnable() {

        @Override
        public void run() {
            semaphore.up();
        }
    });
    semaphore.waitFor();
}
Also used : Semaphore(com.intellij.util.concurrency.Semaphore)

Example 24 with Semaphore

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

the class ChangeListManagerImpl method freeze.

public void freeze(@NotNull String reason) {
    myUpdater.setIgnoreBackgroundOperation(true);
    Semaphore sem = new Semaphore();
    sem.down();
    invokeAfterUpdate(() -> {
        myUpdater.setIgnoreBackgroundOperation(false);
        myUpdater.pause();
        myFreezeName.set(reason);
        sem.up();
    }, InvokeAfterUpdateMode.SILENT_CALLBACK_POOLED, "", ModalityState.defaultModalityState());
    boolean free = false;
    while (!free) {
        ProgressIndicator pi = ProgressManager.getInstance().getProgressIndicator();
        if (pi != null)
            pi.checkCanceled();
        free = sem.waitFor(500);
    }
}
Also used : Semaphore(com.intellij.util.concurrency.Semaphore)

Example 25 with Semaphore

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

the class SmoothProgressAdapter method stop.

@Override
public synchronized void stop() {
    if (myOriginal.isRunning()) {
        myOriginal.stop();
    } else {
        myStartupAlarm.cancel(false);
        if (!myOriginalStarted && myOriginal instanceof Disposable) {
            // dispose original because start & stop were not called so original progress might not have released its resources 
            Disposer.dispose(((Disposable) myOriginal));
        }
    }
    // needed only for correct assertion of !progress.isRunning() in ApplicationImpl.runProcessWithProgressSynchroniously
    final Semaphore semaphore = new Semaphore();
    semaphore.down();
    SwingUtilities.invokeLater(() -> {
        semaphore.waitFor();
        if (myDialog != null) {
            //System.out.println("myDialog.destroyProcess()");
            myDialog.close(DialogWrapper.OK_EXIT_CODE);
            myDialog = null;
        }
    });
    try {
        // should be last to not leaveModal before closing the dialog
        super.stop();
    } finally {
        semaphore.up();
    }
}
Also used : Disposable(com.intellij.openapi.Disposable) 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