Generate initRuntimeIfNeeded() at the beginning of staticCFunction

#KT-44283 Fixed
This commit is contained in:
Svyatoslav Scherbina
2021-06-16 06:39:51 +00:00
committed by Space
parent 6e9739caec
commit e324c9b3fb
4 changed files with 68 additions and 1 deletions
@@ -0,0 +1,25 @@
---
#include <stdlib.h>
#include <pthread.h>
typedef struct {
int d;
} TestStruct;
typedef struct {
void (*f)(TestStruct data);
} ThreadData;
void *dispatch(void *rawArg) {
ThreadData *arg = rawArg;
arg->f((TestStruct) {.d = 1});
return NULL;
}
void invokeFromThread(void (*f)(TestStruct data)) {
pthread_t thread_id;
ThreadData *threadData = malloc(sizeof(ThreadData));
threadData->f = f;
pthread_create(&thread_id, NULL, dispatch, (void *) threadData);
pthread_join(thread_id, NULL);
}
@@ -0,0 +1,21 @@
import kotlinx.cinterop.*
import kt44283.*
import kotlin.native.concurrent.AtomicInt
import kotlin.test.*
val callbackCounter = AtomicInt(0)
fun main() {
val func = staticCFunction<CValue<TestStruct>, Unit> {
kotlin.native.internal.GC.collect() // Helps to ensure that "runtime" is already initialized.
memScoped {
println("Hello, Kotlin/Native! ${it.ptr.pointed.d}")
}
callbackCounter.increment()
}
assertEquals(0, callbackCounter.value)
invokeFromThread(func.reinterpret())
assertEquals(1, callbackCounter.value)
}