From 6931c55b69c14085d01c4d11b440818b0b7613dc Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Thu, 26 Jan 2023 17:19:01 +0200 Subject: [PATCH] Add optins required in Kotlin 1.9 --- .../jmh/kotlin/benchmarks/scheduler/ForkJoinBenchmark.kt | 9 +++++++-- .../benchmarks/scheduler/StatefulAwaitsBenchmark.kt | 1 + .../kotlin/benchmarks/tailcall/SimpleChannelBenchmark.kt | 1 + docs/topics/shared-mutable-state-and-concurrency.md | 3 ++- kotlinx-coroutines-core/common/src/EventLoop.common.kt | 1 + kotlinx-coroutines-core/jdk8/src/future/Future.kt | 2 +- kotlinx-coroutines-core/jvm/src/CoroutineContext.kt | 1 + kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt | 2 ++ .../jvm/src/debug/internal/DebugProbesImpl.kt | 1 + .../jvm/src/internal/ResizableAtomicArray.kt | 1 + .../jvm/src/scheduling/CoroutineScheduler.kt | 4 +++- .../jvm/test/CancellableContinuationJvmTest.kt | 2 ++ .../jvm/test/ConcurrentTestUtilities.kt | 1 + kotlinx-coroutines-core/jvm/test/JobDisposeStressTest.kt | 6 +++++- .../jvm/test/JobHandlersUpgradeStressTest.kt | 4 +++- kotlinx-coroutines-core/jvm/test/VirtualTimeSource.kt | 8 ++++++-- .../ChannelUndeliveredElementSelectOldStressTest.kt | 1 + .../test/channels/ChannelUndeliveredElementStressTest.kt | 1 + .../jvm/test/flow/CallbackFlowTest.kt | 1 + .../jvm/test/guide/example-sync-02.kt | 3 ++- .../jvm/test/scheduling/WorkQueueStressTest.kt | 1 + reactive/kotlinx-coroutines-reactive/src/Publish.kt | 1 + reactive/kotlinx-coroutines-reactive/src/ReactiveFlow.kt | 1 + reactive/kotlinx-coroutines-reactor/src/Mono.kt | 1 + .../src/AndroidExceptionPreHandler.kt | 1 + ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt | 2 ++ 26 files changed, 50 insertions(+), 10 deletions(-) diff --git a/benchmarks/src/jmh/kotlin/benchmarks/scheduler/ForkJoinBenchmark.kt b/benchmarks/src/jmh/kotlin/benchmarks/scheduler/ForkJoinBenchmark.kt index 20bdfa349a..e3b5aa9328 100644 --- a/benchmarks/src/jmh/kotlin/benchmarks/scheduler/ForkJoinBenchmark.kt +++ b/benchmarks/src/jmh/kotlin/benchmarks/scheduler/ForkJoinBenchmark.kt @@ -110,8 +110,13 @@ open class ForkJoinBenchmark : ParametrizedDispatcherBase() { } } - class RecursiveAction(val coefficients: LongArray, val start: Int, val end: Int, @Volatile var result: Double = 0.0, - parent: RecursiveAction? = null) : CountedCompleter(parent) { + class RecursiveAction( + val coefficients: LongArray, + val start: Int, + val end: Int, + @Volatile @OptIn(ExperimentalStdlibApi::class) var result: Double = 0.0, + parent: RecursiveAction? = null + ) : CountedCompleter(parent) { private var first: ForkJoinTask? = null private var second: ForkJoinTask? = null diff --git a/benchmarks/src/jmh/kotlin/benchmarks/scheduler/StatefulAwaitsBenchmark.kt b/benchmarks/src/jmh/kotlin/benchmarks/scheduler/StatefulAwaitsBenchmark.kt index c5b34eda90..4f5e6cbb7d 100644 --- a/benchmarks/src/jmh/kotlin/benchmarks/scheduler/StatefulAwaitsBenchmark.kt +++ b/benchmarks/src/jmh/kotlin/benchmarks/scheduler/StatefulAwaitsBenchmark.kt @@ -57,6 +57,7 @@ open class StatefulAsyncBenchmark : ParametrizedDispatcherBase() { override var dispatcher: String = "fjp" @Volatile + @OptIn(ExperimentalStdlibApi::class) private var state: Array? = null @Setup diff --git a/benchmarks/src/jmh/kotlin/benchmarks/tailcall/SimpleChannelBenchmark.kt b/benchmarks/src/jmh/kotlin/benchmarks/tailcall/SimpleChannelBenchmark.kt index 9654b6dabe..b70566a8b1 100644 --- a/benchmarks/src/jmh/kotlin/benchmarks/tailcall/SimpleChannelBenchmark.kt +++ b/benchmarks/src/jmh/kotlin/benchmarks/tailcall/SimpleChannelBenchmark.kt @@ -19,6 +19,7 @@ open class SimpleChannelBenchmark { private val iterations = 10_000 @Volatile + @OptIn(ExperimentalStdlibApi::class) private var sink: Int = 0 @Benchmark diff --git a/docs/topics/shared-mutable-state-and-concurrency.md b/docs/topics/shared-mutable-state-and-concurrency.md index 8e491b3d64..33fabcc1ad 100644 --- a/docs/topics/shared-mutable-state-and-concurrency.md +++ b/docs/topics/shared-mutable-state-and-concurrency.md @@ -106,7 +106,8 @@ suspend fun massiveRun(action: suspend () -> Unit) { } //sampleStart -@Volatile // in Kotlin `volatile` is an annotation +@OptIn(ExperimentalStdlibApi::class) +@Volatile // in Kotlin `volatile` is an annotation var counter = 0 fun main() = runBlocking { diff --git a/kotlinx-coroutines-core/common/src/EventLoop.common.kt b/kotlinx-coroutines-core/common/src/EventLoop.common.kt index 8d9eed21bc..b098ccd167 100644 --- a/kotlinx-coroutines-core/common/src/EventLoop.common.kt +++ b/kotlinx-coroutines-core/common/src/EventLoop.common.kt @@ -412,6 +412,7 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay { @JvmField var nanoTime: Long ) : Runnable, Comparable, DisposableHandle, ThreadSafeHeapNode, SynchronizedObject() { @Volatile + @OptIn(ExperimentalStdlibApi::class) private var _heap: Any? = null // null | ThreadSafeHeap | DISPOSED_TASK override var heap: ThreadSafeHeap<*>? diff --git a/kotlinx-coroutines-core/jdk8/src/future/Future.kt b/kotlinx-coroutines-core/jdk8/src/future/Future.kt index f7b4fdca0d..4882147166 100644 --- a/kotlinx-coroutines-core/jdk8/src/future/Future.kt +++ b/kotlinx-coroutines-core/jdk8/src/future/Future.kt @@ -180,7 +180,7 @@ public suspend fun CompletionStage.await(): T { } private class ContinuationHandler( - @Volatile @JvmField var cont: Continuation? + @Volatile @OptIn(ExperimentalStdlibApi::class) @JvmField var cont: Continuation? ) : BiFunction { @Suppress("UNCHECKED_CAST") override fun apply(result: T?, exception: Throwable?) { diff --git a/kotlinx-coroutines-core/jvm/src/CoroutineContext.kt b/kotlinx-coroutines-core/jvm/src/CoroutineContext.kt index 59695a055a..473290a0b8 100644 --- a/kotlinx-coroutines-core/jvm/src/CoroutineContext.kt +++ b/kotlinx-coroutines-core/jvm/src/CoroutineContext.kt @@ -213,6 +213,7 @@ internal actual class UndispatchedCoroutineactual constructor ( * in another. */ @Volatile + @OptIn(ExperimentalStdlibApi::class) private var threadLocalIsSet = false init { diff --git a/kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt b/kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt index c993bc2324..6a08b61084 100644 --- a/kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt +++ b/kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt @@ -43,6 +43,7 @@ internal actual object DefaultExecutor : EventLoopImplBase(), Runnable { @Suppress("ObjectPropertyName") @Volatile + @OptIn(ExperimentalStdlibApi::class) private var _thread: Thread? = null override val thread: Thread @@ -55,6 +56,7 @@ internal actual object DefaultExecutor : EventLoopImplBase(), Runnable { private const val SHUTDOWN = 4 @Volatile + @OptIn(ExperimentalStdlibApi::class) private var debugStatus: Int = FRESH private val isShutDown: Boolean get() = debugStatus == SHUTDOWN diff --git a/kotlinx-coroutines-core/jvm/src/debug/internal/DebugProbesImpl.kt b/kotlinx-coroutines-core/jvm/src/debug/internal/DebugProbesImpl.kt index 7080f612b7..4c9f630541 100644 --- a/kotlinx-coroutines-core/jvm/src/debug/internal/DebugProbesImpl.kt +++ b/kotlinx-coroutines-core/jvm/src/debug/internal/DebugProbesImpl.kt @@ -29,6 +29,7 @@ internal object DebugProbesImpl { private val capturedCoroutines: Set> get() = capturedCoroutinesMap.keys @Volatile + @OptIn(ExperimentalStdlibApi::class) private var installations = 0 /** diff --git a/kotlinx-coroutines-core/jvm/src/internal/ResizableAtomicArray.kt b/kotlinx-coroutines-core/jvm/src/internal/ResizableAtomicArray.kt index f949d9f5ea..cb544fcc6b 100644 --- a/kotlinx-coroutines-core/jvm/src/internal/ResizableAtomicArray.kt +++ b/kotlinx-coroutines-core/jvm/src/internal/ResizableAtomicArray.kt @@ -12,6 +12,7 @@ import java.util.concurrent.atomic.* */ internal class ResizableAtomicArray(initialLength: Int) { @Volatile + @OptIn(ExperimentalStdlibApi::class) private var array = AtomicReferenceArray(initialLength) // for debug output diff --git a/kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt b/kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt index 2bad139fe6..2ca66d4da5 100644 --- a/kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt +++ b/kotlinx-coroutines-core/jvm/src/scheduling/CoroutineScheduler.kt @@ -583,7 +583,8 @@ internal class CoroutineScheduler( } // guarded by scheduler lock, index in workers array, 0 when not in array (terminated) - @Volatile // volatile for push/pop operation into parkedWorkersStack + @Volatile + @OptIn(ExperimentalStdlibApi::class) // volatile for push/pop operation into parkedWorkersStack var indexInArray = 0 set(index) { name = "$schedulerName-worker-${if (index == 0) "TERMINATED" else index.toString()}" @@ -634,6 +635,7 @@ internal class CoroutineScheduler( * This reference is set to [NOT_IN_STACK] when worker is physically not in stack. */ @Volatile + @OptIn(ExperimentalStdlibApi::class) var nextParkedWorker: Any? = NOT_IN_STACK /* diff --git a/kotlinx-coroutines-core/jvm/test/CancellableContinuationJvmTest.kt b/kotlinx-coroutines-core/jvm/test/CancellableContinuationJvmTest.kt index f1a957adca..a9c1d94066 100644 --- a/kotlinx-coroutines-core/jvm/test/CancellableContinuationJvmTest.kt +++ b/kotlinx-coroutines-core/jvm/test/CancellableContinuationJvmTest.kt @@ -63,9 +63,11 @@ class CancellableContinuationJvmTest : TestBase() { private class BlockingSource { @Volatile + @OptIn(ExperimentalStdlibApi::class) private var isCancelled = false @Volatile + @OptIn(ExperimentalStdlibApi::class) public var hasSubscriber = false public fun subscribe() { diff --git a/kotlinx-coroutines-core/jvm/test/ConcurrentTestUtilities.kt b/kotlinx-coroutines-core/jvm/test/ConcurrentTestUtilities.kt index 4ccb74b427..1ca0c0142c 100644 --- a/kotlinx-coroutines-core/jvm/test/ConcurrentTestUtilities.kt +++ b/kotlinx-coroutines-core/jvm/test/ConcurrentTestUtilities.kt @@ -17,6 +17,7 @@ actual fun randomWait() { private object BlackHole { @Volatile + @OptIn(ExperimentalStdlibApi::class) var sink = 1 } diff --git a/kotlinx-coroutines-core/jvm/test/JobDisposeStressTest.kt b/kotlinx-coroutines-core/jvm/test/JobDisposeStressTest.kt index 3a074f1568..cd04a1ad20 100644 --- a/kotlinx-coroutines-core/jvm/test/JobDisposeStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/JobDisposeStressTest.kt @@ -14,13 +14,17 @@ class JobDisposeStressTest: TestBase() { private val TEST_DURATION = 3 * stressTestMultiplier // seconds @Volatile + @OptIn(ExperimentalStdlibApi::class) private var done = false @Volatile + @OptIn(ExperimentalStdlibApi::class) private var job: TestJob? = null @Volatile + @OptIn(ExperimentalStdlibApi::class) private var handle: DisposableHandle? = null @Volatile + @OptIn(ExperimentalStdlibApi::class) private var exception: Throwable? = null private fun testThread(name: String, block: () -> Unit): Thread = @@ -77,4 +81,4 @@ class JobDisposeStressTest: TestBase() { @Suppress("DEPRECATION_ERROR") private class TestJob : JobSupport(active = true) -} \ No newline at end of file +} diff --git a/kotlinx-coroutines-core/jvm/test/JobHandlersUpgradeStressTest.kt b/kotlinx-coroutines-core/jvm/test/JobHandlersUpgradeStressTest.kt index 852aa2a8a0..73f4272a06 100644 --- a/kotlinx-coroutines-core/jvm/test/JobHandlersUpgradeStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/JobHandlersUpgradeStressTest.kt @@ -24,9 +24,11 @@ class JobHandlersUpgradeStressTest : TestBase() { private val sink = atomic(0) @Volatile + @OptIn(ExperimentalStdlibApi::class) private var done = false @Volatile + @OptIn(ExperimentalStdlibApi::class) private var job: Job? = null class State { @@ -94,4 +96,4 @@ class JobHandlersUpgradeStressTest : TestBase() { println(" Fired handler ${fired.value} times") } -} \ No newline at end of file +} diff --git a/kotlinx-coroutines-core/jvm/test/VirtualTimeSource.kt b/kotlinx-coroutines-core/jvm/test/VirtualTimeSource.kt index b4bc96ebdd..f356d0b1b2 100644 --- a/kotlinx-coroutines-core/jvm/test/VirtualTimeSource.kt +++ b/kotlinx-coroutines-core/jvm/test/VirtualTimeSource.kt @@ -27,9 +27,11 @@ internal inline fun withVirtualTimeSource(log: PrintStream? = null, block: () -> private const val NOT_PARKED = -1L private class ThreadStatus { - @Volatile @JvmField + @Volatile + @OptIn(ExperimentalStdlibApi::class) @JvmField var parkedTill = NOT_PARKED - @Volatile @JvmField + @Volatile + @OptIn(ExperimentalStdlibApi::class) @JvmField var permit = false var registered = 0 override fun toString(): String = "parkedTill = ${TimeUnit.NANOSECONDS.toMillis(parkedTill)} ms, permit = $permit" @@ -47,9 +49,11 @@ internal class VirtualTimeSource( private var checkpointNanos: Long = System.nanoTime() @Volatile + @OptIn(ExperimentalStdlibApi::class) private var isShutdown = false @Volatile + @OptIn(ExperimentalStdlibApi::class) private var time: Long = 0 private var trackedTasks = 0 diff --git a/kotlinx-coroutines-core/jvm/test/channels/ChannelUndeliveredElementSelectOldStressTest.kt b/kotlinx-coroutines-core/jvm/test/channels/ChannelUndeliveredElementSelectOldStressTest.kt index 1b4b83eb53..abf91a8184 100644 --- a/kotlinx-coroutines-core/jvm/test/channels/ChannelUndeliveredElementSelectOldStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/channels/ChannelUndeliveredElementSelectOldStressTest.kt @@ -40,6 +40,7 @@ class ChannelUndeliveredElementSelectOldStressTest(private val kind: TestChannel private val receiverDone = Channel(1) @Volatile + @OptIn(ExperimentalStdlibApi::class) private var lastReceived = -1L private var stoppedSender = 0L diff --git a/kotlinx-coroutines-core/jvm/test/channels/ChannelUndeliveredElementStressTest.kt b/kotlinx-coroutines-core/jvm/test/channels/ChannelUndeliveredElementStressTest.kt index 335980daaf..37c3d4b39b 100644 --- a/kotlinx-coroutines-core/jvm/test/channels/ChannelUndeliveredElementStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/channels/ChannelUndeliveredElementStressTest.kt @@ -40,6 +40,7 @@ class ChannelUndeliveredElementStressTest(private val kind: TestChannelKind) : T private val receiverDone = Channel(1) @Volatile + @OptIn(ExperimentalStdlibApi::class) private var lastReceived = -1L private var stoppedSender = 0L diff --git a/kotlinx-coroutines-core/jvm/test/flow/CallbackFlowTest.kt b/kotlinx-coroutines-core/jvm/test/flow/CallbackFlowTest.kt index f1be284cae..be32d2d7f0 100644 --- a/kotlinx-coroutines-core/jvm/test/flow/CallbackFlowTest.kt +++ b/kotlinx-coroutines-core/jvm/test/flow/CallbackFlowTest.kt @@ -15,6 +15,7 @@ class CallbackFlowTest : TestBase() { private class CallbackApi(val block: (SendChannel) -> Unit) { var started = false @Volatile + @OptIn(ExperimentalStdlibApi::class) var stopped = false lateinit var thread: Thread diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-sync-02.kt b/kotlinx-coroutines-core/jvm/test/guide/example-sync-02.kt index fb551f8953..b17ca91252 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-sync-02.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-sync-02.kt @@ -23,7 +23,8 @@ suspend fun massiveRun(action: suspend () -> Unit) { println("Completed ${n * k} actions in $time ms") } -@Volatile // in Kotlin `volatile` is an annotation +@OptIn(ExperimentalStdlibApi::class) +@Volatile // in Kotlin `volatile` is an annotation var counter = 0 fun main() = runBlocking { diff --git a/kotlinx-coroutines-core/jvm/test/scheduling/WorkQueueStressTest.kt b/kotlinx-coroutines-core/jvm/test/scheduling/WorkQueueStressTest.kt index e2562b57ba..0004aa1788 100644 --- a/kotlinx-coroutines-core/jvm/test/scheduling/WorkQueueStressTest.kt +++ b/kotlinx-coroutines-core/jvm/test/scheduling/WorkQueueStressTest.kt @@ -22,6 +22,7 @@ class WorkQueueStressTest : TestBase() { private val producerQueue = WorkQueue() @Volatile + @OptIn(ExperimentalStdlibApi::class) private var producerFinished = false @Before diff --git a/reactive/kotlinx-coroutines-reactive/src/Publish.kt b/reactive/kotlinx-coroutines-reactive/src/Publish.kt index ae85e4186a..1770eb6be9 100644 --- a/reactive/kotlinx-coroutines-reactive/src/Publish.kt +++ b/reactive/kotlinx-coroutines-reactive/src/Publish.kt @@ -74,6 +74,7 @@ public class PublisherCoroutine( private val _nRequested = atomic(0L) // < 0 when closed (CLOSED or SIGNALLED) @Volatile + @OptIn(ExperimentalStdlibApi::class) private var cancelled = false // true after Subscription.cancel() is invoked override val isClosedForSend: Boolean get() = !isActive diff --git a/reactive/kotlinx-coroutines-reactive/src/ReactiveFlow.kt b/reactive/kotlinx-coroutines-reactive/src/ReactiveFlow.kt index 1a527a3c2b..2c328e60f5 100644 --- a/reactive/kotlinx-coroutines-reactive/src/ReactiveFlow.kt +++ b/reactive/kotlinx-coroutines-reactive/src/ReactiveFlow.kt @@ -197,6 +197,7 @@ public class FlowSubscription( private val requested = atomic(0L) private val producer = atomic?>(createInitialContinuation()) @Volatile + @OptIn(ExperimentalStdlibApi::class) private var cancellationRequested = false // This code wraps startCoroutineCancellable into continuation diff --git a/reactive/kotlinx-coroutines-reactor/src/Mono.kt b/reactive/kotlinx-coroutines-reactor/src/Mono.kt index 45f3847364..a8cbdbed52 100644 --- a/reactive/kotlinx-coroutines-reactor/src/Mono.kt +++ b/reactive/kotlinx-coroutines-reactor/src/Mono.kt @@ -97,6 +97,7 @@ private class MonoCoroutine( private val sink: MonoSink ) : AbstractCoroutine(parentContext, false, true), Disposable { @Volatile + @OptIn(ExperimentalStdlibApi::class) private var disposed = false override fun onCompleted(value: T) { diff --git a/ui/kotlinx-coroutines-android/src/AndroidExceptionPreHandler.kt b/ui/kotlinx-coroutines-android/src/AndroidExceptionPreHandler.kt index 0bc603ea1e..219f164ae7 100644 --- a/ui/kotlinx-coroutines-android/src/AndroidExceptionPreHandler.kt +++ b/ui/kotlinx-coroutines-android/src/AndroidExceptionPreHandler.kt @@ -13,6 +13,7 @@ internal class AndroidExceptionPreHandler : AbstractCoroutineContextElement(CoroutineExceptionHandler), CoroutineExceptionHandler { @Volatile + @OptIn(ExperimentalStdlibApi::class) private var _preHandler: Any? = this // uninitialized marker // Reflectively lookup pre-handler. diff --git a/ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt b/ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt index 7012c23ecd..3bf0e83134 100644 --- a/ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt +++ b/ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt @@ -128,6 +128,7 @@ internal class HandlerContext private constructor( ) : this(handler, name, false) @Volatile + @OptIn(ExperimentalStdlibApi::class) private var _immediate: HandlerContext? = if (invokeImmediately) this else null override val immediate: HandlerContext = _immediate ?: @@ -176,6 +177,7 @@ internal class HandlerContext private constructor( override fun hashCode(): Int = System.identityHashCode(handler) } +@OptIn(ExperimentalStdlibApi::class) @Volatile private var choreographer: Choreographer? = null