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 = actual fun compareAndSet(expected: Long, newValue: Long): Boolean =
this._native.compareAndSet(expected, newValue) this._native.compareAndSet(expected, newValue)
actual fun compareAndExchange(expected: Long, newValue: Long): Long = actual fun compareAndExchange(expected: Long, newValue: Long): Long {
if (_Linkage._isNativeCompareAndExchangeExists) this._native.compareAndExchange(expected, newValue) if (_Linkage._isNativeCompareAndExchangeExists) {
else _Misc._compareAndExchange( return this._native.compareAndExchange(expected, newValue)
get = this._native::get, } else {
cas = this._native::compareAndSet, val a = this._native
expected = expected, newValue = newValue return _Misc._compareAndExchange(
) get = a::get,
cas = a::compareAndSet,
expected = expected, newValue = newValue
)
}
}
private object _Linkage { private object _Linkage {
@JvmField @JvmField

View File

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