From ce61bcf3a00dcfea22cba107c6bd6189696b2b82 Mon Sep 17 00:00:00 2001 From: joe Date: Fri, 12 Jul 2024 20:55:40 +0100 Subject: [PATCH] Use ReentrantReadWriteLock.read and write extension functions --- src/main/kotlin/util/psi-utils.kt | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/util/psi-utils.kt b/src/main/kotlin/util/psi-utils.kt index be6bd764d..548ea40ed 100644 --- a/src/main/kotlin/util/psi-utils.kt +++ b/src/main/kotlin/util/psi-utils.kt @@ -75,8 +75,9 @@ import com.intellij.util.IncorrectOperationException import com.siyeh.ig.psiutils.ImportUtils import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentMap -import java.util.concurrent.locks.ReadWriteLock import java.util.concurrent.locks.ReentrantReadWriteLock +import kotlin.concurrent.read +import kotlin.concurrent.write // Parent fun PsiElement.findModule(): Module? = ModuleUtilCore.findModuleForPsiElement(this) @@ -254,7 +255,7 @@ inline fun PsiElement.cached(vararg dependencies: Any, crossinline compute: } @PublishedApi -internal val CACHE_LOCKS_KEY = Key.create, ReadWriteLock>>("mcdev.cacheLock") +internal val CACHE_LOCKS_KEY = Key.create, ReentrantReadWriteLock>>("mcdev.cacheLock") inline fun PsiElement.lockedCached( key: Key>, @@ -264,18 +265,14 @@ inline fun PsiElement.lockedCached( val cacheLocks = (this as UserDataHolderEx).putUserDataIfAbsent(CACHE_LOCKS_KEY, ConcurrentHashMap()) val cacheLock = cacheLocks.computeIfAbsent(key) { ReentrantReadWriteLock() } - cacheLock.readLock().lock() - try { + cacheLock.read { val value = getUserData(key)?.upToDateOrNull if (value != null) { return value.get() } - } finally { - cacheLock.readLock().unlock() } - cacheLock.writeLock().lock() - try { + cacheLock.write { val value = getUserData(key)?.upToDateOrNull if (value != null) { return value.get() @@ -284,8 +281,6 @@ inline fun PsiElement.lockedCached( return CachedValuesManager.getCachedValue(this, key) { CachedValueProvider.Result.create(compute(), *(dependencies.toList() + this).toTypedArray()) } - } finally { - cacheLock.writeLock().unlock() } }