Fix pthread to number converstion ^KT-52429

Merge-request: KT-MR-6918
Merged-by: Alexander Shabalin <Alexander.Shabalin@jetbrains.com>
This commit is contained in:
Alexander Shabalin
2022-08-23 16:36:24 +00:00
committed by Space
parent 45fc5fb76b
commit 58569f2eee
2 changed files with 12 additions and 4 deletions
@@ -1,6 +1,7 @@
#include "workerSignals.h"
#include <cassert>
#include <cstring>
#include <pthread.h>
#include <signal.h>
@@ -21,7 +22,8 @@ extern "C" void setupSignalHandler(void) {
extern "C" void signalThread(uint64_t thread, int value) {
pendingValue = value;
auto t = reinterpret_cast<pthread_t>(thread);
pthread_t t = {};
memcpy(&t, &thread, sizeof(pthread_t));
auto result = pthread_kill(t, SIGUSR1);
assert(result == 0);
}
@@ -249,6 +249,14 @@ void waitInNativeState(pthread_cond_t* cond,
CallWithThreadState<ThreadState::kNative>(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<KULong>(thread);
return pthreadToNumber(it->second->thread());
}
OBJ_GETTER0(getActiveWorkers) {