David
09/24/2018, 2:56 PMIllegalStateException
. Any suggestions on how to improve?
import kotlin.native.concurrent.*
class KotlinNativeFramework {
var listener: KotlinNativeFrameworkListener? = null
fun startWorking() {
val myInput = DoubleArray(10) { it.toDouble() }.toList()
val future = Worker.start().execute(TransferMode.SAFE, {
myInput }) { input ->
val doSomeWork = DoSomeWork()
doSomeWork.run(input)
}
future.consume {
result -> listener?.resultsReady(result)
}
}
}
interface KotlinNativeFrameworkListener {
fun resultsReady(result: WorkResult)
}
olonho
09/24/2018, 3:26 PMolonho
09/24/2018, 3:58 PMDavid
09/24/2018, 7:01 PMmyInput
without success. The problem with a producer lambda is that the input data is created over time, then once in a while I want to take that data and process. The data is not handled at any other place in the code after being sent to the worker, so there should be no risk of concurrent modifications. Also, the amount of data in myInput
is rather large so a producer lambda that copies data must be avoided.olonho
09/24/2018, 7:19 PMDavid
09/24/2018, 7:44 PMfreeze()
does help for this error, I apparently had another error “Invalid pointer dequeued from free list” when applying freeze() (in the real code). I have to investigate further to understand how to get this in the example.David
09/25/2018, 1:35 PMolonho
09/26/2018, 9:06 AMgyroSumComputer
better be instantiated on the worker’s sideolonho
09/26/2018, 9:08 AM@ThreadLocal
olonho
09/26/2018, 9:09 AMolonho
09/26/2018, 9:12 AMDavid
09/26/2018, 11:29 AM@ThreadLocal
variable for gyroSumComputer. However, I am getting kotlin.IllegalStateException: Illegal transfer state
. The work in execute seems to be done and a result is computed, however the input to consume is null.
https://github.com/dtornqvist/kotlin-native-threading-debug/blob/kotlin-native-worker/MySimpleProject/KotlinNativeFramework/src/main/kotlin/GyroSumWorker.ktolonho
09/26/2018, 12:43 PMcurrentOutput
field is accessible from both inside worker and passed outside. For example following diff would make it work as expected: diff --git a/MySimpleProject/KotlinNativeFramework/build.gradle b/MySimpleProject/KotlinNativeFramework/build.gradle
index 4dd4e34..2aab1d4 100755
--- a/MySimpleProject/KotlinNativeFramework/build.gradle
+++ b/MySimpleProject/KotlinNativeFramework/build.gradle
@@ -13,23 +13,15 @@ buildscript {
}
}
-apply plugin: 'kotlin'
-
-repositories {
- mavenCentral()
-}
-
-dependencies {
- compile "org.jetbrains.kotlin:kotlin-stdlib"
-}
-
apply plugin: 'konan'
konan.targets = [
- 'ios_arm64', 'ios_x64'
+ //'ios_arm64', 'ios_x64'
+ 'macos_x64'
]
konanArtifacts {
- framework('KotlinNativeFramework')
+ // framework('KotlinNativeFramework')
+ program('KotlinNativeTest')
}
-
\ No newline at end of file
+
diff --git a/MySimpleProject/KotlinNativeFramework/src/main/kotlin/GyroSumComputer.kt b/MySimpleProject/KotlinNativeFramework/src/main/kotlin/GyroSumComputer.kt
index 63f52fa..37abeca 100755
--- a/MySimpleProject/KotlinNativeFramework/src/main/kotlin/GyroSumComputer.kt
+++ b/MySimpleProject/KotlinNativeFramework/src/main/kotlin/GyroSumComputer.kt
@@ -9,10 +9,10 @@ class GyroSumComputer {
fun performOperation(input: InputData): OutputData {
currentOutput = OutputData(currentOutput.xSum + input.x)
- return currentOutput
+ return OutputData(currentOutput.xSum)
}
}
data class OutputData(val xSum: Double)
-data class InputData(val x: Double)
\ No newline at end of file
+data class InputData(val x: Double)
diff --git a/MySimpleProject/KotlinNativeFramework/src/main/kotlin/GyroSumWorker.kt b/MySimpleProject/KotlinNativeFramework/src/main/kotlin/GyroSumWorker.kt
index 9499f72..62f8811 100644
--- a/MySimpleProject/KotlinNativeFramework/src/main/kotlin/GyroSumWorker.kt
+++ b/MySimpleProject/KotlinNativeFramework/src/main/kotlin/GyroSumWorker.kt
@@ -10,14 +10,14 @@ class GyroSumWorker {
private val worker = Worker.start()
init {
- worker.execute(TransferMode.SAFE, { GyroSumComputer() }) {
- gyroSumComputer = it
+ worker.execute(TransferMode.SAFE, { null }) {
+ gyroSumComputer = GyroSumComputer()
}
}
fun performOperation(input: InputData) {
input.freeze()
- worker.execute(TransferMode.SAFE, { input }) {
+ worker.execute(TransferMode.SAFE, { input }) { it ->
val res = gyroSumComputer?.performOperation(it)
println("res = " + res) // res is not null
res
@@ -31,3 +31,8 @@ class GyroSumWorker {
interface GyroSumWorkerListener {
fun gyroSumUpdate(outputData: OutputData)
}
+
+fun main() {
+ val w = GyroSumWorker()
+ println(w.performOperation(InputData(1.0)))
+}
David
09/26/2018, 12:44 PMgyroSumComputer
stored it’s result internally and then returned it. As gyroSumComputer is @ThreadLocal
this didn’t work…David
09/26/2018, 12:46 PMolonho
09/26/2018, 12:48 PMolonho
09/26/2018, 12:51 PMWorker
object is frozen, and can be liberally used from any execution contextDavid
09/26/2018, 1:02 PM