[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.
This commit is contained in:
Artem Vasilev
2022-12-15 17:52:01 +01:00
committed by Space Team
parent 879f6c3432
commit 04924dbfed
@@ -27,7 +27,6 @@ internal class ValueWithPostCompute<KEY, VALUE, DATA>(
private var _calculate: ((KEY) -> Pair<VALUE, DATA>)? = calculate private var _calculate: ((KEY) -> Pair<VALUE, DATA>)? = calculate
private var _postCompute: ((KEY, VALUE, DATA) -> Unit)? = postCompute private var _postCompute: ((KEY, VALUE, DATA) -> Unit)? = postCompute
private var lock: ReentrantLock? = ReentrantLock() private var lock: ReentrantLock? = ReentrantLock()
private var guard: ThreadLocal<Boolean>? = ThreadLocal()
/** /**
* can be in one of the following three states: * can be in one of the following three states:
@@ -43,16 +42,10 @@ internal class ValueWithPostCompute<KEY, VALUE, DATA>(
private var value: Any? = ValueIsNotComputed private var value: Any? = ValueIsNotComputed
private inline fun <T> recursiveGuarded(body: () -> T): T { private inline fun <T> recursiveGuarded(body: () -> T): T {
val currentGuardValue = guard!!.get() check(lock!!.holdCount == 1) {
check(currentGuardValue == null || !currentGuardValue) {
"Should not be called recursively" "Should not be called recursively"
} }
guard!!.set(true) return body()
return try {
body()
} finally {
guard!!.set(false)
}
} }
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
@@ -134,7 +127,6 @@ internal class ValueWithPostCompute<KEY, VALUE, DATA>(
_calculate = null _calculate = null
_postCompute = null _postCompute = null
lock = null lock = null
guard = null
value = calculatedValue value = calculatedValue
return calculatedValue return calculatedValue