Avoiding double access to field in 'compareAndExchange' inline implementation

This commit is contained in:
Andrew Golovashevich 2025-03-26 19:01:54 +03:00
parent fe6d2c22ef
commit d5ed25cb54
2 changed files with 18 additions and 11 deletions

View File

@ -30,13 +30,18 @@ internal actual /*value*/ class _AtomicLong {
actual fun compareAndSet(expected: Long, newValue: Long): Boolean =
this._native.compareAndSet(expected, newValue)
actual fun compareAndExchange(expected: Long, newValue: Long): Long =
if (_Linkage._isNativeCompareAndExchangeExists) this._native.compareAndExchange(expected, newValue)
else _Misc._compareAndExchange(
get = this._native::get,
cas = this._native::compareAndSet,
actual fun compareAndExchange(expected: Long, newValue: Long): Long {
if (_Linkage._isNativeCompareAndExchangeExists) {
return this._native.compareAndExchange(expected, newValue)
} else {
val a = this._native
return _Misc._compareAndExchange(
get = a::get,
cas = a::compareAndSet,
expected = expected, newValue = newValue
)
}
}
private object _Linkage {
@JvmField

View File

@ -24,10 +24,12 @@ internal actual class _AtomicLong {
actual fun compareAndSet(expected: Long, newValue: Long): Boolean =
this._atomicfu.compareAndSet(expected, newValue)
actual fun compareAndExchange(expected: Long, newValue: Long): Long =
_Misc._compareAndExchange(
get = this._atomicfu::value::get,
cas = this._atomicfu::compareAndSet,
actual fun compareAndExchange(expected: Long, newValue: Long): Long {
val a = this._atomicfu
return _Misc._compareAndExchange(
get = a::value::get,
cas = a::compareAndSet,
expected = expected, newValue = newValue
)
}
}