Search in sources :

Example 11 with Semaphore

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

the class ApplicationImpl method runProcessWithProgressSynchronouslyInReadAction.

@Override
public boolean runProcessWithProgressSynchronouslyInReadAction(@Nullable final Project project, @NotNull final String progressTitle, final boolean canBeCanceled, final String cancelText, final JComponent parentComponent, @NotNull final Runnable process) {
    assertIsDispatchThread();
    boolean writeAccessAllowed = isWriteAccessAllowed();
    if (// Disallow running process in separate thread from under write action.
    writeAccessAllowed) // The thread will deadlock trying to get read action otherwise.
    {
        throw new IncorrectOperationException("Starting process with progress from within write action makes no sense");
    }
    final ProgressWindow progress = new ProgressWindow(canBeCanceled, false, project, parentComponent, cancelText);
    // in case of abrupt application exit when 'ProgressManager.getInstance().runProcess(process, progress)' below
    // does not have a chance to run, and as a result the progress won't be disposed
    Disposer.register(this, progress);
    progress.setTitle(progressTitle);
    final Semaphore readActionAcquired = new Semaphore();
    readActionAcquired.down();
    final Semaphore modalityEntered = new Semaphore();
    modalityEntered.down();
    executeOnPooledThread(() -> {
        try {
            ApplicationManager.getApplication().runReadAction(() -> {
                readActionAcquired.up();
                modalityEntered.waitFor();
                ProgressManager.getInstance().runProcess(process, progress);
            });
        } catch (ProcessCanceledException e) {
            progress.cancel();
        // ok to ignore.
        } catch (RuntimeException e) {
            progress.cancel();
            throw e;
        }
    });
    readActionAcquired.waitFor();
    progress.startBlocking(modalityEntered::up);
    LOG.assertTrue(!progress.isRunning());
    return !progress.isCanceled();
}
Also used : ProgressWindow(com.intellij.openapi.progress.util.ProgressWindow) Semaphore(com.intellij.util.concurrency.Semaphore)

Example 12 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 13 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)

Example 14 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 15 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)

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