Native: move samples to backend.native/tests/
This commit is contained in:
committed by
Space
parent
b7337d2e64
commit
7bf6d64cfb
@@ -0,0 +1,27 @@
|
||||
plugins {
|
||||
kotlin("multiplatform")
|
||||
}
|
||||
|
||||
kotlin {
|
||||
// Determine host preset.
|
||||
val hostOs = System.getProperty("os.name")
|
||||
|
||||
// Create target for the host platform.
|
||||
val hostTarget = when {
|
||||
hostOs == "Mac OS X" -> macosX64("globalState")
|
||||
hostOs == "Linux" -> linuxX64("globalState")
|
||||
hostOs.startsWith("Windows") -> mingwX64("globalState")
|
||||
else -> throw GradleException("Host OS '$hostOs' is not supported in Kotlin/Native $project.")
|
||||
}
|
||||
|
||||
hostTarget.apply {
|
||||
binaries {
|
||||
executable {
|
||||
entryPoint = "sample.globalstate.main"
|
||||
}
|
||||
}
|
||||
compilations["main"].cinterops {
|
||||
val global by creating
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
kotlin.code.style=official
|
||||
kotlin.import.noCommonSourceSets=true
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package sample.globalstate
|
||||
|
||||
import kotlin.native.concurrent.*
|
||||
import kotlinx.cinterop.*
|
||||
import platform.posix.*
|
||||
|
||||
inline fun Int.ensureUnixCallResult(op: String, predicate: (Int) -> Boolean = { x -> x == 0} ): Int {
|
||||
if (!predicate(this)) {
|
||||
throw Error("$op: ${strerror(posix_errno())!!.toKString()}")
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
data class SharedDataMember(val double: Double)
|
||||
|
||||
data class SharedData(val string: String, val int: Int, val member: SharedDataMember)
|
||||
|
||||
// Here we access the same shared Kotlin object from multiple threads.
|
||||
val globalObject: SharedData?
|
||||
get() = sharedData.kotlinObject?.asStableRef<SharedData>()?.get()
|
||||
|
||||
fun dumpShared(prefix: String) {
|
||||
println("""
|
||||
$prefix: ${pthread_self()} x=${sharedData.x} f=${sharedData.f} s=${sharedData.string!!.toKString()}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun main() {
|
||||
// Arena owning all native allocs.
|
||||
val arena = Arena()
|
||||
|
||||
// Assign global data.
|
||||
sharedData.x = 239
|
||||
sharedData.f = 0.5f
|
||||
sharedData.string = "Hello Kotlin!".cstr.getPointer(arena)
|
||||
|
||||
// Here we create shared object reference,
|
||||
val stableRef = StableRef.create(SharedData("Shared", 239, SharedDataMember(2.71)))
|
||||
sharedData.kotlinObject = stableRef.asCPointer()
|
||||
dumpShared("thread1")
|
||||
println("shared is $globalObject")
|
||||
|
||||
// Start a new thread, that sees the variable.
|
||||
// memScoped is needed to pass thread's local address to pthread_create().
|
||||
memScoped {
|
||||
val thread = alloc<pthread_tVar>()
|
||||
pthread_create(thread.ptr, null, staticCFunction { argC ->
|
||||
initRuntimeIfNeeded()
|
||||
dumpShared("thread2")
|
||||
val arg = argC!!.asStableRef<SharedDataMember>()
|
||||
println("thread arg is ${arg.get()} shared is $globalObject")
|
||||
arg.dispose()
|
||||
// Workaround for compiler issue.
|
||||
null as COpaquePointer?
|
||||
}, StableRef.create(SharedDataMember(3.14)).asCPointer() ).ensureUnixCallResult("pthread_create")
|
||||
pthread_join(thread.value, null).ensureUnixCallResult("pthread_join")
|
||||
}
|
||||
|
||||
// At this moment we do not need data stored in shared data, so clean up the data
|
||||
// and free memory.
|
||||
sharedData.string = null
|
||||
stableRef.dispose()
|
||||
arena.clear()
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package = sample.globalstate
|
||||
|
||||
---
|
||||
typedef struct {
|
||||
int x;
|
||||
float f;
|
||||
char* string;
|
||||
void* kotlinObject;
|
||||
} SharedDataStruct;
|
||||
|
||||
SharedDataStruct sharedData;
|
||||
Reference in New Issue
Block a user