Do not read volatile _value field second time it is already initialized
This commit is contained in:
@@ -181,14 +181,19 @@ private class SafePublicationLazyImpl<out T>(initializer: () -> T) : Lazy<T>, Se
|
|||||||
|
|
||||||
override val value: T
|
override val value: T
|
||||||
get() {
|
get() {
|
||||||
if (_value === UNINITIALIZED_VALUE) {
|
val value = _value
|
||||||
val initializerValue = initializer
|
if (value !== UNINITIALIZED_VALUE) {
|
||||||
// if we see null in initializer here, it means that the value is already set by another thread
|
@Suppress("UNCHECKED_CAST")
|
||||||
if (initializerValue != null) {
|
return value as T
|
||||||
val newValue = initializerValue()
|
}
|
||||||
if (valueUpdater.compareAndSet(this, UNINITIALIZED_VALUE, newValue)) {
|
|
||||||
initializer = null
|
val initializerValue = initializer
|
||||||
}
|
// if we see null in initializer here, it means that the value is already set by another thread
|
||||||
|
if (initializerValue != null) {
|
||||||
|
val newValue = initializerValue()
|
||||||
|
if (valueUpdater.compareAndSet(this, UNINITIALIZED_VALUE, newValue)) {
|
||||||
|
initializer = null
|
||||||
|
return newValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
|||||||
Reference in New Issue
Block a user