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();
}
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);
}
}
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();
}
}
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();
}
}
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));
}
Aggregations