Skip to content

Commit d8cdc9f

Browse files
committed
Introduce @SharedImmutable and mark all global constants with SharedImmutable/ThreadLocal to allow using coroutines in K/N workers
1 parent 69a2d92 commit d8cdc9f

File tree

13 files changed

+62
-21
lines changed

13 files changed

+62
-21
lines changed

common/kotlinx-coroutines-core-common/src/AbstractContinuation.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package kotlinx.coroutines
66

77
import kotlinx.atomicfu.*
8+
import kotlinx.coroutines.internal.*
89
import kotlin.coroutines.*
910
import kotlin.coroutines.intrinsics.*
1011
import kotlin.jvm.*
@@ -276,6 +277,7 @@ internal abstract class AbstractContinuation<in T>(
276277
internal interface NotCompleted
277278

278279
private class Active : NotCompleted
280+
@SharedImmutable
279281
private val ACTIVE: Active = Active()
280282

281283
internal abstract class CancelHandler : CancelHandlerBase(), NotCompleted

common/kotlinx-coroutines-core-common/src/Dispatched.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import kotlin.coroutines.*
99
import kotlin.jvm.*
1010

1111
@Suppress("PrivatePropertyName")
12+
@SharedImmutable
1213
private val UNDEFINED = Symbol("UNDEFINED")
1314

1415
@NativeThreadLocal

common/kotlinx-coroutines-core-common/src/JobSupport.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,9 +1151,11 @@ private const val RETRY = -1
11511151
private const val FALSE = 0
11521152
private const val TRUE = 1
11531153

1154+
@SharedImmutable
11541155
private val SEALED = Symbol("SEALED")
1155-
1156+
@SharedImmutable
11561157
private val EMPTY_NEW = Empty(false)
1158+
@SharedImmutable
11571159
private val EMPTY_ACTIVE = Empty(true)
11581160

11591161
private class Empty(override val isActive: Boolean) : Incomplete {

common/kotlinx-coroutines-core-common/src/channels/AbstractChannel.kt

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,32 +1001,43 @@ internal abstract class AbstractChannel<E> : AbstractSendChannel<E>(), Channel<E
10011001
)
10021002
}
10031003

1004-
/** @suppress **This is unstable API and it is subject to change.** */
1005-
@JvmField internal val OFFER_SUCCESS: Any = Symbol("OFFER_SUCCESS")
1004+
@JvmField
1005+
@SharedImmutable
1006+
internal val OFFER_SUCCESS: Any = Symbol("OFFER_SUCCESS")
10061007

1007-
/** @suppress **This is unstable API and it is subject to change.** */
1008-
@JvmField internal val OFFER_FAILED: Any = Symbol("OFFER_FAILED")
1008+
@JvmField
1009+
@SharedImmutable
1010+
internal val OFFER_FAILED: Any = Symbol("OFFER_FAILED")
10091011

1010-
/** @suppress **This is unstable API and it is subject to change.** */
1011-
@JvmField internal val POLL_FAILED: Any = Symbol("POLL_FAILED")
1012+
@JvmField
1013+
@SharedImmutable
1014+
internal val POLL_FAILED: Any = Symbol("POLL_FAILED")
10121015

1013-
/** @suppress **This is unstable API and it is subject to change.** */
1014-
@JvmField internal val ENQUEUE_FAILED: Any = Symbol("ENQUEUE_FAILED")
1016+
@JvmField
1017+
@SharedImmutable
1018+
internal val ENQUEUE_FAILED: Any = Symbol("ENQUEUE_FAILED")
10151019

1016-
/** @suppress **This is unstable API and it is subject to change.** */
1017-
@JvmField internal val SELECT_STARTED: Any = Symbol("SELECT_STARTED")
1020+
@JvmField
1021+
@SharedImmutable
1022+
internal val SELECT_STARTED: Any = Symbol("SELECT_STARTED")
10181023

1019-
/** @suppress **This is unstable API and it is subject to change.** */
1020-
@JvmField internal val NULL_VALUE: Any = Symbol("NULL_VALUE")
1024+
@JvmField
1025+
@SharedImmutable
1026+
internal val NULL_VALUE: Any = Symbol("NULL_VALUE")
10211027

