Skip to content

Commit a613bb7

Browse files
committed
Make the code build without changes to the core module
1 parent 0ab0d45 commit a613bb7

File tree

9 files changed

+99
-64
lines changed

9 files changed

+99
-64
lines changed

kotlinx-coroutines-core/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ kotlin {
8181
}
8282
languageSettings {
8383
progressiveMode = true
84-
optInAnnotations.each { useExperimentalAnnotation(it) }
84+
optInAnnotations.each { optIn(it) }
8585
}
8686
}
8787

kotlinx-coroutines-core/common/src/internal/MainDispatchers.kt renamed to kotlinx-coroutines-core/jvm/src/internal/MainDispatchers.kt

+38-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,45 @@
55
package kotlinx.coroutines.internal
66

77
import kotlinx.coroutines.*
8+
import java.util.*
89
import kotlin.coroutines.*
910

11+
/**
12+
* Name of the boolean property that enables using of [FastServiceLoader].
13+
*/
14+
private const val FAST_SERVICE_LOADER_PROPERTY_NAME = "kotlinx.coroutines.fast.service.loader"
15+
16+
// Lazy loader for the main dispatcher
17+
internal object MainDispatcherLoader {
18+
19+
private val FAST_SERVICE_LOADER_ENABLED = systemProp(FAST_SERVICE_LOADER_PROPERTY_NAME, true)
20+
21+
@JvmField
22+
val dispatcher: MainCoroutineDispatcher = loadMainDispatcher()
23+
24+
private fun loadMainDispatcher(): MainCoroutineDispatcher {
25+
return try {
26+
val factories = if (FAST_SERVICE_LOADER_ENABLED) {
27+
FastServiceLoader.loadMainDispatcherFactory()
28+
} else {
29+
// We are explicitly using the
30+
// `ServiceLoader.load(MyClass::class.java, MyClass::class.java.classLoader).iterator()`
31+
// form of the ServiceLoader call to enable R8 optimization when compiled on Android.
32+
ServiceLoader.load(
33+
MainDispatcherFactory::class.java,
34+
MainDispatcherFactory::class.java.classLoader
35+
).iterator().asSequence().toList()
36+
}
37+
@Suppress("ConstantConditionIf")
38+
factories.maxByOrNull { it.loadPriority }?.tryCreateDispatcher(factories)
39+
?: createMissingDispatcher()
40+
} catch (e: Throwable) {
41+
// Service loader can throw an exception as well
42+
createMissingDispatcher(e)
43+
}
44+
}
45+
}
46+
1047
/**
1148
* If anything goes wrong while trying to create main dispatcher (class not found,
1249
* initialization failed, etc), then replace the main dispatcher with a special
@@ -34,7 +71,7 @@ private val SUPPORT_MISSING = true
3471
"ConstantConditionIf",
3572
"IMPLICIT_NOTHING_TYPE_ARGUMENT_AGAINST_NOT_NOTHING_EXPECTED_TYPE" // KT-47626
3673
)
37-
internal fun createMissingDispatcher(cause: Throwable? = null, errorHint: String? = null): MainCoroutineDispatcher =
74+
private fun createMissingDispatcher(cause: Throwable? = null, errorHint: String? = null) =
3875
if (SUPPORT_MISSING) MissingMainCoroutineDispatcher(cause, errorHint) else
3976
cause?.let { throw it } ?: throwMissingMainDispatcherException()
4077

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

-46
This file was deleted.

kotlinx-coroutines-test/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ kotlin {
1919
resources.srcDir("$platform/test-resources")
2020
}
2121
languageSettings {
22-
experimentalAnnotations.forEach { useExperimentalAnnotation(it) }
22+
experimentalAnnotations.forEach { optIn(it) }
2323
}
2424
}
2525
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
/*
22
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
4-
@file:Suppress("unused")
5-
@file:JvmName("TestDispatchers")
64

75
package kotlinx.coroutines.test
86

97
import kotlinx.coroutines.*
10-
import kotlinx.coroutines.test.internal.*
11-
import kotlin.jvm.*
128

139
/**
1410
* Sets the given [dispatcher] as an underlying dispatcher of [Dispatchers.Main].
@@ -17,12 +13,7 @@ import kotlin.jvm.*
1713
* It is unsafe to call this method if alive coroutines launched in [Dispatchers.Main] exist.
1814
*/
1915
@ExperimentalCoroutinesApi
20-
public fun Dispatchers.setMain(dispatcher: CoroutineDispatcher) {
21-
require(dispatcher !is TestMainDispatcher) { "Dispatchers.setMain(Dispatchers.Main) is prohibited, probably Dispatchers.resetMain() should be used instead" }
22-
val mainDispatcher = Main
23-
require(mainDispatcher is TestMainDispatcher) { "TestMainDispatcher is not set as main dispatcher, have $mainDispatcher instead." }
24-
mainDispatcher.setDispatcher(dispatcher)
25-
}
16+
public expect fun Dispatchers.setMain(dispatcher: CoroutineDispatcher)
2617

