Lazy: changes and clarifications after code-review.
LazyThreadSafety.NONE is actually *NONE* now.
This commit is contained in:
@@ -63,6 +63,7 @@ private object UNINITIALIZED_VALUE
|
||||
internal class SynchronizedLazyImpl<out T>(initializer: () -> T, lock: Any? = null) : Lazy<T>(), Serializable {
|
||||
private var initializer: (() -> T)? = initializer
|
||||
@Volatile private var _value: Any? = UNINITIALIZED_VALUE
|
||||
// final field is required to enable safe publication of constructed instance
|
||||
private val lock = lock ?: this
|
||||
|
||||
override val value: T
|
||||
@@ -101,7 +102,7 @@ internal class UnsafeLazyImpl<out T>(initializer: () -> T) : Lazy<T>(), Serializ
|
||||
get() {
|
||||
if (_value === UNINITIALIZED_VALUE) {
|
||||
_value = initializer!!()
|
||||
initializer == null
|
||||
initializer = null
|
||||
}
|
||||
return _value as T
|
||||
}
|
||||
|
||||
@@ -51,14 +51,19 @@ public fun lazy<T>(lock: Any?, initializer: () -> T): Lazy<T> = SynchronizedLazy
|
||||
private class SafePublicationLazyImpl<out T>(initializer: () -> T) : Lazy<T>(), Serializable {
|
||||
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
|
||||
|
||||
override val value: T
|
||||
get() {
|
||||
if (_value === UNINITIALIZED_VALUE) {
|
||||
val newValue = initializer!!()
|
||||
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 _value as T
|
||||
|
||||
Reference in New Issue
Block a user