[LL FIR] allow recursive access to the same FirThreadSafeCache

^KT-56721 fixed
This commit is contained in:
Ilya Kirillov
2023-02-16 14:10:26 +01:00
committed by Space Team
parent d670e5a695
commit c60fd38e05
4 changed files with 11 additions and 10 deletions
@@ -13,17 +13,9 @@ internal class FirThreadSafeCache<K : Any, V, CONTEXT>(
private val createValue: (K, CONTEXT) -> V
) : FirCache<K, V, CONTEXT>() {
private val updating: ThreadLocal<Boolean> = ThreadLocal.withInitial { false }
override fun getValue(key: K, context: CONTEXT): V =
map.getOrPutWithNullableValue(key) {
if (updating.get()) error("Recursive update")
try {
updating.set(true)
createValue(it, context)
} finally {
updating.set(false)
}
createValue(it, context)
}
override fun getValueIfComputed(key: K): V? =
@@ -19,6 +19,6 @@ internal inline fun <KEY : Any, RESULT> ConcurrentMap<KEY, Any>.getOrPutWithNull
key: KEY,
crossinline compute: (KEY) -> Any?
): RESULT {
val value = computeIfAbsent(key) { compute(key) ?: NullValue }
val value = getOrPut(key) { compute(key) ?: NullValue }
return value.nullValueToNull()
}