diff --git a/kotlin-native/backend.native/tests/interop/workerSignals/workerSignals.cpp b/kotlin-native/backend.native/tests/interop/workerSignals/workerSignals.cpp index ad48a7953d9..383a58fe32a 100644 --- a/kotlin-native/backend.native/tests/interop/workerSignals/workerSignals.cpp +++ b/kotlin-native/backend.native/tests/interop/workerSignals/workerSignals.cpp @@ -1,6 +1,7 @@ #include "workerSignals.h" #include +#include #include #include @@ -21,7 +22,8 @@ extern "C" void setupSignalHandler(void) { extern "C" void signalThread(uint64_t thread, int value) { pendingValue = value; - auto t = reinterpret_cast(thread); + pthread_t t = {}; + memcpy(&t, &thread, sizeof(pthread_t)); auto result = pthread_kill(t, SIGUSR1); assert(result == 0); } diff --git a/kotlin-native/runtime/src/main/cpp/Worker.cpp b/kotlin-native/runtime/src/main/cpp/Worker.cpp index fd9e6763011..21071fdc05f 100644 --- a/kotlin-native/runtime/src/main/cpp/Worker.cpp +++ b/kotlin-native/runtime/src/main/cpp/Worker.cpp @@ -249,6 +249,14 @@ void waitInNativeState(pthread_cond_t* cond, CallWithThreadState(WaitOnCondVar, cond, mutex, timeoutNanoseconds, microsecondsPassed); } +KULong pthreadToNumber(pthread_t thread) { + static_assert(sizeof(pthread_t) <= sizeof(KULong), "Casting pthread_t to ULong will lose data"); + // That's almost std::bit_cast. The latter requires sizeof equality of types. + KULong result = 0; + memcpy(&result, &thread, sizeof(pthread_t)); + return result; +} + class Locker { public: explicit Locker(pthread_mutex_t* lock, bool switchThreadState = true) : lock_(lock), switchThreadState_(switchThreadState) { @@ -639,9 +647,7 @@ class State { if (it == workers_.end()) { ThrowWorkerAlreadyTerminated(); } - pthread_t thread = it->second->thread(); - static_assert(sizeof(pthread_t) <= sizeof(KULong), "Casting pthread_t to ULong will lose data"); - return reinterpret_cast(thread); + return pthreadToNumber(it->second->thread()); } OBJ_GETTER0(getActiveWorkers) {