From a71fc83df203484b98b33949bea3af69d73879c7 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 3 Sep 2012 15:47:39 +0200 Subject: [PATCH] [jeka] cleaner code and debug info in the dumb mode area --- .../openapi/project/CacheUpdateRunner.java | 43 ++++++++------- .../openapi/project/DumbServiceImpl.java | 53 +++++++++++-------- .../openapi/project/FileContentQueue.java | 34 +++++------- 3 files changed, 68 insertions(+), 62 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/project/CacheUpdateRunner.java b/platform/platform-impl/src/com/intellij/openapi/project/CacheUpdateRunner.java index 78d404a4d60d..6b122d2f9f01 100644 --- a/platform/platform-impl/src/com/intellij/openapi/project/CacheUpdateRunner.java +++ b/platform/platform-impl/src/com/intellij/openapi/project/CacheUpdateRunner.java @@ -157,24 +157,7 @@ class CacheUpdateRunner { Runnable process = new MyRunnable(innerIndicator, queue, ref, progressUpdater, processInReadAction, application); futures[i] = ApplicationManager.getApplication().executeOnPooledThread(getProcessWrapper(process)); } - try { - for (Future future : futures) { - future.get(); - } - - boolean allFinished = true; - for (Ref ref : finishedRefs) { - if (!(Boolean)ref.get()) { - allFinished = false; - break; - } - } - isFinished.set(allFinished); - - } - catch (Throwable throwable) { - LOG.error(throwable); - } + isFinished.set(waitForAll(finishedRefs, futures)); } } finally { @@ -184,6 +167,28 @@ class CacheUpdateRunner { return isFinished.get(); } + private static boolean waitForAll(Ref[] finishedRefs, Future[] futures) { + try { + for (Future future : futures) { + future.get(); + } + + boolean allFinished = true; + for (Ref ref : finishedRefs) { + if (!(Boolean)ref.get()) { + allFinished = false; + break; + } + } + return allFinished; + + } + catch (Throwable throwable) { + LOG.error(throwable); + } + return false; + } + private class MyRunnable implements Runnable { private final ProgressIndicatorBase myInnerIndicator; private final FileContentQueue myQueue; @@ -210,7 +215,7 @@ class CacheUpdateRunner { if (myProject.isDisposed()) return; if (myInnerIndicator.isCanceled()) return; - final FileContent fileContent = myQueue.take(); + final FileContent fileContent = myQueue.take(myInnerIndicator); if (fileContent == null) { myFinished.set(Boolean.TRUE); return; diff --git a/platform/platform-impl/src/com/intellij/openapi/project/DumbServiceImpl.java b/platform/platform-impl/src/com/intellij/openapi/project/DumbServiceImpl.java index 654805d7ab88..e4fa41300bee 100644 --- a/platform/platform-impl/src/com/intellij/openapi/project/DumbServiceImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/project/DumbServiceImpl.java @@ -358,30 +358,12 @@ public class DumbServiceImpl extends DumbService { } finally { myProcessedItems += count; + if (LOG.isDebugEnabled()) { + LOG.debug("Scheduling checkNextUpdateFromQueue"); + } UIUtil.invokeLaterIfNeeded(new DumbAwareRunnable() { public void run() { - IndexUpdateRunnable nextUpdateRunnable = null; - try { - nextUpdateRunnable = myUpdatesQueue.isEmpty()? null : myUpdatesQueue.pullFirst(); - if (nextUpdateRunnable == null) { - // really terminate the task - myActionQueue.offer(NULL_ACTION); - } - else { - //run next dumb action - // run next action under already existing progress indicator - myActionQueue.offer(new Ref(nextUpdateRunnable.myAction)); - } - } - catch (Throwable e) { - myActionQueue.offer(NULL_ACTION); - LOG.info(e); - } - finally { - if (nextUpdateRunnable == null) { - updateFinished(); - } - } + checkNextUpdateFromQueue(); } }); @@ -406,6 +388,33 @@ public class DumbServiceImpl extends DumbService { while (updateRunner != null); } + private void checkNextUpdateFromQueue() { + if (LOG.isDebugEnabled()) { + LOG.debug("Inside checkNextUpdateFromQueue"); + } + IndexUpdateRunnable nextUpdateRunnable = null; + try { + nextUpdateRunnable = myUpdatesQueue.isEmpty()? null : myUpdatesQueue.pullFirst(); + if (nextUpdateRunnable == null) { + // really terminate the task + myActionQueue.offer(NULL_ACTION); + } + else { + //run next dumb action + // run next action under already existing progress indicator + myActionQueue.offer(new Ref(nextUpdateRunnable.myAction)); + } + } + catch (Throwable e) { + myActionQueue.offer(NULL_ACTION); + LOG.info(e); + } + finally { + if (nextUpdateRunnable == null) { + updateFinished(); + } + } + } }); } } diff --git a/platform/platform-impl/src/com/intellij/openapi/project/FileContentQueue.java b/platform/platform-impl/src/com/intellij/openapi/project/FileContentQueue.java index 3bf77620dd7c..ce7b78a3a9f2 100644 --- a/platform/platform-impl/src/com/intellij/openapi/project/FileContentQueue.java +++ b/platform/platform-impl/src/com/intellij/openapi/project/FileContentQueue.java @@ -20,7 +20,6 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.progress.ProgressIndicator; -import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.vfs.InvalidVirtualFileAccessException; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; @@ -51,7 +50,7 @@ public class FileContentQueue { private final Queue myPushbackBuffer = new ArrayDeque(); private volatile boolean myContentLoadingThreadTerminated = false; - public void queue(final Collection files, @Nullable final ProgressIndicator indicator) { + public void queue(final Collection files, @NotNull final ProgressIndicator indicator) { final Runnable contentLoadingRunnable = new Runnable() { public void run() { try { @@ -59,7 +58,7 @@ public class FileContentQueue { if (indicator != null) { indicator.checkCanceled(); } - addLast(file); + addLast(file, indicator); } // put end-of-queue marker only if not canceled @@ -85,11 +84,11 @@ public class FileContentQueue { ApplicationManager.getApplication().executeOnPooledThread(contentLoadingRunnable); } - private void addLast(VirtualFile file) throws InterruptedException { + private void addLast(VirtualFile file, @NotNull final ProgressIndicator indicator) throws InterruptedException { FileContent content = new FileContent(file); if (file.isValid() && !file.isDirectory()) { - if (!doLoadContent(content)) { + if (!doLoadContent(content, indicator)) { content.setEmptyContent(); } } @@ -100,17 +99,14 @@ public class FileContentQueue { myQueue.put(content); } - private boolean doLoadContent(final FileContent content) throws InterruptedException { - final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator(); + private boolean doLoadContent(final FileContent content, @NotNull final ProgressIndicator indicator) throws InterruptedException { final long contentLength = content.getLength(); boolean counterUpdated = false; try { synchronized (this) { while (myTotalSize > SIZE_THRESHOLD) { - if (indicator != null) { - indicator.checkCanceled(); - } + indicator.checkCanceled(); wait(300L); } myTotalSize += contentLength; @@ -144,21 +140,17 @@ public class FileContentQueue { } @Nullable - public FileContent take() { - + public FileContent take(@NotNull ProgressIndicator indicator) { FileContent content = doTake(); if (content != null) { final long length = content.getLength(); while (true) { - final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator(); - if (indicator != null) { - try { - indicator.checkCanceled(); - } - catch (ProcessCanceledException e) { - pushback(content); - throw e; - } + try { + indicator.checkCanceled(); + } + catch (ProcessCanceledException e) { + pushback(content); + throw e; } synchronized (this) { boolean requestingLargeSize = length > LARGE_SIZE_REQUEST_THRESHOLD;