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
This commit is contained in:
Ilya Gorbunov
2017-12-20 06:48:39 +03:00
parent 685b138f79
commit 5d62277fa5
+1 -1
View File
@@ -174,7 +174,7 @@ private class InitializedLazyImpl<out T>(override val value: T) : Lazy<T>, Seria
@kotlin.jvm.JvmVersion
private class SafePublicationLazyImpl<out T>(initializer: () -> T) : Lazy<T>, 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