Skip to content

Commit cfcf704

Browse files
committed
Kotlin/Native multithreading (WIP)
* Provides newSingleThreadedContext. * Provides Dispatchers.Main on iOS, Dispatchers.Default everywhere. * Coroutine references (Job) and all kinds of channels are shareable across workers. * Each individual coroutine is confined to a single worker. Known problems: * There are some memory leaks in concurrent cases.
1 parent 7e895fc commit cfcf704

File tree

103 files changed

+2886
-819
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+2886
-819
lines changed

README.md

+17-8
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,23 @@ You can also use [`kotlinx-coroutines-core`](https://www.npmjs.com/package/kotli
184184
[`kotlinx-coroutines-core-native`](https://search.maven.org/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core-native/1.3.2/jar)
185185
(follow the link to get the dependency declaration snippet).
186186

187-
Only single-threaded code (JS-style) on Kotlin/Native is currently supported.
188-
Kotlin/Native supports only Gradle version 4.10 and you need to enable Gradle metadata in your
189-
`settings.gradle` file:
187+
Kotlin/Native uses Gradle metadata and needs Gradle version 5.3 or later
188+
See [Gradle Metadata 1.0 announcement](https://blog.gradle.org/gradle-metadata-1.0) for more details.
190189

191-
```groovy
192-
enableFeaturePreview('GRADLE_METADATA')
193-
```
190+
Kotlin/Native does not generally provide binary compatibility between versions.
191+
You should use the same version of Kotlin/Native compiler as was used to build `kotlinx.coroutines`.
192+
193+
Kotlin/Native does not support free sharing of mutable objects between threads as on JVM, so several
194+
limitations apply to using coroutines on Kotlin/Native.
195+
See [Sharing and background threads on Kotlin/Native](kotlin-native-sharing.md) for details.
196+
197+
Some functions like [newSingleThreadContext] and [runBlocking] are available only for Kotlin/JVM and Kotlin/Native
198+
and are not available on Kotlin/JS. In order to access them from the code that is shared between JVM and Native
199+
you need to enable granular metadata (aka HMPP) in your `gradle.properties` file:
194200

195-
Since Kotlin/Native does not generally provide binary compatibility between versions,
196-
you should use the same version of Kotlin/Native compiler as was used to build `kotlinx.coroutines`.
201+
```properties
202+
kotlin.mpp.enableGranularSourceSetsMetadata=true
203+
```
197204

198205
## Building
199206

@@ -241,6 +248,8 @@ The `develop` branch is pushed to `master` during release.
241248
[Promise.await]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/kotlin.js.-promise/await.html
242249
[promise]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/promise.html
243250
[Window.asCoroutineDispatcher]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/org.w3c.dom.-window/as-coroutine-dispatcher.html
251+
[newSingleThreadContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/new-single-thread-context.html
252+
[runBlocking]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html
244253
<!--- INDEX kotlinx.coroutines.flow -->
245254
[Flow]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html
246255
[_flow]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/flow.html

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-core.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public abstract class kotlinx/coroutines/AbstractCoroutine : kotlinx/coroutines/
1313
protected fun onStart ()V
1414
public final fun resumeWith (Ljava/lang/Object;)V
1515
public final fun start (Lkotlinx/coroutines/CoroutineStart;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)V
16-
public final fun start (Lkotlinx/coroutines/CoroutineStart;Lkotlin/jvm/functions/Function1;)V
1716
}
1817

1918
public final class kotlinx/coroutines/AwaitKt {
@@ -80,10 +79,8 @@ public class kotlinx/coroutines/CancellableContinuationImpl : kotlin/coroutines/
8079

8180
public final class kotlinx/coroutines/CancellableContinuationKt {
8281
public static final fun disposeOnCancellation (Lkotlinx/coroutines/CancellableContinuation;Lkotlinx/coroutines/DisposableHandle;)V
83-
public static final fun suspendAtomicCancellableCoroutine (Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
8482
public static final fun suspendAtomicCancellableCoroutine (ZLkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
8583
public static synthetic fun suspendAtomicCancellableCoroutine$default (ZLkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
86-
public static final fun suspendCancellableCoroutine (Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
8784
}
8885

8986
public abstract interface class kotlinx/coroutines/ChildHandle : kotlinx/coroutines/DisposableHandle {
@@ -155,7 +152,7 @@ public abstract class kotlinx/coroutines/CoroutineDispatcher : kotlin/coroutines
155152
public abstract fun dispatch (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Runnable;)V
156153
public fun dispatchYield (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Runnable;)V
157154
public fun get (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext$Element;
158-
public final fun interceptContinuation (Lkotlin/coroutines/Continuation;)Lkotlin/coroutines/Continuation;
155+
public fun interceptContinuation (Lkotlin/coroutines/Continuation;)Lkotlin/coroutines/Continuation;
159156
public fun isDispatchNeeded (Lkotlin/coroutines/CoroutineContext;)Z
160157
public fun minusKey (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext;
161158
public final fun plus (Lkotlinx/coroutines/CoroutineDispatcher;)Lkotlinx/coroutines/CoroutineDispatcher;
@@ -220,8 +217,6 @@ public final class kotlinx/coroutines/CoroutineStart : java/lang/Enum {
220217
public static final field DEFAULT Lkotlinx/coroutines/CoroutineStart;
221218
public static final field LAZY Lkotlinx/coroutines/CoroutineStart;
222219
public static final field UNDISPATCHED Lkotlinx/coroutines/CoroutineStart;
223-
public final fun invoke (Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)V
224-
public final fun invoke (Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)V
225220
public final fun isLazy ()Z
226221
public static fun valueOf (Ljava/lang/String;)Lkotlinx/coroutines/CoroutineStart;
227222
public static fun values ()[Lkotlinx/coroutines/CoroutineStart;
@@ -488,6 +483,11 @@ public final class kotlinx/coroutines/SupervisorKt {
488483
public static final fun supervisorScope (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
489484
}
490485

486+
public final class kotlinx/coroutines/SuspendCancellableCoroutineKt {
487+
public static final fun suspendAtomicCancellableCoroutine (Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
488+
public static final fun suspendCancellableCoroutine (Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
489+
}
490+
491491
public abstract interface class kotlinx/coroutines/ThreadContextElement : kotlin/coroutines/CoroutineContext$Element {
492492
public abstract fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Object;)V
493493
public abstract fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Ljava/lang/Object;

gradle.properties

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44

55
# Kotlin
6-
version=1.3.2-SNAPSHOT
6+
version=1.3.2-native-mt-SNAPSHOT
77
group=org.jetbrains.kotlinx
88
kotlin_version=1.3.60
99

@@ -22,11 +22,15 @@ rxjava2_version=2.2.8
2222
gradle_node_version=1.2.0
2323
node_version=8.9.3
2424
npm_version=5.7.1
25-
mocha_version=4.1.0
25+
mocha_version=6.2.2
2626
mocha_headless_chrome_version=1.8.2
27-
mocha_teamcity_reporter_version=2.2.2
28-
source_map_support_version=0.5.3
27+
mocha_teamcity_reporter_version=3.0.0
28+
source_map_support_version=0.5.16
29+
jsdom_version=15.2.1
30+
jsdom_global_version=3.0.2
2931

3032
# Settings
3133
kotlin.incremental.multiplatform=true
3234
kotlin.native.ignoreDisabledTargets=true
35+
36+
kotlin.mpp.enableGranularSourceSetsMetadata=true

gradle/compile-native-multiplatform.gradle

+6-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ kotlin {
1010

1111
targets {
1212
if (project.ext.ideaActive) {
13-
fromPreset(project.ext.ideaPreset, 'native')
13+
// todo: This is IDEA kludge, without "fromPresent" the code does not resolve properly
14+
//fromPreset(project.ext.ideaPreset, 'native')
15+
addTarget(project.ext.ideaPreset)
1416
} else {
1517
addTarget(presets.linuxX64)
1618
addTarget(presets.iosArm64)
@@ -28,17 +30,9 @@ kotlin {
2830

2931
sourceSets {
3032
nativeMain { dependsOn commonMain }
31-
// Empty source set is required in order to have native tests task
32-
nativeTest {}
33+
nativeTest { dependsOn commonTest }
3334

34-
if (!project.ext.ideaActive) {
35-
configure(nativeMainSets) {
36-
dependsOn nativeMain
37-
}
38-
39-
configure(nativeTestSets) {
40-
dependsOn nativeTest
41-
}
42-
}
35+
configure(nativeMainSets) { dependsOn nativeMain }
36+
configure(nativeTestSets) { dependsOn nativeTest }
4337
}
4438
}

gradle/test-mocha-js.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ task testMochaChrome(type: NodeTask, dependsOn: prepareMochaChrome) {
7676
task installDependenciesMochaJsdom(type: NpmTask, dependsOn: [npmInstall]) {
7777
args = ['install',
7878
"mocha@$mocha_version",
79-
'jsdom',
80-
'jsdom-global',
79+
"jsdom@$jsdom_version",
80+
"jsdom-global@$jsdom_global_version",
8181
"source-map-support@$source_map_support_version",
8282
'--no-save']
8383
if (project.hasProperty("teamcity")) args += ["mocha-teamcity-reporter@$mocha_teamcity_reporter_version"]

0 commit comments

Comments
 (0)