diff --git a/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp b/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp index 8c37df196..71baa56f8 100644 --- a/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp +++ b/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp @@ -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 #include -// gettid added in glibc 2.30 -#if defined(__APPLE__) -#include -#include -static inline uint64_t gettid() { - uint64_t tid = 0; - pthread_threadid_np(nullptr, &tid); - return tid; -} -#elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#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) diff --git a/slsReceiverSoftware/src/Arping.cpp b/slsReceiverSoftware/src/Arping.cpp index 7633e041b..28eccfc60 100644 --- a/slsReceiverSoftware/src/Arping.cpp +++ b/slsReceiverSoftware/src/Arping.cpp @@ -2,6 +2,7 @@ // Copyright (C) 2021 Contributors to the SLS Detector Package #include "Arping.h" +#include "sls/thread_utils.h" #include #include @@ -11,12 +12,6 @@ namespace sls { -// gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#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(); } diff --git a/slsReceiverSoftware/src/ClientInterface.cpp b/slsReceiverSoftware/src/ClientInterface.cpp index 0c08534a8..3cfc9d6fe 100644 --- a/slsReceiverSoftware/src/ClientInterface.cpp +++ b/slsReceiverSoftware/src/ClientInterface.cpp @@ -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 -#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(&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'; diff --git a/slsReceiverSoftware/src/FrameSynchronizerApp.cpp b/slsReceiverSoftware/src/FrameSynchronizerApp.cpp index fc48814a9..349674294 100644 --- a/slsReceiverSoftware/src/FrameSynchronizerApp.cpp +++ b/slsReceiverSoftware/src/FrameSynchronizerApp.cpp @@ -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 threads; sls::TLogLevel printHeadersLevel = sls::logDEBUG; -// gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#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() << " ]"; }); } diff --git a/slsReceiverSoftware/src/MultiReceiverApp.cpp b/slsReceiverSoftware/src/MultiReceiverApp.cpp index a1bc4cb83..ad6b869ca 100644 --- a/slsReceiverSoftware/src/MultiReceiverApp.cpp +++ b/slsReceiverSoftware/src/MultiReceiverApp.cpp @@ -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 //wait #include -// gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#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); } } diff --git a/slsReceiverSoftware/src/Receiver.cpp b/slsReceiverSoftware/src/Receiver.cpp index c9a299b19..f7372ba0e 100644 --- a/slsReceiverSoftware/src/Receiver.cpp +++ b/slsReceiverSoftware/src/Receiver.cpp @@ -19,12 +19,6 @@ namespace sls { -// gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#define gettid() syscall(SYS_gettid) -#endif - Receiver::~Receiver() = default; Receiver::Receiver(uint16_t port) { diff --git a/slsReceiverSoftware/src/ReceiverApp.cpp b/slsReceiverSoftware/src/ReceiverApp.cpp index 92ff84b50..b6d38689d 100644 --- a/slsReceiverSoftware/src/ReceiverApp.cpp +++ b/slsReceiverSoftware/src/ReceiverApp.cpp @@ -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 #include -// gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#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; } diff --git a/slsReceiverSoftware/src/ThreadObject.cpp b/slsReceiverSoftware/src/ThreadObject.cpp index 183b31ece..eec0d2aaf 100644 --- a/slsReceiverSoftware/src/ThreadObject.cpp +++ b/slsReceiverSoftware/src/ThreadObject.cpp @@ -6,18 +6,13 @@ ***********************************************/ #include "ThreadObject.h" +#include "sls/thread_utils.h" #include "sls/container_utils.h" #include #include namespace sls { -// gettid added in glibc 2.30 -#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30 -#include -#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) { diff --git a/slsSupportLib/include/sls/thread_utils.h b/slsSupportLib/include/sls/thread_utils.h new file mode 100644 index 000000000..c1a7ff69c --- /dev/null +++ b/slsSupportLib/include/sls/thread_utils.h @@ -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 // pid_t + +#if defined(__APPLE__) +#include +#include +#elif defined(__linux__) +#include +#if !defined(__GLIBC__) || \ + !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30)) +#include +#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(tid); +#elif defined(__GLIBC__) && \ + (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30)) + return ::gettid(); +#else + return static_cast(::syscall(SYS_gettid)); +#endif +} + +} // namespace sls