1022-
/** @suppress **This is unstable API and it is subject to change.** */
1023-
@JvmField internal val CLOSE_RESUMED: Any = Symbol("CLOSE_RESUMED")
1028+
@JvmField
1029+
@SharedImmutable
1030+
internal val CLOSE_RESUMED: Any = Symbol("CLOSE_RESUMED")
10241031

1025-
/** @suppress **This is unstable API and it is subject to change.** */
1026-
@JvmField internal val SEND_RESUMED = Symbol("SEND_RESUMED")
1032+
@JvmField
1033+
@SharedImmutable
1034+
internal val SEND_RESUMED = Symbol("SEND_RESUMED")
1035+
1036+
@JvmField
1037+
@SharedImmutable
1038+
internal val HANDLER_INVOKED = Symbol("ON_CLOSE_HANDLER_INVOKED")
10271039

10281040
internal typealias Handler = (Throwable?) -> Unit
1029-
@JvmField internal val HANDLER_INVOKED = Any()
10301041

10311042
/**
10321043
* Represents sending waiter in the queue.

common/kotlinx-coroutines-core-common/src/channels/ConflatedBroadcastChannel.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public class ConflatedBroadcastChannel<E>() : BroadcastChannel<E> {
4646
private val onCloseHandler = atomic<Any?>(null)
4747

4848
private companion object {
49+
@SharedImmutable
4950
private val CLOSED = Closed(null)
51+
@SharedImmutable
5052
private val UNDEFINED = Symbol("UNDEFINED")
5153
private val INITIAL_STATE = State<Any?>(UNDEFINED, null)
5254
}

common/kotlinx-coroutines-core-common/src/internal/Atomic.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public abstract class OpDescriptor {
2020
abstract fun perform(affected: Any?): Any?
2121
}
2222

23+
@SharedImmutable
2324
private val NO_DECISION: Any = Symbol("NO_DECISION")
2425

2526
/**

common/kotlinx-coroutines-core-common/src/internal/Concurrent.common.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ internal expect class ReentrantLock() {
2121

2222
internal expect inline fun <T> ReentrantLock.withLock(action: () -> T): T
2323

24-
internal expect fun <E> identitySet(expectedSize: Int): MutableSet<E>
24+
internal expect fun <E> identitySet(expectedSize: Int): MutableSet<E>
25+
26+
@ExperimentalMultiplatform
27+
@OptionalExpectation
28+
internal expect annotation class SharedImmutable()

common/kotlinx-coroutines-core-common/src/selects/Select.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,11 @@ public suspend inline fun <R> select(crossinline builder: SelectBuilder<R>.() ->
186186
}
187187

188188

189+
@SharedImmutable
189190
internal val ALREADY_SELECTED: Any = Symbol("ALREADY_SELECTED")
191+
@SharedImmutable
190192
private val UNDECIDED: Any = Symbol("UNDECIDED")
193+
@SharedImmutable
191194
private val RESUMED: Any = Symbol("RESUMED")
192195

193196
@PublishedApi

common/kotlinx-coroutines-core-common/src/sync/Mutex.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,22 @@ public suspend inline fun <T> Mutex.withLock(owner: Any? = null, action: () -> T
113113
}
114114
}
115115

116+
@SharedImmutable
116117
private val LOCK_FAIL = Symbol("LOCK_FAIL")
118+
@SharedImmutable
117119
private val ENQUEUE_FAIL = Symbol("ENQUEUE_FAIL")
120+
@SharedImmutable
118121
private val UNLOCK_FAIL = Symbol("UNLOCK_FAIL")
122+
@SharedImmutable
119123
private val SELECT_SUCCESS = Symbol("SELECT_SUCCESS")
124+
@SharedImmutable
120125
private val LOCKED = Symbol("LOCKED")
126+
@SharedImmutable
121127
private val UNLOCKED = Symbol("UNLOCKED")
122128

129+
@SharedImmutable
123130
private val EmptyLocked = Empty(LOCKED)
131+
@SharedImmutable
124132
private val EmptyUnlocked = Empty(UNLOCKED)
125133

126134
private class Empty(

native/kotlinx-coroutines-core-native/src/CoroutineContext.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
package kotlinx.coroutines
66

77
import kotlin.coroutines.*
8+
import kotlinx.coroutines.internal.*
89

10+
@ThreadLocal
911
internal val currentEventLoop = ArrayList<BlockingEventLoop>()
1012

1113
private fun takeEventLoop(): BlockingEventLoop =

0 commit comments

Comments
 (0)