Fix race in Lazy for the new MM

This commit is contained in:
Alexander Shabalin
2021-06-28 15:03:46 +03:00
committed by Space
parent 455625bcee
commit 092750e215
3 changed files with 75 additions and 2 deletions
@@ -9,6 +9,8 @@ import kotlin.native.internal.Frozen
internal class FreezeAwareLazyImpl<out T>(initializer: () -> T) : Lazy<T> {
private val value_ = FreezableAtomicReference<Any?>(UNINITIALIZED)
// This cannot be made atomic because of the legacy MM. See https://github.com/JetBrains/kotlin-native/pull/3944
// So it must be protected by the lock below.
private var initializer_: (() -> T)? = initializer
private val lock_ = Lock()
@@ -46,9 +48,10 @@ internal class FreezeAwareLazyImpl<out T>(initializer: () -> T) : Lazy<T> {
override val value: T
get() {
return if (isFrozen) {
return if (isShareable()) {
// TODO: This is probably a big performance problem for lazy with the new MM. Address it.
locked(lock_) {
getOrInit(true)
getOrInit(isFrozen)
}
} else {
getOrInit(false)