2b2c685827
^KT-61259
27 lines
996 B
Kotlin
Vendored
27 lines
996 B
Kotlin
Vendored
@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, kotlin.native.runtime.NativeRuntimeApi::class, ObsoleteWorkersApi::class)
|
|
|
|
import kt42172.*
|
|
import kotlin.native.concurrent.*
|
|
import kotlinx.cinterop.*
|
|
|
|
fun main() {
|
|
val worker = Worker.start()
|
|
worker.execute(TransferMode.SAFE, {}) {
|
|
val withFinalizer = WithFinalizer()
|
|
val finalizer: Finalizer = staticCFunction { ptr: COpaquePointer? ->
|
|
ptr?.asStableRef<Any>()?.dispose()
|
|
println("Executed finalizer")
|
|
}
|
|
val arg = StableRef.create(Any()).asCPointer()
|
|
withFinalizer.setFinalizer(finalizer, arg)
|
|
}.result
|
|
worker.requestTermination().result
|
|
waitWorkerTermination(worker)
|
|
|
|
if (Platform.memoryModel == MemoryModel.EXPERIMENTAL) {
|
|
// Experimental MM by default doesn't run GC neither on worker termination nor on program exit.
|
|
// Enforce GC on program exit:
|
|
kotlin.native.runtime.Debugging.forceCheckedShutdown = true
|
|
}
|
|
}
|