File tree Expand file tree Collapse file tree 9 files changed +65
-59
lines changed
src/nativeMain/kotlin/sample Expand file tree Collapse file tree 9 files changed +65
-59
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 11kotlin.code.style =official
2- kotlin.import.noCommonSourceSets =true
2+ kotlin.import.noCommonSourceSets =true
3+ kotlin.native.binary.memoryModel =experimental
Original file line number Diff line number Diff line change 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'
Original file line number Diff line number Diff 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
3030class CountingModel {
Original file line number Diff line number Diff line change @@ -4,10 +4,9 @@ import co.touchlab.stately.concurrency.ThreadRef
44import kotlin.native.concurrent.Future
55import kotlin.native.concurrent.TransferMode
66import kotlin.native.concurrent.Worker
7- import kotlin.native.concurrent.freeze
87
98fun 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
3129private val mainThreadRef: ThreadRef by lazy {
3230 ThreadRef ()
3331}
Original file line number Diff line number Diff 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
2626fun 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
3232class CountingModelEnsure {
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ import kotlin.native.concurrent.Worker
77fun 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
1313object DefaultGlobalState{
@@ -37,7 +37,7 @@ fun threadLocalDifferentThreads(){
3737fun 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
4343class GlobalStateCompanion {
@@ -55,7 +55,7 @@ fun globalCounting(){
5555
5656var 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
7070val globalCounterDataShared = SomeMutableData (33 )
7171
72- fun globalCountingSharedFail (){
72+ fun globalCountingSharedDontFail (){
7373 globalCounterDataShared.i++
7474}
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ fun main() {
2020// threadLocalDifferentThreads()
2121// companionAlsoFrozen()
2222// globalCounting()
23- // globalCountingFail ()
23+ // globalCountingDontFail ()
2424// globalCountingSharedFail()
2525
2626 // 4) Background
You can’t perform that action at this time.
0 commit comments