portable linux mac getThreadId

This commit is contained in:
Erik Fröjdh
2026-05-11 11:59:28 +02:00
parent e9b6e87de5
commit 4e48139bba
9 changed files with 83 additions and 77 deletions
@@ -1,5 +1,6 @@
#include "CommandLineOptions.h"
#include "VirtualMatterhornServer.h"
#include "sls/thread_utils.h"
#include "sls/logger.h"
#include "sls/sls_detector_exceptions.h"
#include "sls/versionAPI.h"
@@ -10,20 +11,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;
@@ -84,7 +71,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
@@ -101,7 +88,7 @@ int main(int argc, char *argv[]) {
}
} catch (...) {
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
@@ -109,7 +96,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
@@ -3,6 +3,7 @@
#include "ClientInterface.h"
#include "sls/ServerSocket.h"
#include "sls/StaticVector.h"
#include "sls/thread_utils.h"
#include "sls/ToString.h"
#include "sls/sls_detector_exceptions.h"
@@ -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';
@@ -7,6 +7,7 @@
*/
#include "CommandLineOptions.h"
#include "sls/Receiver.h"
#include "sls/thread_utils.h"
#include "sls/ToString.h"
#include "sls/container_utils.h"
#include "sls/logger.h"
@@ -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
@@ -4,6 +4,7 @@
* binary */
#include "CommandLineOptions.h"
#include "sls/Receiver.h"
#include "sls/thread_utils.h"
#include "sls/ToString.h"
#include "sls/container_utils.h"
#include "sls/logger.h"
@@ -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
@@ -3,6 +3,7 @@
/* slsReceiver */
#include "CommandLineOptions.h"
#include "sls/Receiver.h"
#include "sls/thread_utils.h"
#include "sls/ToString.h"
#include "sls/container_utils.h"
#include "sls/logger.h"
@@ -13,12 +14,6 @@
#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
@@ -6,18 +6,13 @@
***********************************************/
#include "ThreadObject.h"
#include "sls/thread_utils.h"
#include "sls/container_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) {
+49
View File
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: LGPL-3.0-or-other
// Copyright (C) 2021 Contributors to the SLS Detector Package
#pragma once
/**
* @file thread_utils.h
* @short Portable per-thread identifier for diagnostics/logging.
*
* Returns a unique-within-process thread id of the calling thread:
* - Linux, glibc >= 2.30: the system gettid() (pid_t).
* - Linux, glibc < 2.30: syscall(SYS_gettid) (long, cast to pid_t).
* - macOS: pthread_threadid_np() (uint64_t, truncated to pid_t).
*
* The macOS kernel thread id is 64-bit. To stay drop-in compatible with the
* existing call sites that store the result in pid_t (e.g. ThreadObject::
* threadId, ClientInterface::parentThreadId / tcpThreadId), we truncate to
* pid_t. The value is used for diagnostics only and is still unique enough
* within a single process.
*/
#include <sys/types.h> // pid_t
#if defined(__APPLE__)
#include <cstdint>
#include <pthread.h>
#elif defined(__linux__)
#include <unistd.h>
#if !defined(__GLIBC__) || \
!(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30))
#include <sys/syscall.h>
#endif
#endif
namespace sls {
inline pid_t getThreadId() noexcept {
#if defined(__APPLE__)
std::uint64_t tid = 0;
pthread_threadid_np(nullptr, &tid);
return static_cast<pid_t>(tid);
#elif defined(__GLIBC__) && \
(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30))
return ::gettid();
#else
return static_cast<pid_t>(::syscall(SYS_gettid));
#endif
}
} // namespace sls