diff --git a/compiler/frontend/src/org/jetbrains/kotlin/context/context.kt b/compiler/frontend/src/org/jetbrains/kotlin/context/context.kt index d17bbc81a6d..a1cf143871a 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/context/context.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/context/context.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.context +import com.intellij.openapi.progress.ProcessCanceledException import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.project.Project import org.jetbrains.kotlin.builtins.KotlinBuiltIns @@ -86,9 +87,9 @@ class MutableModuleContextImpl( fun GlobalContext(debugName: String): GlobalContextImpl { val tracker = ExceptionTracker() - return GlobalContextImpl(LockBasedStorageManager.createWithExceptionHandling(debugName, tracker) { + return GlobalContextImpl(LockBasedStorageManager.createWithExceptionHandling(debugName, tracker, { ProgressManager.checkCanceled() - }, tracker) + }, { throw ProcessCanceledException(it) }), tracker) } fun ProjectContext(project: Project, debugName: String): ProjectContext = ProjectContextImpl(project, GlobalContext(debugName)) diff --git a/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java b/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java index e6cda7d6208..b1586900e55 100644 --- a/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java +++ b/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java @@ -66,17 +66,18 @@ public class LockBasedStorageManager implements StorageManager { @NotNull String debugText, @NotNull ExceptionHandlingStrategy exceptionHandlingStrategy ) { - return createWithExceptionHandling(debugText, exceptionHandlingStrategy, null); + return createWithExceptionHandling(debugText, exceptionHandlingStrategy, null, null); } @NotNull public static LockBasedStorageManager createWithExceptionHandling( @NotNull String debugText, @NotNull ExceptionHandlingStrategy exceptionHandlingStrategy, - @Nullable Runnable checkCancelled + @Nullable Runnable checkCancelled, + @Nullable Function1 interruptedExceptionHandler ) { return new LockBasedStorageManager(debugText, exceptionHandlingStrategy, - SimpleLock.Companion.simpleLock(checkCancelled)); + SimpleLock.Companion.simpleLock(checkCancelled, interruptedExceptionHandler)); } protected final SimpleLock lock; @@ -94,11 +95,15 @@ public class LockBasedStorageManager implements StorageManager { } public LockBasedStorageManager(String debugText) { - this(debugText, null); + this(debugText, (Runnable)null, (Function1)null); } - public LockBasedStorageManager(String debugText, @Nullable Runnable checkCancelled) { - this(debugText, ExceptionHandlingStrategy.THROW, SimpleLock.Companion.simpleLock(checkCancelled)); + public LockBasedStorageManager( + String debugText, + @Nullable Runnable checkCancelled, + @Nullable Function1 interruptedExceptionHandler + ) { + this(debugText, ExceptionHandlingStrategy.THROW, SimpleLock.Companion.simpleLock(checkCancelled, interruptedExceptionHandler)); } @Override diff --git a/core/util.runtime/src/org/jetbrains/kotlin/storage/locks.kt b/core/util.runtime/src/org/jetbrains/kotlin/storage/locks.kt index 7dfa73e415a..8d47c184f30 100644 --- a/core/util.runtime/src/org/jetbrains/kotlin/storage/locks.kt +++ b/core/util.runtime/src/org/jetbrains/kotlin/storage/locks.kt @@ -17,8 +17,12 @@ interface SimpleLock { fun unlock() companion object { - fun simpleLock(checkCancelled: Runnable? = null) = - checkCancelled?.let { CancellableSimpleLock(it) } ?: DefaultSimpleLock() + fun simpleLock(checkCancelled: Runnable? = null, interruptedExceptionHandler: ((InterruptedException) -> Unit)? = null) = + if (checkCancelled != null && interruptedExceptionHandler != null) { + CancellableSimpleLock(checkCancelled, interruptedExceptionHandler) + } else { + DefaultSimpleLock() + } } } @@ -47,13 +51,25 @@ open class DefaultSimpleLock(protected val lock: Lock = ReentrantLock()) : Simpl } -class CancellableSimpleLock(lock: Lock, private val checkCancelled: Runnable) : DefaultSimpleLock(lock) { - constructor(checkCancelled: Runnable) : this(checkCancelled = checkCancelled, lock = ReentrantLock()) +class CancellableSimpleLock( + lock: Lock, + private val checkCancelled: Runnable, + private val interruptedExceptionHandler: (InterruptedException) -> Unit +) : DefaultSimpleLock(lock) { + constructor(checkCancelled: Runnable, interruptedExceptionHandler: (InterruptedException) -> Unit) : this( + checkCancelled = checkCancelled, + lock = ReentrantLock(), + interruptedExceptionHandler = interruptedExceptionHandler + ) override fun lock() { - while (!lock.tryLock(CHECK_CANCELLATION_PERIOD_MS, TimeUnit.MILLISECONDS)) { - //ProgressIndicatorAndCompilationCanceledStatus.checkCanceled() - checkCancelled.run() + try { + while (!lock.tryLock(CHECK_CANCELLATION_PERIOD_MS, TimeUnit.MILLISECONDS)) { + //ProgressIndicatorAndCompilationCanceledStatus.checkCanceled() + checkCancelled.run() + } + } catch (e: InterruptedException) { + interruptedExceptionHandler(e) } } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/PerFileAnalysisCache.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/PerFileAnalysisCache.kt index 92fb7229d37..21421be87ad 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/PerFileAnalysisCache.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/PerFileAnalysisCache.kt @@ -57,9 +57,11 @@ internal class PerFileAnalysisCache(val file: KtFile, componentProvider: Compone private val cache = HashMap() private var fileResult: AnalysisResult? = null private val lock = ReentrantLock() - private val guardLock = CancellableSimpleLock(lock) { - ProgressIndicatorProvider.checkCanceled() - } + private val guardLock = CancellableSimpleLock(lock, + checkCancelled = { + ProgressIndicatorProvider.checkCanceled() + }, + interruptedExceptionHandler = { throw ProcessCanceledException(it) }) private fun check(element: KtElement) { checkWithAttachment(element.containingFile == file, { diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ProjectResolutionFacade.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ProjectResolutionFacade.kt index 1876520056d..702a8201192 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ProjectResolutionFacade.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/ProjectResolutionFacade.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.idea.caches.resolve +import com.intellij.openapi.progress.ProcessCanceledException import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.project.Project import com.intellij.psi.PsiElement @@ -51,9 +52,11 @@ internal class ProjectResolutionFacade( get() = globalContext.storageManager.compute { cachedValue.value } private val analysisResultsLock = ReentrantLock() - private val analysisResultsSimpleLock = CancellableSimpleLock(analysisResultsLock) { - ProgressManager.checkCanceled() - } + private val analysisResultsSimpleLock = CancellableSimpleLock(analysisResultsLock, + checkCancelled = { + ProgressManager.checkCanceled() + }, + interruptedExceptionHandler = { throw ProcessCanceledException(it) }) private val analysisResults = CachedValuesManager.getManager(project).createCachedValue( { diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/util/globalContextUtils.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/util/globalContextUtils.kt index f4d36dada8e..fd8ad10b980 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/util/globalContextUtils.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/util/globalContextUtils.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.idea.caches.resolve.util +import com.intellij.openapi.progress.ProcessCanceledException import com.intellij.openapi.project.Project import org.jetbrains.kotlin.context.GlobalContextImpl import org.jetbrains.kotlin.idea.project.libraryToSourceAnalysisEnabled @@ -24,9 +25,9 @@ private fun GlobalContextImpl.contextWithCompositeExceptionTracker(debugName: St private fun GlobalContextImpl.contextWithNewLockAndCompositeExceptionTracker(debugName: String): GlobalContextImpl { val newExceptionTracker = CompositeExceptionTracker(this.exceptionTracker) return GlobalContextImpl( - LockBasedStorageManager.createWithExceptionHandling(debugName, newExceptionTracker) { + LockBasedStorageManager.createWithExceptionHandling(debugName, newExceptionTracker, { ProgressIndicatorAndCompilationCanceledStatus.checkCanceled() - }, + }, { throw ProcessCanceledException(it) }), newExceptionTracker ) } diff --git a/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/resolve/scriptAnnotationsPreprocessing.kt b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/resolve/scriptAnnotationsPreprocessing.kt index a1ab4aa931c..4d973e75eea 100644 --- a/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/resolve/scriptAnnotationsPreprocessing.kt +++ b/plugins/scripting/scripting-compiler-impl/src/org/jetbrains/kotlin/scripting/resolve/scriptAnnotationsPreprocessing.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.scripting.resolve +import com.intellij.openapi.progress.ProcessCanceledException import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.project.Project import org.jetbrains.kotlin.builtins.DefaultBuiltIns @@ -41,9 +42,9 @@ internal fun String?.orAnonymous(kind: String = ""): String = internal fun constructAnnotation(psi: KtAnnotationEntry, targetClass: KClass, project: Project): Annotation { val module = ModuleDescriptorImpl( Name.special(""), - LockBasedStorageManager("scriptAnnotationsPreprocessing") { + LockBasedStorageManager("scriptAnnotationsPreprocessing", { ProgressManager.checkCanceled() - }, + }, { throw ProcessCanceledException(it) }), DefaultBuiltIns.Instance ) val evaluator = ConstantExpressionEvaluator(module, LanguageVersionSettingsImpl.DEFAULT, project)