[refactoring] IJPL-3459: (followup) replace .isShutdownHookRunning() with .isShutdownStarted()

+ make ShutDownTracker internal

GitOrigin-RevId: e6954bf75e2e7b91436684b15eb96ea1798a2745
This commit is contained in:
Ruslan Cheremin
2024-07-15 19:58:02 +02:00
committed by intellij-monorepo-bot
parent 8db5ce2793
commit ef82709c51
5 changed files with 20 additions and 22 deletions

View File

@@ -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) {

View File

@@ -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);

View File

@@ -177,7 +177,7 @@ public class NativeFileWatcherImpl extends PluggableFileWatcher {
if (myIsShuttingDown) {
return;
}
if (ShutDownTracker.isShutdownHookRunning()) {
if (ShutDownTracker.isShutdownStarted()) {
myIsShuttingDown = true;
return;
}

View File

@@ -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
- <init>():V

View File

@@ -32,6 +32,7 @@ import java.util.concurrent.TimeUnit;
* regular ways of resource management there possible.
* </p>
*/
@ApiStatus.Internal
public final class ShutDownTracker {
private final Deque<Runnable> myShutdownTasks = new ConcurrentLinkedDeque<>();
private final Deque<Runnable> 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.
* <p>
* <b>BEWARE</b>: 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));
}