From ab710c2e18a93dd1dbd55bb91179be19a2dbf364 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Fri, 21 Sep 2012 14:33:47 +0400 Subject: [PATCH] invoke shutdown hook in the EDT if needed --- .../util/indexing/FileBasedIndexImpl.java | 70 +++++++++++-------- .../history/integration/LocalHistoryImpl.java | 6 +- .../openapi/util/ShutDownTracker.java | 11 +-- 3 files changed, 49 insertions(+), 38 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java index b6b78a22b4be..4d4ac768e1f4 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java @@ -68,6 +68,7 @@ import com.intellij.util.io.DataOutputStream; import com.intellij.util.io.storage.HeavyProcessLatch; import com.intellij.util.messages.MessageBus; import com.intellij.util.messages.MessageBusConnection; +import com.intellij.util.ui.UIUtil; import gnu.trove.*; import jsr166e.SequenceLock; import org.jetbrains.annotations.NonNls; @@ -605,39 +606,44 @@ public class FileBasedIndexImpl extends FileBasedIndex { private final AtomicBoolean myShutdownPerformed = new AtomicBoolean(false); private void performShutdown() { - if (!myShutdownPerformed.compareAndSet(false, true)) { - return; // already shut down - } - try { - if (myFlushingFuture != null) { - myFlushingFuture.cancel(false); - myFlushingFuture = null; - } - - myFileDocumentManager.saveAllDocuments(); - } - finally { - LOG.info("START INDEX SHUTDOWN"); - try { - myChangedFilesCollector.forceUpdate(null, null, null, true); - - for (ID indexId : myIndices.keySet()) { - final UpdatableIndex index = getIndex(indexId); - assert index != null; - checkRebuild(indexId, true); // if the index was scheduled for rebuild, only clean it - //LOG.info("DISPOSING " + indexId); - index.dispose(); + ShutDownTracker.invokeAndWait(false, true, new Runnable() { + @Override + public void run() { + if (!myShutdownPerformed.compareAndSet(false, true)) { + return; // already shut down } + try { + if (myFlushingFuture != null) { + myFlushingFuture.cancel(false); + myFlushingFuture = null; + } - myVfManager.removeVirtualFileListener(myChangedFilesCollector); + myFileDocumentManager.saveAllDocuments(); + } + finally { + LOG.info("START INDEX SHUTDOWN"); + try { + myChangedFilesCollector.forceUpdate(null, null, null, true); - //FileUtil.delete(getMarkerFile()); + for (ID indexId : myIndices.keySet()) { + final UpdatableIndex index = getIndex(indexId); + assert index != null; + checkRebuild(indexId, true); // if the index was scheduled for rebuild, only clean it + //LOG.info("DISPOSING " + indexId); + index.dispose(); + } + + myVfManager.removeVirtualFileListener(myChangedFilesCollector); + + //FileUtil.delete(getMarkerFile()); + } + catch (Throwable e) { + LOG.error("Problems during index shutdown", e); + } + LOG.info("END INDEX SHUTDOWN"); + } } - catch (Throwable e) { - LOG.error("Problems during index shutdown", e); - } - LOG.info("END INDEX SHUTDOWN"); - } + }); } private void flushAllIndices(final long modCount) { @@ -1034,6 +1040,8 @@ public class FileBasedIndexImpl extends FileBasedIndex { return data; } + long start = System.currentTimeMillis(); + final TIntHashSet filesSet = new TIntHashSet(); iterateIndexableFiles(new ContentIterator() { @Override @@ -1044,6 +1052,10 @@ public class FileBasedIndexImpl extends FileBasedIndex { }, project, ProgressManager.getInstance().getProgressIndicator()); ProjectIndexableFilesFilter filter = new ProjectIndexableFilesFilter(filesSet, myFilesModCount); project.putUserData(ourProjectFilesSetKey, new SoftReference(filter)); + + long finish = System.currentTimeMillis(); + LOG.debug(filesSet.size() + " files iterated in " + (finish - start) + " ms"); + return filter; } finally { diff --git a/platform/lvcs-impl/src/com/intellij/history/integration/LocalHistoryImpl.java b/platform/lvcs-impl/src/com/intellij/history/integration/LocalHistoryImpl.java index 632f549d5983..9fca8b0ddcc7 100644 --- a/platform/lvcs-impl/src/com/intellij/history/integration/LocalHistoryImpl.java +++ b/platform/lvcs-impl/src/com/intellij/history/integration/LocalHistoryImpl.java @@ -48,7 +48,6 @@ public class LocalHistoryImpl extends LocalHistory implements ApplicationCompone private LocalHistoryEventDispatcher myEventDispatcher; private final AtomicBoolean isInitialized = new AtomicBoolean(); - private Runnable myShutdownTask; public static LocalHistoryImpl getInstanceImpl() { return (LocalHistoryImpl)getInstance(); @@ -58,10 +57,11 @@ public class LocalHistoryImpl extends LocalHistory implements ApplicationCompone public void initComponent() { if (!ApplicationManager.getApplication().isUnitTestMode() && ApplicationManager.getApplication().isHeadlessEnvironment()) return; - myShutdownTask = new Runnable() { + Runnable myShutdownTask = new Runnable() { @Override public void run() { disposeComponent(); + ShutDownTracker.getInstance().unregisterShutdownTask(this); } }; ShutDownTracker.getInstance().registerShutdownTask(myShutdownTask); @@ -143,8 +143,6 @@ public class LocalHistoryImpl extends LocalHistory implements ApplicationCompone myChangeList.close(); LocalHistoryLog.LOG.info("Local history storage successfully closed."); - - ShutDownTracker.getInstance().unregisterShutdownTask(myShutdownTask); } @TestOnly diff --git a/platform/util/src/com/intellij/openapi/util/ShutDownTracker.java b/platform/util/src/com/intellij/openapi/util/ShutDownTracker.java index 5cddf3912004..af89263e617f 100644 --- a/platform/util/src/com/intellij/openapi/util/ShutDownTracker.java +++ b/platform/util/src/com/intellij/openapi/util/ShutDownTracker.java @@ -17,6 +17,7 @@ package com.intellij.openapi.util; import com.intellij.openapi.diagnostic.Logger; import com.intellij.util.concurrency.Semaphore; +import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.NotNull; import javax.swing.*; @@ -133,9 +134,9 @@ public class ShutDownTracker implements Runnable { return list.isEmpty()? null : list.removeLast(); } - public static void invokeAndWait(boolean timed, boolean edt, @NotNull final Runnable runnable) { - if (!edt) { - if (timed) { + public static void invokeAndWait(boolean returnOnTimeout, boolean runInEdt, @NotNull final Runnable runnable) { + if (!runInEdt) { + if (returnOnTimeout) { final Semaphore semaphore = new Semaphore(); semaphore.down(); new Thread(new Runnable() { @@ -153,7 +154,7 @@ public class ShutDownTracker implements Runnable { return; } - if (timed) { + if (returnOnTimeout) { final Semaphore semaphore = new Semaphore(); semaphore.down(); SwingUtilities.invokeLater(new Runnable() { @@ -168,7 +169,7 @@ public class ShutDownTracker implements Runnable { } try { - SwingUtilities.invokeAndWait(runnable); + UIUtil.invokeAndWaitIfNeeded(runnable); } catch (Exception e) { LOG.error(e);