invoke shutdown hook in the EDT if needed

This commit is contained in:
Alexey Kudravtsev
2012-09-24 13:42:52 +04:00
parent 86eca6d84a
commit ab710c2e18
3 changed files with 49 additions and 38 deletions
@@ -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<?, ?, FileContent> 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<?, ?, FileContent> 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<ProjectIndexableFilesFilter>(filter));
long finish = System.currentTimeMillis();
LOG.debug(filesSet.size() + " files iterated in " + (finish - start) + " ms");
return filter;
}
finally {
@@ -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
@@ -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);