Skip to content

Commit 41f4865

Browse files
Migrate to the new memory model
1 parent 92e461b commit 41f4865

File tree

9 files changed

+65
-59
lines changed

9 files changed

+65
-59
lines changed

build.gradle

Lines changed: 0 additions & 46 deletions
This file was deleted.

build.gradle.kts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
plugins {
2+
id("org.jetbrains.kotlin.multiplatform") version "1.6.0-M1-139"
3+
}
4+
repositories {
5+
mavenCentral()
6+
maven {
7+
url = uri("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev")
8+
}
9+
}
10+
11+
val hostOs = System.getProperty("os.name")
12+
val isLinux = hostOs == "Linux"
13+
val isWindows = hostOs.startsWith("Windows")
14+
15+
kotlin {
16+
17+
if (isLinux){
18+
linuxX64("native")
19+
} else if(isWindows){
20+
mingwX64("native")
21+
}else {
22+
macosX64("native")
23+
}
24+
25+
sourceSets {
26+
val nativeMain by getting {
27+
dependencies {
28+
implementation("co.touchlab:stately-concurrency:1.1.7")
29+
}
30+
}
31+
}
32+
33+
targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
34+
binaries {
35+
executable {
36+
entryPoint = "sample.main"
37+
}
38+
}
39+
}
40+
}
41+
42+
// Use the following Gradle tasks to run your application:
43+
// :runReleaseExecutableMacos - without debug symbols
44+
// :runDebugExecutableMacos - with debug symbols

gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
kotlin.code.style=official
2-
kotlin.import.noCommonSourceSets=true
2+
kotlin.import.noCommonSourceSets=true
3+
kotlin.native.binary.memoryModel=experimental

settings.gradle

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
rootProject.name = 'KNConcurrencySamples'
1+
pluginManagement {
2+
repositories {
3+
maven {
4+
url "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev"
5+
}
6+
gradlePluginPortal()
7+
}
8+
}
9+
10+
rootProject.name = 'KNConcurrencySamples'

src/nativeMain/kotlin/sample/Background.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fun captureTooMuch(){
2424
println("I have ${model.count}")
2525

2626
model.increment()
27-
println("I have ${model.count}") //We won't get here
27+
println("I have ${model.count}") // We will get here the new MM
2828
}
2929

3030
class CountingModel{

src/nativeMain/kotlin/sample/BackgroundSupport.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import co.touchlab.stately.concurrency.ThreadRef
44
import kotlin.native.concurrent.Future
55
import kotlin.native.concurrent.TransferMode
66
import kotlin.native.concurrent.Worker
7-
import kotlin.native.concurrent.freeze
87

98
fun background(block: () -> Unit) {
10-
val future = worker.execute(TransferMode.SAFE, { block.freeze() }) {
9+
val future = worker.execute(TransferMode.SAFE, { block }) {
1110
it()
1211
}
1312
collectFutures.add(future)
@@ -27,7 +26,6 @@ fun teardownThreading() {
2726
worker.requestTermination()
2827
}
2928

30-
@SharedImmutable
3129
private val mainThreadRef: ThreadRef by lazy {
3230
ThreadRef()
3331
}

src/nativeMain/kotlin/sample/Debugging.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ fun ensureNeverFrozenBackground() {
1919
val sd = SomeData("a", 1)
2020
sd.ensureNeverFrozen()
2121
background {
22-
println("Won't get here $sd")
22+
println("We will get here with the new MM $sd")
2323
}
2424
}
2525

2626
fun captureTooMuchInit() {
2727
val model = CountingModelEnsure()
2828
model.increment()
29-
println("I have ${model.count}") //We won't even get here
29+
println("I have ${model.count}") //We will get here with the new MM
3030
}
3131

3232
class CountingModelEnsure{

src/nativeMain/kotlin/sample/GlobalState.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import kotlin.native.concurrent.Worker
77
fun cantChangeThis(){
88
println("i ${DefaultGlobalState.i}")
99
DefaultGlobalState.i++
10-
println("i ${DefaultGlobalState.i}") //We won't get here
10+
println("i ${DefaultGlobalState.i}") // We will get here with the new MM
1111
}
1212

1313
object DefaultGlobalState{
@@ -37,7 +37,7 @@ fun threadLocalDifferentThreads(){
3737
fun companionAlsoFrozen(){
3838
println("i ${GlobalStateCompanion.i}")
3939
GlobalStateCompanion.i++
40-
println("i ${GlobalStateCompanion.i}") //We won't get here
40+
println("i ${GlobalStateCompanion.i}") // We will get here with the new MM
4141
}
4242

4343
class GlobalStateCompanion{
@@ -55,7 +55,7 @@ fun globalCounting(){
5555

5656
var globalCounterData = SomeMutableData(33)
5757

58-
fun globalCountingFail(){
58+
fun globalCountingDontFail(){
5959
background {
6060
try {
6161
globalCounterData.i++
@@ -69,6 +69,6 @@ fun globalCountingFail(){
6969
@SharedImmutable
7070
val globalCounterDataShared = SomeMutableData(33)
7171

72-
fun globalCountingSharedFail(){
72+
fun globalCountingSharedDontFail(){
7373
globalCounterDataShared.i++
7474
}

src/nativeMain/kotlin/sample/SampleMain.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fun main() {
2020
// threadLocalDifferentThreads()
2121
// companionAlsoFrozen()
2222
// globalCounting()
23-
// globalCountingFail()
23+
// globalCountingDontFail()
2424
// globalCountingSharedFail()
2525

2626
// 4) Background

0 commit comments

Comments
 (0)