From 5d62277fa5553d322093e81c6e5bce031228decc Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 20 Dec 2017 06:48:39 +0300 Subject: [PATCH] Make SafePublicationLazyImpl.initializer volatile This establishes happens-before relation between nulling out the initializer and checking whether it is null. This will prevent the subsequent _value reads being reordered before the initializer reads. #KT-21868 Fixed --- libraries/stdlib/src/kotlin/util/Lazy.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/stdlib/src/kotlin/util/Lazy.kt b/libraries/stdlib/src/kotlin/util/Lazy.kt index ce064baa137..9b0ee761e2f 100644 --- a/libraries/stdlib/src/kotlin/util/Lazy.kt +++ b/libraries/stdlib/src/kotlin/util/Lazy.kt @@ -174,7 +174,7 @@ private class InitializedLazyImpl(override val value: T) : Lazy, Seria @kotlin.jvm.JvmVersion private class SafePublicationLazyImpl(initializer: () -> T) : Lazy, Serializable { - private var initializer: (() -> T)? = initializer + @Volatile private var initializer: (() -> T)? = initializer @Volatile private var _value: Any? = UNINITIALIZED_VALUE // this final field is required to enable safe publication of constructed instance private val final: Any = UNINITIALIZED_VALUE