Skip to content

Commit 73e843b

Browse files
committed
Do not try to start sharing strategy in an undispatched manner in native-mt mode as it is not supported
Fixes #3136
1 parent 8dfe2e3 commit 73e843b

File tree

6 files changed

+15
-1
lines changed

6 files changed

+15
-1
lines changed

kotlinx-coroutines-core/common/src/flow/operators/Share.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package kotlinx.coroutines.flow
1010
import kotlinx.coroutines.*
1111
import kotlinx.coroutines.channels.*
1212
import kotlinx.coroutines.flow.internal.*
13+
import kotlinx.coroutines.internal.*
1314
import kotlin.coroutines.*
1415
import kotlin.jvm.*
1516

@@ -204,8 +205,9 @@ private fun <T> CoroutineScope.launchSharing(
204205
* * Delayed sharing strategies have a chance to immediately observe consecutive subscriptions.
205206
* E.g. in the cases like `flow.shareIn(...); flow.take(1)` we want sharing strategy to see the initial subscription
206207
* * Eager sharing does not start immediately, so the subscribers have actual chance to subscribe _prior_ to sharing.
208+
* // TODO: kludge: native-mt doesn't support undispatched
207209
*/
208-
val start = if (started == SharingStarted.Eagerly) CoroutineStart.DEFAULT else CoroutineStart.UNDISPATCHED
210+
val start = if (started == SharingStarted.Eagerly || isNativeMt) CoroutineStart.DEFAULT else CoroutineStart.UNDISPATCHED
209211
return launch(context, start = start) { // the single coroutine to rule the sharing
210212
// Optimize common built-in started strategies
211213
when {

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

+2
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ public expect open class SynchronizedObject() // marker abstract class
1717
*/
1818
@InternalCoroutinesApi
1919
public expect inline fun <T> synchronized(lock: SynchronizedObject, block: () -> T): T
20+
21+
internal expect val isNativeMt: Boolean

kotlinx-coroutines-core/common/test/flow/sharing/ShareInTest.kt

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package kotlinx.coroutines.flow
66

77
import kotlinx.coroutines.*
88
import kotlinx.coroutines.channels.*
9+
import kotlinx.coroutines.internal.*
910
import kotlin.test.*
1011

1112
class ShareInTest : TestBase() {
@@ -213,6 +214,7 @@ class ShareInTest : TestBase() {
213214

214215
@Test
215216
fun testShouldStart() = runTest {
217+
if (isNativeMt) return@runTest
216218
val flow = flow {
217219
expect(2)
218220
emit(1)
@@ -229,6 +231,7 @@ class ShareInTest : TestBase() {
229231

230232
@Test
231233
fun testShouldStartScalar() = runTest {
234+
if (isNativeMt) return@runTest
232235
val j = Job()
233236
val shared = flowOf(239).stateIn(this + j, SharingStarted.Lazily, 42)
234237
assertEquals(42, shared.first())

kotlinx-coroutines-core/js/src/internal/Synchronized.kt

+2
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ public actual typealias SynchronizedObject = Any
1818
@InternalCoroutinesApi
1919
public actual inline fun <T> synchronized(lock: SynchronizedObject, block: () -> T): T =
2020
block()
21+
22+
internal actual val isNativeMt: Boolean = false

kotlinx-coroutines-core/jvm/src/internal/Synchronized.kt

+2
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ public actual typealias SynchronizedObject = Any
1818
@InternalCoroutinesApi
1919
public actual inline fun <T> synchronized(lock: SynchronizedObject, block: () -> T): T =
2020
kotlin.synchronized(lock, block)
21+
22+
internal actual val isNativeMt: Boolean = false

kotlinx-coroutines-core/native/src/internal/Synchronized.kt

+3
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ public actual typealias SynchronizedObject = kotlinx.atomicfu.locks.Synchronized
1818
*/
1919
@InternalCoroutinesApi
2020
public actual inline fun <T> synchronized(lock: SynchronizedObject, block: () -> T): T = lock.withLock2(block)
21+
22+
@OptIn(ExperimentalStdlibApi::class)
23+
internal actual val isNativeMt: Boolean = !kotlin.native.isExperimentalMM()

0 commit comments

Comments
 (0)