Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Double>(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<Double>(parent) {

private var first: ForkJoinTask<Double>? = null
private var second: ForkJoinTask<Double>? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ open class StatefulAsyncBenchmark : ParametrizedDispatcherBase() {
override var dispatcher: String = "fjp"

@Volatile
@OptIn(ExperimentalStdlibApi::class)
private var state: Array<LongArray>? = null

@Setup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ open class SimpleChannelBenchmark {
private val iterations = 10_000

@Volatile
@OptIn(ExperimentalStdlibApi::class)
private var sink: Int = 0

@Benchmark
Expand Down
3 changes: 2 additions & 1 deletion docs/topics/shared-mutable-state-and-concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions kotlinx-coroutines-core/common/src/EventLoop.common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay {
@JvmField var nanoTime: Long
) : Runnable, Comparable<DelayedTask>, DisposableHandle, ThreadSafeHeapNode, SynchronizedObject() {
@Volatile
@OptIn(ExperimentalStdlibApi::class)
private var _heap: Any? = null // null | ThreadSafeHeap | DISPOSED_TASK

override var heap: ThreadSafeHeap<*>?
Expand Down
2 changes: 1 addition & 1 deletion kotlinx-coroutines-core/jdk8/src/future/Future.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public suspend fun <T> CompletionStage<T>.await(): T {
}

private class ContinuationHandler<T>(
@Volatile @JvmField var cont: Continuation<T>?
@Volatile @OptIn(ExperimentalStdlibApi::class) @JvmField var cont: Continuation<T>?
) : BiFunction<T?, Throwable?, Unit> {
@Suppress("UNCHECKED_CAST")
override fun apply(result: T?, exception: Throwable?) {
Expand Down
1 change: 1 addition & 0 deletions kotlinx-coroutines-core/jvm/src/CoroutineContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ internal actual class UndispatchedCoroutine<in T>actual constructor (
* in another.
*/
@Volatile
@OptIn(ExperimentalStdlibApi::class)
private var threadLocalIsSet = false

init {
Expand Down
2 changes: 2 additions & 0 deletions kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ internal actual object DefaultExecutor : EventLoopImplBase(), Runnable {

@Suppress("ObjectPropertyName")
@Volatile
@OptIn(ExperimentalStdlibApi::class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is clearly a breaking change in the language, something suspicious is here and we cannot accept it as is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reported it to kotlin-design. Fix of this issue is definitely not the part of migration master of kotlin to 1.9

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it means that the master is broken right now, and that it is definitely the part of preparing master to 1.9

private var _thread: Thread? = null

override val thread: Thread
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal object DebugProbesImpl {
private val capturedCoroutines: Set<CoroutineOwner<*>> get() = capturedCoroutinesMap.keys

@Volatile
@OptIn(ExperimentalStdlibApi::class)
private var installations = 0

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import java.util.concurrent.atomic.*
*/
internal class ResizableAtomicArray<T>(initialLength: Int) {
@Volatile
@OptIn(ExperimentalStdlibApi::class)
private var array = AtomicReferenceArray<T>(initialLength)

// for debug output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()}"
Expand Down Expand Up @@ -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

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ actual fun randomWait() {

private object BlackHole {
@Volatile
@OptIn(ExperimentalStdlibApi::class)
var sink = 1
}

Expand Down
6 changes: 5 additions & 1 deletion kotlinx-coroutines-core/jvm/test/JobDisposeStressTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -77,4 +81,4 @@ class JobDisposeStressTest: TestBase() {

@Suppress("DEPRECATION_ERROR")
private class TestJob : JobSupport(active = true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -94,4 +96,4 @@ class JobHandlersUpgradeStressTest : TestBase() {
println(" Fired handler ${fired.value} times")

}
}
}
8 changes: 6 additions & 2 deletions kotlinx-coroutines-core/jvm/test/VirtualTimeSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ChannelUndeliveredElementSelectOldStressTest(private val kind: TestChannel
private val receiverDone = Channel<Boolean>(1)

@Volatile
@OptIn(ExperimentalStdlibApi::class)
private var lastReceived = -1L

private var stoppedSender = 0L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ChannelUndeliveredElementStressTest(private val kind: TestChannelKind) : T
private val receiverDone = Channel<Boolean>(1)

@Volatile
@OptIn(ExperimentalStdlibApi::class)
private var lastReceived = -1L

private var stoppedSender = 0L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CallbackFlowTest : TestBase() {
private class CallbackApi(val block: (SendChannel<Int>) -> Unit) {
var started = false
@Volatile
@OptIn(ExperimentalStdlibApi::class)
var stopped = false
lateinit var thread: Thread

Expand Down
3 changes: 2 additions & 1 deletion kotlinx-coroutines-core/jvm/test/guide/example-sync-02.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class WorkQueueStressTest : TestBase() {
private val producerQueue = WorkQueue()

@Volatile
@OptIn(ExperimentalStdlibApi::class)
private var producerFinished = false

@Before
Expand Down
1 change: 1 addition & 0 deletions reactive/kotlinx-coroutines-reactive/src/Publish.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class PublisherCoroutine<in T>(
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
Expand Down
1 change: 1 addition & 0 deletions reactive/kotlinx-coroutines-reactive/src/ReactiveFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public class FlowSubscription<T>(
private val requested = atomic(0L)
private val producer = atomic<Continuation<Unit>?>(createInitialContinuation())
@Volatile
@OptIn(ExperimentalStdlibApi::class)
private var cancellationRequested = false

// This code wraps startCoroutineCancellable into continuation
Expand Down
1 change: 1 addition & 0 deletions reactive/kotlinx-coroutines-reactor/src/Mono.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ private class MonoCoroutine<in T>(
private val sink: MonoSink<T>
) : AbstractCoroutine<T>(parentContext, false, true), Disposable {
@Volatile
@OptIn(ExperimentalStdlibApi::class)
private var disposed = false

override fun onCompleted(value: T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?:
Expand Down Expand Up @@ -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

Expand Down