From 04924dbfede7ebc84fec0d42de286bf3c11d16a2 Mon Sep 17 00:00:00 2001 From: Artem Vasilev Date: Thu, 15 Dec 2022 17:52:01 +0100 Subject: [PATCH] [LL FIR] replace guard with ReentrantLock.holdCount Previously, there was a guard variable that prevented recursive locks. It can be replaced with lock.holdCount check. --- .../low/level/api/fir/caches/ValueWithPostCompute.kt | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/ValueWithPostCompute.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/ValueWithPostCompute.kt index 6108fbee742..b91c5d42e14 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/ValueWithPostCompute.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/ValueWithPostCompute.kt @@ -27,7 +27,6 @@ internal class ValueWithPostCompute( private var _calculate: ((KEY) -> Pair)? = calculate private var _postCompute: ((KEY, VALUE, DATA) -> Unit)? = postCompute private var lock: ReentrantLock? = ReentrantLock() - private var guard: ThreadLocal? = ThreadLocal() /** * can be in one of the following three states: @@ -43,16 +42,10 @@ internal class ValueWithPostCompute( private var value: Any? = ValueIsNotComputed private inline fun recursiveGuarded(body: () -> T): T { - val currentGuardValue = guard!!.get() - check(currentGuardValue == null || !currentGuardValue) { + check(lock!!.holdCount == 1) { "Should not be called recursively" } - guard!!.set(true) - return try { - body() - } finally { - guard!!.set(false) - } + return body() } @Suppress("UNCHECKED_CAST") @@ -134,7 +127,6 @@ internal class ValueWithPostCompute( _calculate = null _postCompute = null lock = null - guard = null value = calculatedValue return calculatedValue