Add InterruptedException handler to CancellableSimpleLock

#EA-220650 Fixed
This commit is contained in:
Vladimir Dolzhenko
2020-06-25 23:46:00 +02:00
committed by Vladimir Dolzhenko
parent a9444c386d
commit ab20b3e083
7 changed files with 54 additions and 25 deletions
@@ -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<InterruptedException, Unit> 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<InterruptedException, Unit> interruptedExceptionHandler
) {
this(debugText, ExceptionHandlingStrategy.THROW, SimpleLock.Companion.simpleLock(checkCancelled, interruptedExceptionHandler));
}
@Override
@@ -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)
}
}