Make FreezableAtomicReference atomic when unfrozen for the new MM.
This commit is contained in:
committed by
Space
parent
518e8691b7
commit
28b6427d3e
@@ -182,7 +182,9 @@ void Kotlin_AtomicReference_checkIfFrozen(KRef value) {
|
||||
}
|
||||
|
||||
OBJ_GETTER(Kotlin_AtomicReference_compareAndSwap, KRef thiz, KRef expectedValue, KRef newValue) {
|
||||
Kotlin_AtomicReference_checkIfFrozen(newValue);
|
||||
if (isPermanentOrFrozen(thiz)) {
|
||||
Kotlin_AtomicReference_checkIfFrozen(newValue);
|
||||
}
|
||||
// See Kotlin_AtomicReference_get() for explanations, why locking is needed.
|
||||
AtomicReferenceLayout* ref = asAtomicReference(thiz);
|
||||
RETURN_RESULT_OF(SwapHeapRefLocked, &ref->value_, expectedValue, newValue,
|
||||
@@ -190,7 +192,9 @@ OBJ_GETTER(Kotlin_AtomicReference_compareAndSwap, KRef thiz, KRef expectedValue,
|
||||
}
|
||||
|
||||
KBoolean Kotlin_AtomicReference_compareAndSet(KRef thiz, KRef expectedValue, KRef newValue) {
|
||||
Kotlin_AtomicReference_checkIfFrozen(newValue);
|
||||
if (isPermanentOrFrozen(thiz)) {
|
||||
Kotlin_AtomicReference_checkIfFrozen(newValue);
|
||||
}
|
||||
// See Kotlin_AtomicReference_get() for explanations, why locking is needed.
|
||||
AtomicReferenceLayout* ref = asAtomicReference(thiz);
|
||||
ObjHolder holder;
|
||||
@@ -200,7 +204,9 @@ KBoolean Kotlin_AtomicReference_compareAndSet(KRef thiz, KRef expectedValue, KRe
|
||||
}
|
||||
|
||||
void Kotlin_AtomicReference_set(KRef thiz, KRef newValue) {
|
||||
Kotlin_AtomicReference_checkIfFrozen(newValue);
|
||||
if (isPermanentOrFrozen(thiz)) {
|
||||
Kotlin_AtomicReference_checkIfFrozen(newValue);
|
||||
}
|
||||
AtomicReferenceLayout* ref = asAtomicReference(thiz);
|
||||
SetHeapRefLocked(&ref->value_, newValue, &ref->lock_, &ref->cookie_);
|
||||
}
|
||||
|
||||
@@ -326,7 +326,7 @@ public class FreezableAtomicReference<T>(private var value_: T) {
|
||||
public var value: T
|
||||
get() = @Suppress("UNCHECKED_CAST")(getImpl() as T)
|
||||
set(new) {
|
||||
if (this.isFrozen)
|
||||
if (this.isShareable())
|
||||
setImpl(new)
|
||||
else
|
||||
value_ = new
|
||||
@@ -342,7 +342,9 @@ public class FreezableAtomicReference<T>(private var value_: T) {
|
||||
* @return the old value
|
||||
*/
|
||||
public fun compareAndSwap(expected: T, new: T): T {
|
||||
return if (this.isFrozen) @Suppress("UNCHECKED_CAST")(compareAndSwapImpl(expected, new) as T) else {
|
||||
return if (this.isShareable()) {
|
||||
@Suppress("UNCHECKED_CAST")(compareAndSwapImpl(expected, new) as T)
|
||||
} else {
|
||||
val old = value_
|
||||
if (old === expected) value_ = new
|
||||
old
|
||||
@@ -358,7 +360,8 @@ public class FreezableAtomicReference<T>(private var value_: T) {
|
||||
* @return true if successful
|
||||
*/
|
||||
public fun compareAndSet(expected: T, new: T): Boolean {
|
||||
if (this.isFrozen) return compareAndSetImpl(expected, new)
|
||||
if (this.isShareable())
|
||||
return compareAndSetImpl(expected, new)
|
||||
val old = value_
|
||||
if (old === expected) {
|
||||
value_ = new
|
||||
|
||||
@@ -14,6 +14,9 @@ import kotlin.native.identityHashCode
|
||||
import kotlin.reflect.KClass
|
||||
import kotlinx.cinterop.*
|
||||
|
||||
@GCUnsafeCall("Kotlin_Any_isShareable")
|
||||
external internal fun Any?.isShareable(): Boolean
|
||||
|
||||
// Implementation details.
|
||||
|
||||
@GCUnsafeCall("Kotlin_Worker_stateOfFuture")
|
||||
|
||||
@@ -125,8 +125,5 @@ private class CleanerImpl(
|
||||
private val cleanPtr: NativePtr,
|
||||
): Cleaner {}
|
||||
|
||||
@GCUnsafeCall("Kotlin_Any_isShareable")
|
||||
external private fun Any?.isShareable(): Boolean
|
||||
|
||||
@GCUnsafeCall("CreateStablePointer")
|
||||
external private fun createStablePointer(obj: Any): NativePtr
|
||||
|
||||
@@ -82,7 +82,8 @@ void ObjHeader::destroyMetaObject(ObjHeader* object) {
|
||||
}
|
||||
|
||||
ALWAYS_INLINE bool isPermanentOrFrozen(const ObjHeader* obj) {
|
||||
return mm::IsFrozen(obj);
|
||||
// TODO: Freeze TF_IMMUTABLE objects upon creation.
|
||||
return mm::IsFrozen(obj) || ((obj->type_info()->flags_ & TF_IMMUTABLE) != 0);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE bool isShareable(const ObjHeader* obj) {
|
||||
|
||||
Reference in New Issue
Block a user