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