From 95aa3bde0a794cede1746838b43858fa8af0283e Mon Sep 17 00:00:00 2001 From: Maria Sokolova Date: Thu, 4 Apr 2024 12:45:21 +0200 Subject: [PATCH] Enable atomicfu-compiler-plugin transformations * Enabled JVM IR and Native IR transformations * Introduced required minor changes in code --- gradle.properties | 4 ++-- .../engine/curl/internal/CurlCallbacks.kt | 2 +- .../test/io/ktor/client/tests/EventsTest.kt | 10 +++++----- .../io/ktor/utils/io/ByteChannelSequential.kt | 6 +++--- .../src/io/ktor/utils/io/ByteBufferChannel.kt | 17 +++++++---------- .../ktor/network/sockets/tests/UDPSocketTest.kt | 10 +++++----- .../io/ktor/server/cio/CIOApplicationEngine.kt | 3 ++- .../ktor/util/network/NetworkAddressNative.kt | 4 +++- 8 files changed, 28 insertions(+), 28 deletions(-) diff --git a/gradle.properties b/gradle.properties index 4edd65e1177..dd4d75483ac 100644 --- a/gradle.properties +++ b/gradle.properties @@ -24,8 +24,8 @@ kotlin.incremental.multiplatform=true kotlin.mpp.applyDefaultHierarchyTemplate=false kotlin.native.ignoreIncorrectDependencies=true kotlin.native.binary.memoryModel=experimental -#kotlinx.atomicfu.enableJvmIrTransformation=true -#kotlinx.atomicfu.enableJsIrTransformation=true +kotlinx.atomicfu.enableJvmIrTransformation=true +kotlinx.atomicfu.enableNativeIrTransformation=true kotlin.daemon.jvmargs=-Xmx6g -XX:+HeapDumpOnOutOfMemoryError kotlin.daemon.useFallbackStrategy=false diff --git a/ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlCallbacks.kt b/ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlCallbacks.kt index 6b41b7a163a..b2f31182ed4 100644 --- a/ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlCallbacks.kt +++ b/ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlCallbacks.kt @@ -55,7 +55,7 @@ internal fun onBodyChunkReceived( return -1 } if (written > 0) { - wrapper.bytesWritten += written + wrapper.bytesWritten.addAndGet(written) } if (wrapper.bytesWritten.value == chunkSize) { wrapper.bytesWritten.value = 0 diff --git a/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/EventsTest.kt b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/EventsTest.kt index 069881ab938..55195d6d085 100644 --- a/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/EventsTest.kt +++ b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/EventsTest.kt @@ -14,11 +14,11 @@ import kotlinx.atomicfu.* import kotlin.test.* class EventsTest : ClientLoader() { - val created = atomic(0) - val ready = atomic(0) - val received = atomic(0) - val counter = atomic(0) - val cause: AtomicRef = atomic(null) + private val created = atomic(0) + private val ready = atomic(0) + private val received = atomic(0) + private val counter = atomic(0) + private val cause: AtomicRef = atomic(null) @Test fun testBasicEvents() = clientTests { diff --git a/ktor-io/common/src/io/ktor/utils/io/ByteChannelSequential.kt b/ktor-io/common/src/io/ktor/utils/io/ByteChannelSequential.kt index 715158cba05..5f322ed2ed3 100644 --- a/ktor-io/common/src/io/ktor/utils/io/ByteChannelSequential.kt +++ b/ktor-io/common/src/io/ktor/utils/io/ByteChannelSequential.kt @@ -840,9 +840,9 @@ public abstract class ByteChannelSequentialBase( private fun addBytesRead(count: Int) { require(count >= 0) { "Can't read negative amount of bytes: $count" } - channelSize.minusAssign(count) + channelSize.addAndGet(-count) _totalBytesRead.addAndGet(count.toLong()) - _availableForRead.minusAssign(count) + _availableForRead.addAndGet(-count) check(channelSize.value >= 0) { "Readable bytes count is negative: $availableForRead, $count in $this" } check(availableForRead >= 0) { "Readable bytes count is negative: $availableForRead, $count in $this" } @@ -851,7 +851,7 @@ public abstract class ByteChannelSequentialBase( private fun addBytesWritten(count: Int) { require(count >= 0) { "Can't write negative amount of bytes: $count" } - channelSize.plusAssign(count) + channelSize.addAndGet(count) _totalBytesWritten.addAndGet(count.toLong()) check(channelSize.value >= 0) { "Readable bytes count is negative: ${channelSize.value}, $count in $this" } diff --git a/ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt b/ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt index 145db6f1198..791e2ee03ab 100644 --- a/ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt +++ b/ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt @@ -2228,7 +2228,7 @@ internal open class ByteBufferChannel( continuation.resume(flush && hasEnoughBytes) return COROUTINE_SUSPENDED } - } while (!setContinuation({ readOp }, _readOp, continuation, { closed == null && readSuspendPredicate(size) })) + } while (!_readOp.setContinuation(continuation, { closed == null && readSuspendPredicate(size) })) return COROUTINE_SUSPENDED } @@ -2283,7 +2283,7 @@ internal open class ByteBufferChannel( ucont.resume(Unit) break } - } while (!setContinuation({ writeOp }, _writeOp, ucont.intercepted(), { writeSuspendPredicate(size) })) + } while (!_writeOp.setContinuation(ucont.intercepted(), { writeSuspendPredicate(size) })) flushImpl(minWriteSize = size) @@ -2329,7 +2329,7 @@ internal open class ByteBufferChannel( c.resume(Unit) break } - } while (!setContinuation({ writeOp }, _writeOp, c, { writeSuspendPredicate(size) })) + } while (!_writeOp.setContinuation(c, { writeSuspendPredicate(size) })) flushImpl(minWriteSize = size) @@ -2338,22 +2338,19 @@ internal open class ByteBufferChannel( } } - private inline fun > setContinuation( - getter: () -> C?, - updater: AtomicRef, + private inline fun > AtomicRef.setContinuation( continuation: C, predicate: () -> Boolean ): Boolean { - while (true) { - val current = getter() + loop { current -> check(current == null) { "Operation is already in progress" } if (!predicate()) { return false } - if (updater.compareAndSet(null, continuation)) { - return (predicate() || !updater.compareAndSet(continuation, null)) + if (this.compareAndSet(null, continuation)) { + return (predicate() || !this.compareAndSet(continuation, null)) } } } diff --git a/ktor-network/jvmAndNix/test/io/ktor/network/sockets/tests/UDPSocketTest.kt b/ktor-network/jvmAndNix/test/io/ktor/network/sockets/tests/UDPSocketTest.kt index 211061991ea..04e7248cb4b 100644 --- a/ktor-network/jvmAndNix/test/io/ktor/network/sockets/tests/UDPSocketTest.kt +++ b/ktor-network/jvmAndNix/test/io/ktor/network/sockets/tests/UDPSocketTest.kt @@ -112,12 +112,12 @@ class UDPSocketTest { .bind() socket.outgoing.invokeOnClose { - done += 1 + done.addAndGet(1) } assertFailsWith { socket.outgoing.invokeOnClose { - done += 2 + done.addAndGet(2) } } @@ -136,7 +136,7 @@ class UDPSocketTest { .bind() socket.outgoing.invokeOnClose { - done += 1 + done.addAndGet(1) assertTrue(it is AssertionError) } @@ -154,7 +154,7 @@ class UDPSocketTest { .bind() socket.outgoing.invokeOnClose { - done += 1 + done.addAndGet(1) } socket.close() @@ -174,7 +174,7 @@ class UDPSocketTest { socket.outgoing.close(AssertionError()) socket.outgoing.invokeOnClose { - done += 1 + done.addAndGet(1) assertTrue(it is AssertionError) } diff --git a/ktor-server/ktor-server-cio/jvmAndNix/src/io/ktor/server/cio/CIOApplicationEngine.kt b/ktor-server/ktor-server-cio/jvmAndNix/src/io/ktor/server/cio/CIOApplicationEngine.kt index 9a431db3e1d..7644440d002 100644 --- a/ktor-server/ktor-server-cio/jvmAndNix/src/io/ktor/server/cio/CIOApplicationEngine.kt +++ b/ktor-server/ktor-server-cio/jvmAndNix/src/io/ktor/server/cio/CIOApplicationEngine.kt @@ -51,7 +51,8 @@ public class CIOApplicationEngine( private val startupJob: CompletableDeferred = CompletableDeferred() private val stopRequest: CompletableJob = Job() - private var serverJob: Job by atomic(Job()) + // See KT-67440 + @Volatile private var serverJob: Job = Job() init { serverJob = initServerJob() diff --git a/ktor-utils/posix/src/io/ktor/util/network/NetworkAddressNative.kt b/ktor-utils/posix/src/io/ktor/util/network/NetworkAddressNative.kt index 94cc80f6ed1..9632ae030d6 100644 --- a/ktor-utils/posix/src/io/ktor/util/network/NetworkAddressNative.kt +++ b/ktor-utils/posix/src/io/ktor/util/network/NetworkAddressNative.kt @@ -20,8 +20,10 @@ public actual abstract class NetworkAddress constructor( public val port: Int, explicitAddress: Any? = null ) { + private val _explicitAddress: AtomicRef = atomic(explicitAddress) + @InternalAPI - public var explicitAddress: AtomicRef = atomic(explicitAddress) + public var explicitAddress: Any? by _explicitAddress /** * Resolve current socket address.