Mac compatible version of gettid() (#1585)
Build on RHEL9 docker image / build (push) Successful in 4m47s
Build on RHEL8 docker image / build (push) Successful in 5m3s
Build and Deploy on local RHEL9 / build (push) Successful in 2m5s
Build and Deploy on local RHEL8 / build (push) Successful in 5m0s
Run Simulator Tests on local RHEL9 / build (push) Successful in 20m42s
Run Simulator Tests on local RHEL8 / build (push) Successful in 24m10s

* added getThreadId
This commit is contained in:
Erik Fröjdh
2026-10-13 08:23:56 +02:00
committed by GitHub
parent 649a36ea09
commit c87cc7e515
9 changed files with 70 additions and 77 deletions
@@ -3,6 +3,7 @@
#include "helpers/Helpers.hpp"
#include "sls/logger.h"
#include "sls/sls_detector_exceptions.h"
#include "sls/thread_utils.h"
#include "sls/versionAPI.h"
#include <semaphore.h>
@@ -12,20 +13,6 @@
#include <sys/wait.h>
#include <unistd.h>
// gettid added in glibc 2.30
#if defined(__APPLE__)
#include <cstdint>
#include <pthread.h>
static inline uint64_t gettid() {
uint64_t tid = 0;
pthread_threadid_np(nullptr, &tid);
return tid;
}
#elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
#endif
using namespace sls;
pid_t pid = -1;
@@ -90,7 +77,7 @@ int main(int argc, char *argv[]) {
// also return EXIT_FAILURE
}
LOG(TLogLevel::logINFOBLUE)
<< "Exiting Stop Server [ Tid: " << gettid() << " ]";
<< "Exiting Stop Server [ Tid: " << getThreadId() << " ]";
LOG(sls::logINFO) << "Exiting Stop Server";
} else if (pid > 0) {
// parent
@@ -108,7 +95,7 @@ int main(int argc, char *argv[]) {
} catch (...) {
LOG(logERROR) << "Some Error occured in Control Server, exiting";
LOG(sls::logINFOBLUE)
<< "Exiting Control Server [ Tid: " << gettid() << " ]";
<< "Exiting Control Server [ Tid: " << getThreadId() << " ]";
LOG(sls::logINFO) << "Exiting Detector Server";
kill(pid, SIGINT); // tell child to exit
waitpid(pid, nullptr, 0); // wait for child to exit
@@ -116,7 +103,8 @@ int main(int argc, char *argv[]) {
}
waitpid(pid, nullptr, 0); // wait for child to exit
LOG(sls::logINFOBLUE)
<< "Exiting Detector Control Server [ Tid: " << gettid() << " ]";
<< "Exiting Detector Control Server [ Tid: " << getThreadId()
<< " ]";
LOG(sls::logINFO) << "Exiting Detector Server";
} else {
LOG(sls::logERROR)
+2 -7
View File
@@ -2,6 +2,7 @@
// Copyright (C) 2021 Contributors to the SLS Detector Package
#include "Arping.h"
#include "sls/thread_utils.h"
#include <chrono>
#include <signal.h>
@@ -11,12 +12,6 @@
namespace sls {
// gettid added in glibc 2.30
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
#endif
void func(int signum) { wait(NULL); }
Arping::Arping() {}
@@ -59,7 +54,7 @@ void Arping::StartProcess() {
childPid = fork();
// child process
if (childPid == 0) {
LOG(logINFOBLUE) << "Created [ Arping Process, Tid: " << gettid()
LOG(logINFOBLUE) << "Created [ Arping Process, Tid: " << getThreadId()
<< " ]";
ProcessExecution();
}
+3 -8
View File
@@ -7,6 +7,7 @@
#include "sls/sls_detector_exceptions.h"
#include "sls/string_utils.h"
#include "sls/thread_utils.h"
#include "sls/versionAPI.h"
#include <array>
@@ -27,12 +28,6 @@ namespace sls {
using ns = std::chrono::nanoseconds;
using Interface = ServerInterface;
// gettid added in glibc 2.30
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
#endif
std::mutex ClientInterface::callbackMutex;
ClientInterface::~ClientInterface() {
@@ -47,7 +42,7 @@ ClientInterface::ClientInterface(uint16_t portNumber)
: detType(GENERIC), portNumber(portNumber), server(portNumber) {
validatePortNumber(portNumber);
functionTable();
parentThreadId = gettid();
parentThreadId = getThreadId();
tcpThread =
make_unique<std::thread>(&ClientInterface::startTCPServer, this);
}
@@ -79,7 +74,7 @@ void ClientInterface::registerCallBackRawDataReady(
}
void ClientInterface::startTCPServer() {
tcpThreadId = gettid();
tcpThreadId = getThreadId();
LOG(logINFOBLUE) << "Created [ TCP server Tid: " << tcpThreadId << "]";
LOG(logINFO) << "SLS Receiver starting TCP Server on port " << portNumber
<< '\n';
@@ -12,6 +12,7 @@
#include "sls/logger.h"
#include "sls/network_utils.h"
#include "sls/sls_detector_defs.h"
#include "sls/thread_utils.h"
#include <csignal> //SIGINT
#include <cstdio>
@@ -31,12 +32,6 @@
std::vector<std::thread> threads;
sls::TLogLevel printHeadersLevel = sls::logDEBUG;
// gettid added in glibc 2.30
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
#endif
/** Define Colors to print data call back in different colors for different
* recievers */
#define PRINT_IN_COLOR(c, f, ...) \
@@ -522,7 +517,8 @@ int main(int argc, char *argv[]) {
return EXIT_SUCCESS;
}
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']';
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << sls::getThreadId()
<< ']';
// close files on ctrl+c
sls::setupSignalHandler(SIGINT, sigInterruptHandler);
@@ -548,8 +544,8 @@ int main(int argc, char *argv[]) {
sem_t *semaphore = &semaphores[i];
threads.emplace_back(
[i, semaphore, port, user_data, &threadException]() {
LOG(sls::logINFOBLUE)
<< "Thread " << i << " [ Tid: " << gettid() << ']';
LOG(sls::logINFOBLUE) << "Thread " << i << " [ Tid: "
<< sls::getThreadId() << ']';
try {
sls::Receiver receiver(port);
receiver.registerCallBackStartAcquisition(
@@ -567,8 +563,8 @@ int main(int argc, char *argv[]) {
threadException = std::current_exception();
raise(SIGINT);
}
LOG(sls::logINFOBLUE)
<< "Exiting Thread " << i << " [ Tid: " << gettid() << " ]";
LOG(sls::logINFOBLUE) << "Exiting Thread " << i << " [ Tid: "
<< sls::getThreadId() << " ]";
});
}
+9 -12
View File
@@ -9,6 +9,7 @@
#include "sls/logger.h"
#include "sls/network_utils.h"
#include "sls/sls_detector_defs.h"
#include "sls/thread_utils.h"
#include <csignal> //SIGINT
#include <cstring>
@@ -16,12 +17,6 @@
#include <sys/wait.h> //wait
#include <unistd.h>
// gettid added in glibc 2.30
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
#endif
/** Define Colors to print data call back in different colors for different
* recievers */
#define PRINT_IN_COLOR(c, f, ...) \
@@ -156,7 +151,8 @@ int main(int argc, char *argv[]) {
return EXIT_SUCCESS;
}
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << ']';
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << sls::getThreadId()
<< ']';
// close files on ctrl+c
sls::setupSignalHandler(SIGINT, sigInterruptHandler);
@@ -182,7 +178,8 @@ int main(int argc, char *argv[]) {
/** - if child process */
else if (pid == 0) {
LOG(sls::logINFOBLUE)
<< "Child process " << i << " [ Tid: " << gettid() << ']';
<< "Child process " << i << " [ Tid: " << sls::getThreadId()
<< ']';
try {
uint16_t port = m.port + i;
@@ -213,13 +210,13 @@ int main(int argc, char *argv[]) {
// each child process gets a copy of the semaphore
sem_wait(&semaphore);
sem_destroy(&semaphore);
LOG(sls::logINFOBLUE)
<< "Exiting Child Process [ Tid: " << gettid() << ']';
LOG(sls::logINFOBLUE) << "Exiting Child Process [ Tid: "
<< sls::getThreadId() << ']';
exit(EXIT_SUCCESS);
} catch (...) {
sem_destroy(&semaphore);
LOG(sls::logINFOBLUE)
<< "Exiting Child Process [ Tid: " << gettid() << " ]";
LOG(sls::logINFOBLUE) << "Exiting Child Process [ Tid: "
<< sls::getThreadId() << " ]";
exit(EXIT_FAILURE);
}
}
-6
View File
@@ -19,12 +19,6 @@
namespace sls {
// gettid added in glibc 2.30
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
#endif
Receiver::~Receiver() = default;
Receiver::Receiver(uint16_t port) {
+6 -9
View File
@@ -8,17 +8,12 @@
#include "sls/logger.h"
#include "sls/network_utils.h"
#include "sls/sls_detector_defs.h"
#include "sls/thread_utils.h"
#include <csignal> //SIGINT
#include <semaphore.h>
#include <unistd.h>
// gettid added in glibc 2.30
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
#endif
sem_t semaphore;
/**
@@ -44,7 +39,8 @@ int main(int argc, char *argv[]) {
return EXIT_SUCCESS;
}
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << gettid() << " ]";
LOG(sls::logINFOBLUE) << "Current Process [ Tid: " << sls::getThreadId()
<< " ]";
// close files on ctrl+c
sls::setupSignalHandler(SIGINT, sigInterruptHandler);
@@ -60,10 +56,11 @@ int main(int argc, char *argv[]) {
sem_destroy(&semaphore);
} catch (...) {
sem_destroy(&semaphore);
LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << gettid() << " ]";
LOG(sls::logINFOBLUE)
<< "Exiting [ Tid: " << sls::getThreadId() << " ]";
throw;
}
LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << gettid() << " ]";
LOG(sls::logINFOBLUE) << "Exiting [ Tid: " << sls::getThreadId() << " ]";
LOG(sls::logINFO) << "Exiting Receiver";
return EXIT_SUCCESS;
}
+2 -7
View File
@@ -7,17 +7,12 @@
#include "ThreadObject.h"
#include "sls/container_utils.h"
#include "sls/thread_utils.h"
#include <iostream>
#include <unistd.h>
namespace sls {
// gettid added in glibc 2.30
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
#endif
ThreadObject::ThreadObject(int index, std::string type)
: index(index), type(type) {
LOG(logDEBUG) << type << " thread created: " << index;
@@ -46,7 +41,7 @@ void ThreadObject::StartRunning() { runningFlag = true; }
void ThreadObject::StopRunning() { runningFlag = false; }
void ThreadObject::RunningThread() {
threadId = gettid();
threadId = getThreadId();
LOG(logINFOBLUE) << "Created [ " << type << "Thread " << index
<< ", Tid: " << threadId << "]";
while (!killThread) {
+36
View File
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: LGPL-3.0-or-other
// Copyright (C) 2021 Contributors to the SLS Detector Package
#pragma once
/**
* @file thread_utils.h
* @short id of the calling thread
*/
#include <sys/types.h> // pid_t
#if defined(__APPLE__)
#include <cstdint>
#include <pthread.h>
#elif defined(__linux__)
#include <sys/syscall.h>
#include <unistd.h>
#else
#error "getThreadId is not implemented for this platform"
#endif
namespace sls {
/** Kernel thread id of the calling thread. On macOS the 64-bit id is
* truncated to pid_t. */
inline pid_t getThreadId() noexcept {
#if defined(__APPLE__)
std::uint64_t tid = 0;
pthread_threadid_np(nullptr, &tid);
return static_cast<pid_t>(tid);
#else
return static_cast<pid_t>(::syscall(SYS_gettid));
#endif
}
} // namespace sls