From ef82709c516fac841cf22be4c024eeecb2d43db2 Mon Sep 17 00:00:00 2001
From: Ruslan Cheremin
Date: Mon, 15 Jul 2024 19:58:02 +0200
Subject: [PATCH] [refactoring] IJPL-3459: (followup) replace
.isShutdownHookRunning() with .isShutdownStarted()
+ make ShutDownTracker internal
GitOrigin-RevId: e6954bf75e2e7b91436684b15eb96ea1798a2745
---
.../openapi/editor/impl/DocumentImpl.java | 6 ++---
.../editor/impl/TrailingSpacesStripper.java | 2 +-
.../vfs/impl/local/NativeFileWatcherImpl.java | 2 +-
platform/util/api-dump-unreviewed.txt | 6 -----
.../openapi/util/ShutDownTracker.java | 26 +++++++++++--------
5 files changed, 20 insertions(+), 22 deletions(-)
diff --git a/platform/core-impl/src/com/intellij/openapi/editor/impl/DocumentImpl.java b/platform/core-impl/src/com/intellij/openapi/editor/impl/DocumentImpl.java
index 54bd4fb55475..4bc933026d9e 100644
--- a/platform/core-impl/src/com/intellij/openapi/editor/impl/DocumentImpl.java
+++ b/platform/core-impl/src/com/intellij/openapi/editor/impl/DocumentImpl.java
@@ -1,4 +1,4 @@
-// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
+// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.openapi.editor.impl;
import com.intellij.core.CoreBundle;
@@ -875,7 +875,7 @@ public final class DocumentImpl extends UserDataHolderBase implements DocumentEx
getLineSet(); // initialize line set to track changed lines
- if (!ShutDownTracker.isShutdownHookRunning()) {
+ if (!ShutDownTracker.isShutdownStarted()) {
DocumentListener[] listeners = getListeners();
ProgressManager.getInstance().executeNonCancelableSection(() -> {
for (int i = listeners.length - 1; i >= 0; i--) {
@@ -916,7 +916,7 @@ public final class DocumentImpl extends UserDataHolderBase implements DocumentEx
myFrozen = null;
setModificationStamp(newModificationStamp);
- if (!ShutDownTracker.isShutdownHookRunning()) {
+ if (!ShutDownTracker.isShutdownStarted()) {
DocumentListener[] listeners = getListeners();
ProgressManager.getInstance().executeNonCancelableSection(() -> {
for (DocumentListener listener : listeners) {
diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/impl/TrailingSpacesStripper.java b/platform/platform-impl/src/com/intellij/openapi/editor/impl/TrailingSpacesStripper.java
index 71e01a9659aa..8a02d3dc13da 100644
--- a/platform/platform-impl/src/com/intellij/openapi/editor/impl/TrailingSpacesStripper.java
+++ b/platform/platform-impl/src/com/intellij/openapi/editor/impl/TrailingSpacesStripper.java
@@ -229,7 +229,7 @@ public final class TrailingSpacesStripper implements FileDocumentManagerListener
boolean markAsNeedsStrippingLater = ((DocumentImpl)document)
.stripTrailingSpaces(getProject(document, activeEditors), inChangedLinesOnly, skipCaretLines ? caretOffsets : null);
- if (!activeEditors.isEmpty() && !ShutDownTracker.isShutdownHookRunning()) {
+ if (!activeEditors.isEmpty() && !ShutDownTracker.isShutdownStarted()) {
runBatchCaretOperation(activeEditors, () -> {
for (int i = 0; i < carets.size(); i++) {
Caret caret = carets.get(i);
diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/NativeFileWatcherImpl.java b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/NativeFileWatcherImpl.java
index 1a746a9d021d..1714b2fd02db 100644
--- a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/NativeFileWatcherImpl.java
+++ b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/NativeFileWatcherImpl.java
@@ -177,7 +177,7 @@ public class NativeFileWatcherImpl extends PluggableFileWatcher {
if (myIsShuttingDown) {
return;
}
- if (ShutDownTracker.isShutdownHookRunning()) {
+ if (ShutDownTracker.isShutdownStarted()) {
myIsShuttingDown = true;
return;
}
diff --git a/platform/util/api-dump-unreviewed.txt b/platform/util/api-dump-unreviewed.txt
index 8bcc57e1274f..ab262c2f12e7 100644
--- a/platform/util/api-dump-unreviewed.txt
+++ b/platform/util/api-dump-unreviewed.txt
@@ -1087,12 +1087,6 @@ com.intellij.openapi.util.ScalableIcon
- javax.swing.Icon
- a:getScale():F
- a:scale(F):javax.swing.Icon
-f:com.intellij.openapi.util.ShutDownTracker
-- s:getInstance():com.intellij.openapi.util.ShutDownTracker
-- s:isShutdownHookRunning():Z
-- registerShutdownTask(java.lang.Runnable):V
-- unregisterShutdownTask(java.lang.Runnable):V
-- waitFor(J,java.util.concurrent.TimeUnit):Z
a:com.intellij.openapi.util.SimpleFieldCache
- com.intellij.openapi.util.FieldCache
- ():V
diff --git a/platform/util/src/com/intellij/openapi/util/ShutDownTracker.java b/platform/util/src/com/intellij/openapi/util/ShutDownTracker.java
index 5508ba7e9c43..50753d0b053a 100644
--- a/platform/util/src/com/intellij/openapi/util/ShutDownTracker.java
+++ b/platform/util/src/com/intellij/openapi/util/ShutDownTracker.java
@@ -32,6 +32,7 @@ import java.util.concurrent.TimeUnit;
* regular ways of resource management there possible.
*
*/
+@ApiStatus.Internal
public final class ShutDownTracker {
private final Deque myShutdownTasks = new ConcurrentLinkedDeque<>();
private final Deque myCachesShutdownTasks = new ConcurrentLinkedDeque<>();
@@ -50,12 +51,15 @@ public final class ShutDownTracker {
return ShutDownTrackerHolder.ourInstance;
}
- //FIXME RC: many clients use that method, even though it is unreliable: thread.isAlive is true only while
- // hook thread is running, it becomes false as soon as the thread finishes -- but clients use this method
- // to ask 'is shutdown _initiated_' so they expect it to return true from that moment and until app actually
- // terminates. shutdownSequenceIsStarted fits better for that goal
+ /** @deprecated use {@link #isShutdownStarted()} -- more correct and explicit */
+ @Deprecated
public static boolean isShutdownHookRunning() {
- return getInstance().myThread.isAlive();
+ return isShutdownStarted();
+ }
+
+ /** @return true if shutdown hook is started -- no more tasks could be added after that */
+ public static boolean isShutdownStarted() {
+ return getInstance().shutdownSequenceIsStarted;
}
/** true if shutdown thread is started: no shutdown tasks could be added after that */
@@ -91,16 +95,16 @@ public final class ShutDownTracker {
}
/**
- * FIXME RC: method terminates immediately if shutdown thread is not started yet (!isAlive) -- which makes
- * its use unreliable, since you can't be sure the thread is already started, and without it the method
- * could terminate immediately while hook is not even started
- *
- * Waits for shutdown tasks termination up to specified amount of time.
+ * If shutdown is started -- waits for shutdown tasks termination, up to specified amount of time.
+ *
+ * BEWARE: method terminates immediately if shutdown thread is not started yet (!isAlive) -- which
+ * makes its use unreliable, since you can't be sure the thread is already started, and without that the
+ * method could terminate immediately while hook is not even started.
*
* @return true if terminated inside given timeout
*/
public boolean waitFor(long timeout, @NotNull TimeUnit unit) {
- if (isShutdownHookRunning()) {
+ if (myThread.isAlive()) {
try {
myThread.join(unit.toMillis(timeout));
}