2718
/**
2819
* Resets state of the [Dispatchers.Main] to the original main dispatcher.
@@ -32,8 +23,4 @@ public fun Dispatchers.setMain(dispatcher: CoroutineDispatcher) {
3223
* It is unsafe to call this method if alive coroutines launched in [Dispatchers.Main] exist.
3324
*/
3425
@ExperimentalCoroutinesApi
35-
public fun Dispatchers.resetMain() {
36-
val mainDispatcher = Main
37-
require(mainDispatcher is TestMainDispatcher) { "TestMainDispatcher is not set as main dispatcher, have $mainDispatcher instead." }
38-
mainDispatcher.resetDispatcher()
39-
}
26+
public expect fun Dispatchers.resetMain()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.test
6+
import kotlinx.coroutines.*
7+
8+
@ExperimentalCoroutinesApi
9+
public actual fun Dispatchers.setMain(dispatcher: CoroutineDispatcher) {
10+
throw UnsupportedOperationException("`setMain` is not supported on JS")
11+
}
12+
13+
@ExperimentalCoroutinesApi
14+
public actual fun Dispatchers.resetMain() {
15+
throw UnsupportedOperationException("`resetMain` is not supported on JS")
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
@file:JvmName("TestDispatchers")
5+
6+
package kotlinx.coroutines.test
7+
8+
import kotlinx.coroutines.*
9+
import kotlinx.coroutines.test.internal.*
10+
11+
@ExperimentalCoroutinesApi
12+
public actual fun Dispatchers.setMain(dispatcher: CoroutineDispatcher) {
13+
require(dispatcher !is TestMainDispatcher) { "Dispatchers.setMain(Dispatchers.Main) is prohibited, probably Dispatchers.resetMain() should be used instead" }
14+
val mainDispatcher = Main
15+
require(mainDispatcher is TestMainDispatcher) { "TestMainDispatcher is not set as main dispatcher, have $mainDispatcher instead." }
16+
mainDispatcher.setDispatcher(dispatcher)
17+
}
18+
19+
@ExperimentalCoroutinesApi
20+
public actual fun Dispatchers.resetMain() {
21+
val mainDispatcher = Main
22+
require(mainDispatcher is TestMainDispatcher) { "TestMainDispatcher is not set as main dispatcher, have $mainDispatcher instead." }
23+
mainDispatcher.resetDispatcher()
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.test
6+
7+
import kotlinx.coroutines.*
8+
9+
@ExperimentalCoroutinesApi
10+
public actual fun Dispatchers.setMain(dispatcher: CoroutineDispatcher) {
11+
throw UnsupportedOperationException("`setMain` is not supported on Native")
12+
}
13+
14+
@ExperimentalCoroutinesApi
15+
public actual fun Dispatchers.resetMain() {
16+
throw UnsupportedOperationException("`resetMain` is not supported on Native")
17+
}

0 commit comments

Comments
 (0)